Schlagwort-Archiv: Backup

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.

Mac OS X TimeMachine Backups on a Linux Server

With Mac OS X 10.5 Apple introduced it’s first backup solution for the rest of us. Most other programs I’ve used were either too complicated, too unreliable or a combination of the two or just to plain expensive. TimeMachine aims to solve all of that.

In the past I’ve used anything from plain copies via the Finder to Retrospect, Archiware (a great tool if you have a few machines and a server), rsync etc. For my dad for example all of those were not quite what was needed. The problem with most of these solutions in our modern world are, when you use a Laptop, you are not always connected to the backup system and hence schedules fail to run. As soon as manual intervention is needed, backups usually don’t happen.

TimeMachines approach seems logical with the use of external disks. As I have a Linux server running in my home and my MacBook Pro is always connected to the network be it via AirPort or Ethernet it makes sense to use the server for backups.

After a bit of googling I found a post on http://www.kremalicious.com/2008/06/ubuntu-as-mac-file-server-and-time-m… outlining
This is what I had to do:

Prerequisites: I run an Ubuntu based system in version 8.10. Most of these things should apply to other Linux machines as well though.

The ubuntu/debian netatalk package comes without ssl support that is needed for Mac OS X to work because OpenSSL is not compatible with the GPL. Hence you either need to compile your own package or download this one here:

Rolling your own involves downloading the source packages

# apt-get build-dep netatalk
# apt-get install cracklib2-dev fakeroot libssl-dev
# apt-get source netatalk
# cd /usr/src/netatalk-2*
#DEB_BUILD_OPTIONS=ssl dpkg-buildpackage -rfakeroot

Install it with

# dpkg -i netatalk*.deb

In order to prevent Ubuntu from automatically upgrading you newly installed package you need to set it on hold:

# echo „netatalk hold“ | sudo dpkg –set-selections

All you need to do next is configure /etc/default/netatalk and turn off everything but AFPD_RUN=yes

edit /etc/netatalk/afpd.conf to have the last line read

– -transall -uamlist uams_randnum.so,uams_dhx.so -nosavepassword -advertise_ssh

and finally in /etc/netatalk/AppleVolumes.default you get to list the shares netatalk shall serve you. Edit the file according to your wishes. It could be something like this:

/srv/TimeMachine/sg/ „TimeMachine“ options:usedots,upriv allow:myusername

That way only the user „myusername“ can access the share which is probably what you want. Also the share is nicely advertised as „TimMachine“.

Next in line is a Bonjour/Zeroconf daemon that will advertise the netatalk services on the network. In this case Avahi is used for that purpose.

a simple

# apt-get install avahi-daemon
# apt-get install libnss-mdns

should be all that is needed.

edit the hosts line on /etc/nsswitch.conf to read

hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4 mdns

Now we need to tell Avahi that it needs to broadcast the availability of AFP across the network so that the server will automatically show up on the MacBook Pro.

Open /etc/avahi/services/afpd.service

<?xml version=“1.0″ standalone=’no‘?><!–*-nxml-*–>
<!DOCTYPE service-group SYSTEM „avahi-service.dtd“>
<service-group>
<name replace-wildcards=“yes“>%h</name>
<service>
<type>_afpovertcp._tcp</type>
<port>548</port>
</service>
<service>
<type>_device-info._tcp</type>
<port>0</port>
<txt-record>model=Xserve</txt-record>
</service>
</service-group>

now restart avahi

at this point the share pops up under network on the Mac.

Last but not least TimeMachine needs to be configured to use that share as its storage pool. Nicely enough Apple hides all network volumes except the ones from Mac OS X Server and TimeCapsule in the TimeMachine control panel. Heck, not even an AirPort base station with an attached USB disk can be used. Sometimes I really don’t understand Apple…

To get the Mac to see the network volume as a TimeMachine storage pool open a terminal and write

defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1

TimeMachine should create a sparsebundle disk image for the files. That is a special disk image that only uses as much space as is really just needed and grows in size afterwards. On my box this wasn’t created automaically, but you can easily create one with the disk utility on the Mac and copy it over to the Linux box. The filename of the image must be computername_MACADDRESS-OF-ETH0-WITHOUT-COLONS.sparsebundle
computername ist not your actual computername as seen in the sharing panel of system preferences but always simply „computername“.
That should do.