Tuesday, September 25, 2012

Rsync for backups

On my home network I had a need to backup my server and several desktops.  I went out and bought a 2TB external USB drive which I use for this. I decided to use rsync to do the backups and would have it run by cron.

Backup Script.
Here is the script that I wrote to do the backups. 
rsync -av --progress --delete --log-file=/var/log/backups/home-$(date +%Y%m%d)_rsync.log --exclude "/home/username/.gvfs" /home /blackdrive/HomeBackup
rsync -av --progress --delete --log-file=/var/log/backups/root-$(date +%Y%m%d)_rsync.log /root /blackdrive/RootBackup
rsync -av --progress --delete --log-file=/var/log/backups/storage-$(date +%Y%m%d)_rsync.log /storage /blackdrive/StorageBackup
rsync -av --progress --delete --log-file=/var/log/backups/etc-$(date +%Y%m%d)_rsync.log /etc /blackdrive/EtcBackup

I've mounted the drive at /blackdrive  ( Imaginative name.  The case is black).  Some interesting things of note in this script.  I store the logs of the backups in /var/log/backups by date.  and when backing up the home directory on my fedora server I exclude the .gvfs folder.  This is a virtual filesystem used by my desktop that no other processes seem to be able to open and it causes rsync to report problems.  the --delete cleans out files on the target drive that I've deleted on the source drive. 

Cronjob.
I then created a cronjob with this entry:
0 4 * * * /root/backups.sh

This runs the backup script at 4am everyday.  I figured no one in my house would be awake at that time.

If your wondering where the backup of the other desktops occurs.  I mount the /storage volume via nfs and samba on the various boxes in my house and they all push their backups to that folder.

Backup Postgresql Database on Fedora Linux

I had a need to backup my various databases on my home server.  My environment consists of postgresql running on a fedora 17 server.

Backing up
create a cronjob with the command:
crontab -e

put this entry in the file:
50 23 * * * /bin/pg_dumpall -U postgres > /storage/databases/postgres.backup
this tells it to dump all of the databases using the postgres user and put them in a file out in /storage/databases/

Restore
Run the following command as the postgres user.
psql -f /storage/databases/postgres.backup

I back the /storage area up on a nightly basis to external media.

I may in the future incorporate some compression and a file rotation into a script, but at this juncture I haven't had a need for that.