Nightly Mirroring via Rsync

In this article we showed how to use rsync to mirror a website. Well, rsync can also be used to provide a lukewarm recovery system. On this workstation, the root filesystem is on /dev/hda3, and we will mirror it to /dev/sda3, which we have mounted on /mnt. If you don’t have rsync, grab rsync from here, compile and install:

# tar -xzf rsyn*.gz
rsync-2.5.5]#
rsync-2.5.5]# ./configure --prefix=/usr
configure: Configuring rsync 2.5.5
checking build system type... i686-pc-linux-gnu
rsync 2.5.5 configuration successful
.
.
.
rsync-2.5.5]# make
gcc -I. -I. -g -O2 -DHAVE_CONFIG_H -Wall -W -c rsync.c -o rsync.o
gcc -I. -I. -g -O2 -DHAVE_CONFIG_H -Wall -W -c generator.c -o generator.o
generator.c: In function `recv_generator':
rsync-2.5.5]# make install

Let’s try a test rsync:

# rsync -avz --delete  --exclude  "/share/" 
--exclude "/mnt/" / /mnt
wrote 3640351862 bytes  read 1463556 bytes  1971212.68 bytes/sec
total size is 5732809242  speedup is 1.57
rsync error: partial transfer (code 23) at main.c(578)

The partial transfer error here is because we are transferring /proc, which is silly, because proc will be built at run-time. Let’s exclude proc and run again:

# rsync -avz --delete  --exclude  "/share/" --exclude "/mnt/"
--exclude "/proc/" / /mnt
building file list ... done
.
.
.
wrote 4817639 bytes  read 628 bytes  143828.87 bytes/sec
total size is 4795536117  speedup is 995.28
#

The –delete option deletes the files that don’t exist on the source from the target. Now, let’s try out our new recovery system. Now would be a good time to make a recovery diskette and test it with both root partitions. See Bull-Headed Booting for tips on making a boot diskette. For our purposes, though, putting another entry in lilo.conf is just fine. If you have grub, the general idea is similar. Be careful that you know what bootloader you are using!! We happen to be using lilo. Here is our original lilo.conf:

prompt
timeout=50
default=linux
boot=/dev/hda
map=/boot/map
install=/boot/boot-menu.b
image=/boot/vmlinuz-2.4.20
label=linux
root=/dev/hda3
read-only

Let’s add a section. Be careful to not make the recovery section default, or you might accidentally boot your recovery system and not know it:

prompt
timeout=50
default=linux
boot=/dev/hda
map=/boot/map
install=/boot/boot-menu.b
image=/boot/vmlinuz-2.4.20
label=linux
root=/dev/hda3
read-only
image=/boot/vmlinuz-2.4.20
label=recovery
root=/dev/sda3
read-only

Run lilo when finished:

# lilo
Warning: LBA32 addressing assumed
Added linux *
Added recovery
#

Reboot, and test the new root filesystem out. To wrap this up and make all happy, we created an /rsyncmnt directory, and added a crontab entry: 0 0 * * * umount /dev/sda3;mount -t ext3 /dev/sda3 /rsyncmnt;/usr/bin/rsync -avz –delete –exclude “/share/” –exclude “/mnt/”–exclude “/mnt2/” –exclude “/rsyncmnt/” –exclude “/proc/” / /rsyncmnt;umount /rsyncmnt. With this crontab entry, first we unmount anything on /dev/sda3, then we mount rsyncmnt. On the rsync command, notice that we are excluding the other mount points like share, mnt, and mnt2 that we use. If you don’t exclude rsyncmnt, heheh, you will rsync the stuff that rsyncs. Nasty business. We then unmount /rsyncmnt. The job will run at midnight every night.