September 2
2008
Setting up some scripts to automate the process
Now it’s time to put things together and to automate the backup process; for convenience i’ve created two simple bash script, one to invoke dar, and one to invoke rsync. These are then put into crontab.
The darBckScript.sh
#!/bin/bash
DIR="/backups"
RSYNCSCRIPT="/home/rsyncBackup.sh"
DARBINARY="/usr/local/bin/dar"
USAGE="Usage: $0 F|D [TEST]"
if [ "$#" -lt "1" ]; then
echo "$USAGE"
exit -1
fi
if [[ "$1" != "F" && "$1" != "D" ]]; then
echo "$USAGE"
exit -1
fi
if [[ "$2" != "" && "$2" != "TEST" ]]; then
echo "$USAGE"
exit -1
fi
if [ ! -d $DIR ]
then
echo "$DIR does not exists or is not a directory"
echo "creating $DIR directory..."
mkdir "$DIR" || { echo "Failed to create $DIR. Cannot continue."; exit -1; }
fi
if [ "$1" = "F" ]; then
#FULL BACKUP
echo "Running FULL BACKUP"
FILE=${DIR}/`/bin/date -I`_data
elif [ "$1" = "D" ]; then
#DIFFERENTIAL BACKUP
echo "Running DIFFERENTIAL BACKUP"
FILE=${DIR}/`/bin/date -I`_diff
PREV=`/bin/ls $DIR/*.dar|/usr/bin/tail -n 1`
REFSTR="-A ${PREV%%.*}"
fi
if [ "$2" = "TEST" ]; then
TESTSTR="-e -v -vs"
fi
PS4="Dar command line: "
set -x
$DARBINARY -m 128 $TESTSTR -y -s 600M -D -R / -c "$FILE" \
-Z "*.gz" -Z "*.zip" -Z "*.rar" -Z "*.bz2" -Z "*.mp3" -Z "*.avi" -Z "*.7z" -Z "*.gif" \
-Z "*.png" -Z "*.jpeg" -Z "*.jpg" -Z "*.mpg" -Z "*.gz" -Z "*.gzip" -Z "*.mov" -Z "*.swf" \
-X "<file_exclusion_pattern_1>" \
-X "<file_exclusion_pattern_2>" \
-X "<...>" \
-X "<file_exclusion_pattern_n>" \
-g <include_dir_1> \
-g <include_dir_2> \
-g <...> \
-g <include_dir_m> \
-P <exclude_dir_1> \
-P <exclude_dir_2> \
-P <...> \
-P <exclude_dir_n> \
$REFSTR \
|| exit -2
set +x
$DARBINARY -t $FILE || exit -3
$RSYNCSCRIPT
exit 0
The script can be invoked with ./darBckScript.sh F|D [TEST], where F means full backup, D means differential backup, and TEST is an optional command line argument which enables verbose output and make a fake run (i.e. no backup is generated nor modified).
You may need to modify the lines 4,5,6, and, to match your requirements, you need to edit lines from 55 to 66;
for example if you want to make a backup with these characteristics:
- backup of directory/home/dir1/
- backup of directory/home/dir2/
- exclude directory /home/dir1/not_important from backup
- exclude all *.gz files
then you need to edit lines from 55 to 66 as:
-X "*.gz" \ -g home/dir1 \ -g home/dir1 \ -P home/dir1/not_important \
Please note that the directories are specified without the starting "/" because they must be relative to the / directory, as specified by the -R / switch.+
The rsyncBackup.sh
#!/bin/bash
rsync --verbose --progress --stats --compress \
--recursive --times --perms --links --delete \
--password-file <password_file> \
<backups_directory> <user>@<remote_host>::<module_name>
As you can see it is only the rsync command discussed above. I’ve chosen to make it in a separate file because it is conceptually separated from the backup itself, and moreover it may be invoked individually at any time, for example for a manual file transfer. You need to edit the <…> parameters to match your requirements.
Put scripts under cron control
Now you have to schedule the backups under cron. I’ve decided to make a full backup once a month (on the first day of month) and a differential backup once a day. Both backups plans run at 1.15 AM;
This is what we have to type after crontab -e:
15 1 2-31 * * /full/path/to/your/darBckScript.sh D 15 1 1 * * /full/path/to/your/darBckScript.sh F










(2 votes, average: 4.50 out of 5)
Leave a Reply