Cloning or backing up a system with rsync

I have used rsync as the tool of my choice for a long time to often clone my system or make a backup, which I can still boot from. Feel free to use this code for your own projects. I would enjoy an attribution and a comment here or an email if you use it, but I will put this into the public domain.

Here is the rsync script I use. You just call it as modersync source-dir remote-dir. Source or remote-dir can be even / (the root dir to copy the running system or overwrite the running system). Be careful, use the switch -n (dry run) the first time you experiment with this. Source and remote-dir can even be remote locations reachable via scp.

[bash]

!/bin/bash

rsync a mobiledesktop

parameters: src, dest

src="$1" dst="$2"

LOGFILE=var/log/modersync.log

echo "Welcome to modersync, written by ulno (http://ulno.net)." echo "" echo "Last calls of modersync:" tail "$src/$LOGFILE" "$dst/$LOGFILE"|grep -v "$LOGFILE"|sort -u|tail echo "" echo "Source will be \"$src/\"" echo "Destination will be \"$dst/\"" echo "Extra parameters will be: \"$3 $4\"" echo "To proceed hit return (to interrupt press ctrl-c)." read

test -d "$src" -a -d "$dst" || \ ( echo "specify source and destination-directory!" exit 1 )

rsync $3 $4 -av -H --delete --progress \ --exclude /dev \ --exclude /proc \ --exclude /sys \ --exclude /tmp \ --exclude /mnt \ --exclude /mnt2 \ --exclude /media \ --exclude /MoDeSync \ --exclude .gvfs \ --exclude /etc/modules \ --exclude /usr/local/etc/ulno-machine-settings \ --exclude /boot/grub/menu.lst \ --exclude /boot/grub/grub.cfg \ --exclude /etc/default/grub \ --exclude /etc/modules \ --exclude /etc/fstab \ --exclude /etc/mtab \ --exclude /etc/X11/xorg.conf \ --exclude /etc/hosts \ --exclude /etc/hostname \ --exclude /etc/resolv.conf \ --exclude /etc/uswsusp.conf \ --exclude /var/run \ --exclude /run \ --exclude /etc/modprobe.d/blacklist-custom.conf \ --exclude "$LOGFILE" \ "$src/" "$dst/" rm "$dst/MoDeSync/dirty.generated" &> /dev/null cp "$src/MoDeSync/dirty.generated" "$dst/MoDeSync/" &> /dev/null

logline="$(date -u +%s) $(date) on $(hostname) from /$src to /$dst." echo $logline >> "$src/$LOGFILE" echo $logline >> "$dst/$LOGFILE"

ldconfig update-grub dpkg-reconfigure initramfs-tools

echo echo "Done."

[/bash]

Be careful, this script demands the folder MoDeSync to be present. The log is saved there and will give you a hint about the copy direction in the beginning.

Comments