Zálohovací script XEN strojů pomocí LVM


viz Zálohování XEN strojů
#!/bin/bash
#e-mail address for error message
ADMIN="jmeno@domena.primarni jmeno@domena.zalozni"
#hostname
HOSTNAME=`/bin/hostname`
#e-mail FRROM  field
FROM="xenbackup@$HOSTNAME"

#backupfolder
HOMEBCKP="/cesta/na/nejake/misto/pro/zalohy/xen/"
#backupname
BACKUPNAME="virtuals"
#backup datetime file name
BCKPDATETIME="backup-datetime"
#array of backuped virt. machines
PAUSE_VIRTUALS=('nejakyxenstrojnejminzatizeny' 'druhyxenstroj' 'tretixenstrojnejviczatizeny')

#LVM
#grp - path of logical volume
LVM_GRP="/dev/main"
#full name of logical volume to backup
LVM_VIRTUALS="${LVM_GRP}/virtuals"
#lvm backup(snapshot) logical volume name
LVM_BCKPNAME="lvmbackup"
#full name of backup(snapshot) volume name
LVM_SNAPSHOT="${LVM_GRP}/${LVM_BCKPNAME}"
#size of snapshot lvm, have to be enough size for all changes during backuping
LV_SIZE="20G"
#backup mount dir,have to exists !
MNT_DIR="/mnt/lvmbackup"
#any dir for cd to - for release backup snapshot (safe umount)
INUSE_DIR="/tmp"

#Commands
MOUNT_CMD="/bin/mount"
UMOUNT_CMD="/bin/umount"

XM_CMD="/usr/sbin/xm"
#pause between sysrq and sleep - optimistic idea
XM_PAUSE=1

LVCREATE_CMD="/sbin/lvcreate"
LVREMOVE_CMD="/sbin/lvremove"

SYNC_CMD="/bin/sync"

#tar command use
TAR_CMD="/bin/tar"

myexit(){
 logger -p error "backup-virtuals: Failure. Message: $1"
  echo "BACKUP-VIRTUALS ERROR !!! on ${HOSTNAME} Message: $1" | /usr/bin/mailx \
    -s "BACKUP-VIRTUALS ERROR: ${HOSTNAME}" -r ${FROM} ${ADMIN}
 exit $2
}

echo "`date "+%F %X"` move old backup to *.old - start"
logger -p info "backup-virtuals: start"
#to backup directory
cd $HOMEBCKP
if [ $? != 0 ]; then
  myexit "Error: directory $HOMEBCKP not exists" 1
fi


#move the old backup
if [ -e ./$BACKUPNAME.tar.gz ]; then
  mv -f ./$BACKUPNAME.tar.gz ./$BACKUPNAME.tar.gz.old
  if [ $? != 0 ]; then
    myexit "Error: directory $POMPATH not exists" 3
  fi
  if [ -e ./$BCKPDATETIME ]; then
    mv -f ./$BCKPDATETIME ./$BCKPDATETIME.old
  fi
fi
echo "`date "+%F %X"` move old backup to *.old - done"

myunpause(){
  for UNMACHINE in "${PAUSE_VIRTUALS[@]}"; do
    $XM_CMD unpause $UNMACHINE
  done
}

#pause machines
echo "`date "+%F %X"` Pause machines - start"
for MACHINE in "${PAUSE_VIRTUALS[@]}"; do
   $XM_CMD sysrq $MACHINE s
   sleep $XM_PAUSE
   $XM_CMD pause $MACHINE
   res=$?
   if [ $res != 0 ]; then
     myunpause
     myexit "Error: $cmd error, return $res" 5
   fi
done
echo "`date "+%F %X"` Pause machines - done"

#sync
$SYNC_CMD

#create lvm lvmbackup
echo "`date "+%F %X"` Create lvm $LVM_BCKPNAME - start"
$LVCREATE_CMD -L$LV_SIZE -s -n  $LVM_BCKPNAME $LVM_VIRTUALS
if [ $? != 0 ]; then
  myunpause
  myexit "Error: lvcreate fail" 7
