xfs
[Top] [All Lists]

xfsdump script

To: linux-xfs@xxxxxxxxxxx
Subject: xfsdump script
From: Ian Cumming <ian@xxxxxxxxxx>
Date: Thu, 25 Apr 2002 16:52:26 +1000
Sender: owner-linux-xfs@xxxxxxxxxxx
User-agent: Mutt/1.3.27i
Hi, 

Just sending out this simple script for anyone who wants to see an
example of using xfsdump to backup a system to a local disk. I couldn't 
find any examples on the net, so I wrote my own.

The script performs incremental backups of my system's filesystems to a
local drive. At the start of each week a full (level 0) backup is
performed. The xfsdumps are compressed with gzip to improve on space
efficiency (in my case, I'm backup up 100Gb onto a 40Gb backup drive).

I'm not a shell script guru, but this is what I came up with. Hopefully
this might be useful to others looking for examples.

rgds,
Ian.


-- backup.sh --
#!/bin/sh
        
# Simple backup script to backup filesystems with xfsdump to a local disk.
# Written by Ian Cumming, 25/04/02 - ian@xxxxxxxxxx

# Operation:
# Incremental backups are performed daily, with full backup performed on the 
first day of the week.
# Dump files are archived with gzip to improve space efficiency.
# Compressed dumps may be restored with "gunzip -c xfsdump.gz | xfsrestore -i - 
/retore/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="/sbin/xfsdump"
MOUNT="/bin/mount"
UMOUNT="/bin/umount"
DAY="date +%a" # print the day of the week

# variables
BACKUP_PATH="/mnt/backup" # could be subdir of mount path
MOUNT_PATH="/mnt/backup"
MEDIA_LABEL="Backup Drive (hdd5)"


# 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=`get_dump_level`
        fi

        # Perform the backups
        # These commands could be rolled into a separate function, but I 
decided against this to improve readability.
        
        # backup /
        $XFSDUMP -l $LEVEL -F -E -L "/" -M "$MEDIA_LABEL" - / | gzip - > 
"$BACKUP_PATH/root/root_xfsdump$LEVEL.gz"
        
        # backup /usr
        $XFSDUMP -l $LEVEL -F -E -L "/usr" -M "$MEDIA_LABEL" - /usr | gzip - > 
"$BACKUP_PATH/usr/usr_xfsdump$LEVEL.gz"
        
        # backup /var
        $XFSDUMP -l $LEVEL -F -E -L "/var" -M "$MEDIA_LABEL" - /var | gzip - > 
"$BACKUP_PATH/var/var_xfsdump$LEVEL.gz"

        # backup /pub
        $XFSDUMP -l $LEVEL -F -E -L "/pub" -M "$MEDIA_LABEL" - /pub | gzip - > 
"$BACKUP_PATH/pub/pub_xfsdump$LEVEL.gz"

        # backup /home
        $XFSDUMP -l $LEVEL -F -E -L "/home" -M "$MEDIA_LABEL" - /home | gzip - 
> "$BACKUP_PATH/home/home_xfsdump$LEVEL.gz"


        # Perform post backup operations
        post_backup
}


# Return the dump level, indexed from Sunday (level 0)
get_dump_level()
{
        case `$DAY` in
                Sun) echo 0;;
                Mon) echo 1;;
                Tue) echo 2;;
                Wed) echo 3;;
                Thu) echo 4;;
                Fri) echo 5;;
                Sat) echo 6;;
        esac
}       


# 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 "Mounting $MOUNT_PATH"
        if ! `$MOUNT $MOUNT_PATH`; then
                echo "Unable to mount $MOUNT_PATH (perhaps its already 
mounted?)"
                exit 0
        fi
}


# commands to perform after the backup occurs
post_backup()
{
        # notify users that backup is complete
        `echo Daily backups completed. | $WALL`

        # unmount the backup volume
        echo "Unmounting $MOUNT_PATH"
        if ! `$UMOUNT $MOUNT_PATH`; then
                echo "Unable to umount $MOUNT_PATH"
        fi
}


# do main
main $@
-- end backup.sh --

-- 
Ian Cumming, ian@xxxxxxxxxxxxxx

"The number of Unix installations has grown to 10, with more expected."
-- The Unix Programmer's Manual, 2nd Edition, June, 1972


<Prev in Thread] Current Thread [Next in Thread>
  • xfsdump script, Ian Cumming <=