backup script with mgzip,tar,xfsdump and incremental function

#!/bin/sh

# Compressed dumps may be restored with “gunzip -c xfsdump.gz | xfsrestore -i – /restore/path”

# Usage:
# ./backup.sh [dump_level]
# With no arguments, the script will backup filesystems using incremental dump options (-l)
# The dump level is the offset from the first day of the week, ie: Sunday = 0, Monday = 1, etc..
# If dump_level is specified, then this will override the default behaviour.

# Example:
# An example cron job entry, to run at 4am every day.
# 0 4 * * * * /root/scripts/backup.sh

# commands
WALL=”/usr/bin/wall”
XFSDUMP=”/usr/sbin/xfsdump”
MOUNT=”/bin/mount”
UMOUNT=”/bin/umount”

# variables
BACKUP_PATH=”/backup/gw” # could be subdir of mount path
MOUNT_PATH=”/lvm-snapshot/root/”
MEDIA_LABEL=”Backup Drive (data)”

# it all starts here
main() {
# Perform pre backup operations
pre_backup

# Get the level of the backup (can be overriden with argv[1])
if [ “$1” != “” ] ; then
echo “Overriding dump level with $1”
LEVEL=$1
else
LEVEL=`date +%w`
fi

# Perform the backups
# These commands could be rolled into a separate function, but I
# decided against this to improve readability.

# backup /data/diskless
if [ “$LEVEL” == “0” ] ; then
$XFSDUMP -F – /data | mgzip > “$BACKUP_PATH/data_xfsdump$LEVEL.gz”
else
$XFSDUMP -F -l $LEVEL – /data | mgzip > “$BACKUP_PATH/data_xfsdump$LEVEL.gz”
fi
# backup /
if [ “$LEVEL” == “0” ] ; then
rm -rf $BACKUP_PATH/root.snar
fi
tar -g $BACKUP_PATH/root.snar -cvf – $MOUNT_PATH | mgzip > $BACKUP_PATH/root_tardump$LEVEL.gz
# backup /boot
if [ “$LEVEL” == “0” ] ; then
rm -rf $BACKUP_PATH/boot.snar
fi
tar -g $BACKUP_PATH/boot.snar -cvf – /boot | mgzip > $BACKUP_PATH/boot_tardump$LEVEL.gz

# Perform post backup operations
post_backup
}

# commands to perform before the backup occurs
pre_backup() {
# warn users of backup
`echo Daily backups are being performed. You may stay logged in while this occurs. | $WALL`

# set the umask
umask 027

# mount the backup volume
echo “Creating LVM lv.”
lvcreate -L1G -s /dev/main/root -n root_backup
echo “Mounting $MOUNT_PATH”
if ! `$MOUNT /dev/main/root_backup $MOUNT_PATH -o ro -t ext4`; then
echo “Unable to mount $MOUNT_PATH (perhaps its already #mounted?)”
exit 1
fi
}

# commands to perform after the backup occurs
post_backup() {

# unmount the backup volume
echo “Unmounting $MOUNT_PATH”
if ! `$UMOUNT $MOUNT_PATH`; then
echo “Unable to umount $MOUNT_PATH”
exit 1
fi
echo “Removing LVM lv.”
lvremove -f /dev/main/root_backup

# notify users that backup is complete
if [ $? == 0 ]; then
`echo Backups completed. | $WALL`
else
`echo Backups ERROR. Something got wrong. | $WALL`
fi

}

# do main
main $@