Blog of Raivo Laanemets

Stories about web development, freelancing and personal computers.

Home directory backup script

On

This my backup script for my home directory. It sends backup through rsync over SSH into a remote server. It skips all top-level directories that contain a .git directory. This works for me as all my git repos have remote origins which are backed up by other means. I have written the backup script as a Makefile. The script is optimized for speed so that it can be run every day.

backup:
	find . -maxdepth 2 -iname '*.git' | xargs -n1 dirname | xargs -n1 basename > repos
	echo Downloads >> repos
	echo machines >> repos
	echo .cache >> repos
	rsync -ravzu --delete --exclude-from repos -e ssh . backup@home.rlaanemets.com:/files/backups/raivo
	rm -f repos

.PHONY: backup

Explanation

find . -maxdepth 2 -iname '*.git' finds all .git directories under top-level directories.

| xargs -n1 dirname chops off the /.git part.

| xargs -n1 basename > repos removes path to the top-level directory (this might actually be not necessary but it gives clean names) and stores these directory names in a file called repos.

echo Downloads >> repos adds extra entries into the file. These will be also skipped from the backup.

rsync -ravzu --delete --exclude-from repos -e ssh . backup@... backs up the files into the remote location while skipping those that occur in the repos file. Authentication to the remote account has been set up using a PKI key pair. The explanation of rsync flags:

  • r - copy directories recursively.
  • a - enables archive mode. Tries to follow symlinks and preserve as much information as possible.
  • v - verbose mode. Feedback to the user.
  • z - compress transmitted data.
  • u - skips file that are newer on the remote location and also skips files that have same timestamp and same size.
  • --delete - removes remote files that do not exist locally.

rm -f repos removes the repos file as it is not needed anymore.

To run the backup process, make backup must be executed in the home directory. It is strongly recommended to take periodic snapshots of remote files, possibly onto a write-once media.


Comments

No comments have been added so far.

Email is not displayed anywhere.
URLs (max 3) starting with http:// or https:// can be used. Use @Name to mention someone.