Full system backup with rsync
I created a backup script for my data & system and i'd like to share it. I converted this forum post into a wiki page. Maybe it helps someone.
I have a 320GB hdd (1) as my main hard drive, and a 160GB hdd (2) as a backup drive (i keep this one in a drawer nearby, disconnected from the computer). What i wanted was to create a partial copy of 1 on 2, such that 2 would contain the entire system (bootable) + a part of my data.
1. Filesystem and partitioning
You need to have the same filesystem on both (i have ext4). Maybe it works with a mix of ext3 and 4, but it's better to have just one. The partitioning scheme on hdd 1 doesn't matter, but for hdd 2 you need a single partition to which you backup.
2. The scripts
#!/bin/sh # rsync backup script sudo rsync -av --delete-excluded --exclude-from="excl" / "$1" sudo touch "$1"/BACKUP
This one is very simple. You rsync in archive mode (which ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved) and exclude the files that match the patterns from [b]excl[/b]. The / is the source from where you backup (in our case the whole root) and "$1" is the destination to where you backup (this is passed as an argument to the script).
# Include + /dev/console + /dev/initctl + /dev/null + /dev/zero + /media/win + /var/run/mpd + /home/wooptoo/music/Amestecate + /home/wooptoo/music/script + /home/wooptoo/music/list.txt + /home/wooptoo/music/.hg* # Exclude - /home/wooptoo/dl/* - /home/wooptoo/games/kits/* - /home/wooptoo/mov/* - /home/wooptoo/music/* - /dev/* - /media/* - /media/win/* - /mnt/* - /proc/* - /sys/* - /tmp/* - /var/run/* - /var/run/mpd/*
This is a bit more tricky. It's an exclude (and include) file in rsync format. Exclude: I excluded my games, movies and music from the backup, and also the system directories /dev, /media, /mnt, /proc, /sys, /tmp, /var/run. These are excluded because their content is created at runtime by the system. Note that the direcotries themselves are preserved (you need them!) but they are empty. Include: even though i excluded /dev, i need to include 4 file from it (which are not dinamically created by udev), these are console, initctl, null, zero. I also included the directories /media/win and /var/run/mpd. But these are empty, because their content was excluded (in the exclude section).
3. Backing up
So we got these two files: backup and excl. Mount the backup hdd, let's say at /media/backup/ and run the script:
rsync will backup the whole root to that destination. I excluded game kits, music and movies from my backup because they are just too large to fit on hdd 2, and it would also take a lot of time to backup and keep in sync afterwards.
4. Bootloader setup
After the sync is finished you need to install a boot loader on hdd2, so you can have a working copy of your system. Open the grub console and type in:
root (hd1,0) setup (hd1)
The root command tells grub where your system is installed (in this case hdd 2, first partition). Setup tells grub where to install the boot loader. In my example it is installed in the MBR of hdd 2.
The problem here is that the boot loader installs correctly, but its menu entries are for the partitions of the main system, not the backup system. So if you'll try to boot the backup system, it won't work. You can fix this by creating a custom menu.lst for the backup hdd. But i prefer not to do this, in order to have an accurate copy of my data. I just prefer to edit the entries from the boot menu on the fly if i need to boot the backup directly. But you can automatically add a custom menu.lst to the backup hdd from the backup script:
#!/bin/sh # rsync backup script sudo rsync -av --delete-excluded --exclude-from="excl" / "$1" sudo cp ~/custom.menu.lst "$1"/boot/grub/menu.lst sudo touch "$1"/BACKUP
5. Reboot and try out your new system
I think this approach (system + data backup) is better than just data backup because if something goes wrong with the main hdd, you can always swap in the backup one and continue working. Besides this, you now have another working system, from which you can recover the main one without the need of live CDs.
In my setup both hdds are SATA with AHCI, so they are hot-pluggable. You can plugin the backup drive, run the backup script, and disconnect it. This is very advantageous because you don't have to reboot. But you can use an USB stick/hdd as backup drive if you only have IDE.