System Backup Script with tar

While BackupPC with it’s web interface is certainly a nice thing to have it imposes a few problems if you want to use it to backup your system. Bare metal restores are more tricky, because files are stored in a proprietary format. Hence you will need to set up BackupPC before you can restore your system, something that is not so trivial.
I looked into another system that would allow me to create a backup of my system completely automated and that would allow for simple bare metal restores. I ended up using tar, as it is a proven tool and is supported even on the most basic live distro you’ll find.
I wrote a small shell script that gives me a few options:

  • backup the master boot record
  • let exclude certain directories from my backup
  • keep defined number of versions of my backup

My entire system backed up like this ends up in a tarball of about 1.8GB.

#!/bin/bash#backup script

#check if you are root
if [ $(whoami) != ‚root‘ ]; then
echo „Must be root to run $0″
exit 1;
fi

#base dir to store backup tarball
DIRECTORY=“/path/to/backup“
DATE=`date +%Y%m%d`

#files to exclude
#exclude dynamically created system directories such as /dev or /sys and any other dirs
#you don’t want to include
#also exclude the backup dir
EXCLUDES=(/dev /home /lost+found /media /mnt /proc /sys $DIRECTORY)

#
#
# start of backup
#
#

#copy mbr to file
echo „copying master boot record to /root/mbr.bin…“

dd if=/dev/sda of=/root/mbr.bin bs=512 count=1

len=${#EXCLUDES[*]} #Num elements in EXCLUDES

echo „Backup will exclude the following $len directories:“

i=0
while [ $i -lt $len ]; do
echo „$i: ${EXCLUDES[$i]}“
let i++
done

#prepend –exclude option to every directory
for EXCLUDE in ${EXCLUDES[@]}
do
EXCLUDE=“–exclude=“${EXCLUDE}
EXCLUDELIST=“$EXCLUDELIST $EXCLUDE“
done

#check if backup disk is available
if [ -d „$DIRECTORY“ ]; then
# Will enter here if $DIRECTORY exists

echo „Starting backup with tar cvpzf $DIRECTORY/${DATE}_backup.tgz $EXCLUDELIST /“
#start actual backup process
tar cvpzf $DIRECTORY/${DATE}_backup.tgz $EXCLUDELIST /

echo „change permissions of backup to root only access…“

chmod 700 $DIRECTORY/${DATE}_backup.tgz

echo „${DATE}_backup.tgz has been successfully created.“
echo „Size of backup is `ls -lh $DIRECTORY | grep ${DATE}_backup.tgz | awk ‚{print $5 }’`“

#deleting old versions
BACKUP_VERSIONS=(`ls $DIRECTORY | grep _backup.tgz`)

len=${#BACKUP_VERSIONS[*]}
i=0

#if more than 4 versions exist
if [ $len -gt 4 ]; then

#calculate number of versions to remove
rmno=$(($len-4))

#delete versions at the beginning of the list
while [ $i -lt $rmno ]; do

echo „removing ${BACKUP_VERSIONS[$i]} …“
rm $DIRECTORY/${BACKUP_VERSIONS[$i]}

let i++
done
fi
else
echo „cannot start backup, backup disk is not available“
exit 1
fi

If you want to restore the system you can do so simply by untaring to another disk:

tar xvpfz backup.tgz -C /path/to/disk_mount/

If you are restoring to another disk with a different partition scheme it may be necessary to recreate some entries in /etc/fstab as well as in grub.conf regarding the root device for grub and the kernel.
Restoring the master boot record is equally simple:

dd if=/root/mbr.bin of=/dev/target_disk count=1 bs=446

bs equals 446 if we do not want to overwrite the partition table that may be different on our new disk. If it’s the same we can also restore with bs=512.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.