#!/bin/bash # from: http://www.system-rescue-cd.org/Sysresccd-manual-en_LVM_Making-consistent-backups-with-LVM VOLGROP='systemvg' # name of the volume group ORIGVOL='rootlv' # name of the logical volume to backup SNAPVOL='snaplv' # name of the snapshot to create SNAPSIZ='-l 100%FREE' # ex. '-L 4G' space to allocate for the snapshot in the volume group FSAOPTS='-z4 -j2 -s45000 -c -' # options to pass to fsarchiver BACKDIR='/mnt/hdd/backup' # where to put the backup BACKNAM='rootlv' # name of the archive # ---------------------------------------------------------------------------------------------- PATH="${PATH}:/usr/sbin:/sbin:/usr/bin:/bin" TIMESTAMP="$(date +%Y%m%d-%Hh%M)" { # only run as root if [ "$(id -u)" != '0' ] then echo "this script has to be run as root" exit 1 fi # check that the snapshot does not already exist if [ -e "/dev/${VOLGROP}/${SNAPVOL}" ] then echo "the lvm snapshot already exists, please destroy it by hand first" exit 1 fi echo ${BACKNAM}-${TIMESTAMP} echo "========================================================" # create the lvm snapshot echo "creating lvm snapshot ${SNAPVOL}..." if ! lvcreate -v -s ${SNAPSIZ} -n ${SNAPVOL} /dev/${VOLGROP}/${ORIGVOL} then echo "creation of the lvm snapshot failed" exit 1 fi # decrypt snapshot echo "decrypting snapshot into ${VOLGROP}-${SNAPVOL}_crypt..." if ! cryptsetup -v luksOpen /dev/${VOLGROP}/${SNAPVOL} ${VOLGROP}-${SNAPVOL}_crypt then echo "decrypt snapshot volume failed" exit 1 fi # main command of the script that does the real stuff echo "creating archive folder..." mkdir ${BACKDIR}/${BACKNAM}-${TIMESTAMP} echo "archiving snapshot to ${BACKDIR}/${BACKNAM}-${TIMESTAMP}/..." if fsarchiver -v savefs ${FSAOPTS} ${BACKDIR}/${BACKNAM}-${TIMESTAMP}/${BACKNAM}-${TIMESTAMP}.fsa /dev/mapper/${VOLGROP}-${SNAPVOL}_crypt then echo "fsarchiver finished successfully" #find "${BACKDIR}/${BACKNAM}-${TIMESTAMP}" -name "${BACKNAM}-${TIMESTAMP}.f*" -exec md5sum "{}" + > ${BACKDIR}/${BACKNAM}-${TIMESTAMP}/md5checklist.chk for i in $(find "${BACKDIR}/${BACKNAM}-${TIMESTAMP}" -name "${BACKNAM}-${TIMESTAMP}.f*") do echo "calculating md5 for $i" md5sum $i >> ${BACKDIR}/${BACKNAM}-${TIMESTAMP}/md5checklist.chk echo "md5 done" done RES=0 else echo "fsarchiver failed" RES=1 fi # close luks echo "closing encrypted volume..." if ! cryptsetup -v luksClose /dev/mapper/${VOLGROP}-${SNAPVOL}_crypt then echo "cannot close encrypted snapshot volume" RES=1 fi # remove the snapshot echo "removing snapshot..." if ! lvremove -f /dev/${VOLGROP}/${SNAPVOL} then echo "cannot remove the lvm snapshot" RES=1 fi echo "done. status=${RES}" exit ${RES} # variable ${TIMESTAMP} does not work in tee for some reason } 2>&1 | tee "${BACKDIR}/session.log"