How to backup with rsync


Nowadays, almost everyone uses his computer for productivity and personal data management; software, configuration files, documents, are all important to users’ everyday life; and exactly for this reason, it’s fundamental to prevent data loss, with periodic backups of filesystem. There are many tools, GUI and CLI-based, which are designed for this purpose.
In this article, it’s explained how to use rsync for a full system backup.

Backup all the filesystem

It’s needed just one command in shell, with root privileges, to perform a complete backup.

# rsync -aAXv --exclude={"path/to/excluded/directories"} / /path/to/destination

This command depends on brace expansion available in bash and zsh.
The -aAX arguments are needed for ensuring that symbolic links, devices, permissions and ownerships, modification times, ACLs and extended attributes are preserved.
With --exclude option users’ can specify folders that has not to be copied to destination; usually this string contains /dev, /sys, /run, /tmp, /proc. Could be necessary to add also /mnt or /media if they’ll not be used as backup destination folder, otherwise could start an infinite loop.

Restoring backup

Restoring backup is easy as making it. It’s the same command, but with with source and destination reversed.
Remember that a gold rule is to make three backup copies, and put them in different locations; computers permits an easy access and management of your informations, but it’s also easy lose everything, so, prudence is never enough.

  • David C. Snyder

    Hmm…the example command-line should include the -H flag to preserve hard-links and -x to prevent the backup from spanning filesystems (especially important if you’re doing a backup of the root filesystem to an NFS or otherwise locally mounted second filesystem). Also, some discussion of –link-dest would be beneficial to folks interested in creating incremental backups using rsync. For example, “rm -rf /path/to/destination/tuesday; rsync -axAXHR –link-dest=../monday / /path/to/destination/tuesday”. The next day, you could run something like “rm -rf /path/to/destination/wednesday; rsync -axAXHR –link-dest=../tuesday / /path/to/destination/wednesday”. The result is that “wednesday” subdir will contain the same directory structure as “tuesday”; however, unchanged files will essentially take up no space since they will be hard-linked from the folder specified by –link-dest (note that the path is relative to the final destination). Note that the “rm -rf” commands are a good idea, but be very careful with your scripts if you’re doing this using variables. An uninitialized variable could force you to recover from backups sooner than you had planned!