Backup home

  Last modified on Wednesday 12 August 2015

Update: I am now using rsnapshot to make incremental backups.

Make automatic backups of a home folder using a dash of shell scripting + rsync + SSH + cron.

Let's go!

One of the advantages of setting up a Linux home server is providing a 24/7 uptime location to store backups of important files. A little peace of mind can be gained in a few steps as I describe the simple backup solution I use to automatically mirror my laptop's home folder over the local area network (LAN).

Server is a Raspberry Pi 2 (example: login:pi/ip_address:192.168.1.88) connected to a 1TB USB hard drive running Debian.

0. SSH

Secure remote access using keys to configure SSH for encrypted connections between server and client. Keychain makes it easy to use a passphrase-protected encryption key in automated scripts.

1. Script

On the server

Create a backup directory to hold the contents of home:

$ mkdir ~/backup-home

On the client

Create a backup script using the rsync file-copy tool. A sample myRsync script to backup HOME includes:

  • load the details of the SSH key stored in $HOME/.keychain/$HOSTNAME-sh
  • mirror $HOME/ to 192.168.1.88:~/backup-home/ using options --archive and --delete
  • exclude certain items from the backup like cache and trash

WARNING! Take care nothing important is wiped out using --delete. First run rsync in a console with option --dry-run to test settings:

$ rsync --verbose --archive --delete --dry-run ~/ pi@192.168.1.88:~/backup-home/

Sample script:

#!/bin/bash
# ~/bin/myRsync
ssh_agent="${HOME}/.keychain/$HOSTNAME-sh"
rsync_options="--verbose --archive --delete"
rsync_exclude="--exclude=*[Cc]ache* --exclude=*[Tt]rash*"
rsync_src="${HOME}/"
rsync_dest="pi@192.168.1.88:~/backup_home/"

. $ssh_agent
echo $(date)
rsync $rsync_options $rsync_exclude $rsync_src $rsync_dest

Place the script in ~/bin and make it executable. Run manually at least once to test:

$ chmod 744 ~/bin/myRsync
$ ~/bin/myRsync

2. Automate

Cron is a daemon that runs programs at specified times. Use the command crontab -e to setup myRsync to auto-run with the following parameters (example):

# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

# Daily backup of $HOME to the netbook server
55 23 * * * /home/username/bin/myRsync >> /home/username/cron.log

Happy hacking!

More • sshnetworklinuxshellprogramming