Snapshot anlegen

Written by cobalt on März 2nd, 2012

Mit dem folgenden script läßt sich komfortabel ein Snapshot einer Partition auf einem LVM anlegen:

#!/bin/bash

# snapshot logical volume block device
# (c)2011 Hans-Helmar Althaus
# Version, Date, Who, What
# 1.0.0, 2011.12.09, HHA, initial Version
# 1.0.1, 2011.12.12, HHA, wait for udev to settle down

VERSION="1.0.1"
MPOINT="/"

# LVLIST="VGNAME:LVNAME:MOUNTDIR:SNAPSIZE"
LVLIST="data:storage:storage:32G"

snapvolume() {
  VGNAME=$1
  LVNAME=$2
  FSNAME=$3
  SNAPSIZE=$4
  [ -z $4 ] && return -1

  mkdir -p ${MPOINT}/${FSNAME}_snap || return 1

  if cat /proc/mounts | awk '{print $2}' | grep ${MPOINT}/${FSNAME}_snap ; then
    umount ${MPOINT}/${FSNAME}_snap || return 2
  fi

  # check for (and umount) bind mounts (autofs)
  for fs in $(mount | grep ${MPOINT}/${FSNAME}_snap | awk '{print $1}') ; do 
    umount $fs || return 3
  done

  if lvdisplay | grep 'LV Name' | grep /dev/${VGNAME}/${LVNAME} | grep /dev/${VGNAME}/${LVNAME}_snap ; then
    for i in {1..5}; do # try ...
      ret=4
      if lvremove -f /dev/${VGNAME}/${LVNAME}_snap; then
        ret=0
        break
      else # wait for udev to finish I/O on logical volume block device, this is a known bug see:
           # https://bugzilla.redhat.com/show_bug.cgi?id=570359
        sleep 1
        udevadm settle
      fi
    done
    [ "$ret" = "0" ] || return $ret
  fi
  sync
  lvcreate -L${SNAPSIZE} -s -n ${LVNAME}_snap /dev/${VGNAME}/${LVNAME} || return 5
  mount -o ro /dev/${VGNAME}/${LVNAME}_snap ${MPOINT}/${FSNAME}_snap || return 6
}

for LVSNAP in ${LVLIST}; do
  snapvolume $(echo ${LVSNAP} | awk -F: '{printf( "%s %s %s %s\n",$1,$2,$3,$4)}' )
done
 

Comments are closed.