fi
echo "`date "+%F %X"` Create lvm $LVM_BCKPNAME - done"

#error message
txt="YOU HAVE TO USE lvremove command as soon as possible"

#unpause all
echo "`date "+%F %X"` UnPause machines - start"
for MACHINE in "${PAUSE_VIRTUALS[@]}"; do
  $XM_CMD unpause $MACHINE
  res=$?
  if [ $res != 0 ]; then
     myunpause
     $LVREMOVE_CMD -f $LVM_SNAPSHOT
     if [ $? = 0 ]; then
       txt=" $LVM_BCKPNAME removed"
     fi
     myexit "Error: $cmd error, return $res, $txt" 10
  fi
done
echo "`date "+%F %X"` UnPause machines - done"

#$MOUNT_CMD
echo "`date "+%F %X"` Mount and cd to $MNT_DIR - start"
$MOUNT_CMD $LVM_SNAPSHOT $MNT_DIR
if [ $? != 0 ]; then
  cd $INUSE_DIR
  $LVREMOVE_CMD -f $LVM_SNAPSHOT
  if [ $? = 0 ]; then
     txt="$LVM_BCKPNAME removed"
  fi
  myexit "Error: $MOUNT_CMD fail, $txt" 15
fi

cd $MNT_DIR
if [ $? != 0 ]; then
  cd $INUSE_DIR
  $SYNC_CMD
  $UMOUNT_CMD $MNT_DIR
  if [ $? = 0 ]; then
    $LVREMOVE_CMD -f $LVM_SNAPSHOT
    if [ $? = 0 ]; then
      txt="$LVM_BCKPNAME removed"
    fi
  fi
  myexit "Error: cd $MNT_DIR fail, $txt" 17
fi
echo "`date "+%F %X"` Mount and cd to $MNT_DIR - done"

#backup machine
echo "`date "+%F %X"` Backup to tar.gz - start"
$TAR_CMD -czf "$HOMEBCKP/$BACKUPNAME.tar.gz" --exclude='*.iso' .
if [ $? != 0 ]; then
  cd $INUSE_DIR
  $SYNC_CMD
  $UMOUNT_CMD $MNT_DIR
  if [ $? = 0 ]; then
    $LVREMOVE_CMD -f $LVM_SNAPSHOT
    if [ $? = 0 ]; then
      txt="$LVM_BCKPNAME removed"
    fi
  fi
  myexit "Error: backup tar fail, $txt" 20
fi
echo "`date "+%F %X"` Backup to tar.gz - done"

#$UMOUNT_CMD
echo "`date "+%F %X"` $UMOUNT_CMD - start"
#for sure sequence cd $INUSE_DIR  for inuse $MNT_DIR
cd $INUSE_DIR
$SYNC_CMD
$UMOUNT_CMD $MNT_DIR
if [ $? != 0 ]; then
  $SYNC_CMD
  $UMOUNT_CMD $MNT_DIR
  if [ $? = 0 ]; then
    $LVREMOVE_CMD -f $LVM_SNAPSHOT
    if [ $? = 0 ]; then
      txt="$LVM_BCKPNAME removed"
    fi
  fi
  myexit "Error: backup $UMOUNT_CMD fail, $txt" 25
fi
echo "`date "+%F %X"` $UMOUNT_CMD - done"

#lvm remove
echo "Lvremove - start"
$LVREMOVE_CMD -f $LVM_SNAPSHOT
if [ $? != 0 ]; then
  myexit "Error: backup lvremove fail, $txt" 30
fi
echo "`date "+%F %X"` Lvremove - done"

date "+%F %X" > $HOMEBCKP/$BCKPDATETIME
echo "`date "+%F %X"` Backup done"
echo "---------------------------------------------"
logger -p info "backup-virtuals: OK"

exit 0