Easy Linux Backups

I have tried most Linux Backup utilities, not all mind you but most of them. In the end I just created a simple perl script that generates and runs a single tar command. dar is a good alternative to tar since it allows splitting compressed files, but for a while dar was incompatible with the version of Ubuntu I was running so I decided tar was a better choice.

Some of the advantages to this simple backup solution.

  1. Make backups while you are working.
  2. Backups are pretty fast and small. My system backs up in under 4 gig, 2 for just the linux system without the home directories.
  3. Restoring does not delete new files, only files in the backup are overwritten.
  4. Easily configurable - modify the script to bakup just your music, movies, virtual OS's, Home dirs etc. This was really important to me because I do not want to backup music and movies when I back up my system. I want to be able to backup/restore just music, movies, home dirs, system etc.

Ok, now for the simple script.


#!/usr/bin/perl;

my $today = `date "+%Y-%m-%d"`;
chomp($today);

#######################################
## CONFIG
# Backup Filename - use $today for date
my $backupfilename = qq[/media/shared/backup/linux/system-backup-$today.tgz];

## Paths to backup
my @include = (
'/',
);

## Paths to exclude (these can be inside the include paths)
my @exclude = (
'/newsys/*',
'/proc/*',
'/lost+found/*',
'/dev/*',
'/mnt/*',
'/media/*',
'/sys/*',
'/tmp/*',
'/home/*',
'*.iso',
);
#####################################
# End Config

my $include = join(' ',@include);
my $exclude = join('" --exclude="',@exclude);
$exclude = qq[--exclude="$exclude"];
$command = qq[sudo tar cvpzf $backupfilename $exclude $include];
print qq[Backup started at $today\n];

## Uncomment to debug;
#print qq[$command\n];
#exit(0);

exec "$command";
print qq[\nBackup Complete\n];

Configure the script

  1. Save the script somewhere like /home/usrername/scripts/system-backup.pl
  2. Change the path in my $backupfilename = qq[/media/shared/backup/linux/system-backup-$today.tgz];
    This is where you want your backups to be stored. $today will be replaced with the current date.
  3. Add the path to the exclude list in this eaxmple the exclude line /media/* takes care of this since the backups is stored under /media/shared
    Note I use a seperate home backup script so I am excluding /home/* , you may not want that.

Running the script

  1. From the script dir type perl system-backup.pl or whatever you saved it as, type in your password when prompted (you must either have sudo access or be running as root).
    If you only run this script as root, you can remove sudo from the line $command = qq[sudo tar cvpzf $backupfilename $exclude $include];
  2. Thats it, if you want to save a log of what is being backed up so you can review it and tweak the exclude list pipe the output to a text file.
    perl system-backup.pl > system-backuplog.txt

Restoring from a Backup

To restore from a backup just type tar xvfz pathtobackup -C dir to restore to.
In the above eaxmple this would be:
tar xvfz /media/shared/backup/linux/system-backup-date.tgz -C /

That is it, enjoy!
I have attached the above script for easy downloading. The attachment has 2 scripts, one for your system and one for your home directory.