Restored guide

Setting up a passwordless automated rsync backup

February 26, 2011 · 2 min read · Eric London's Drupal Blog

A nightly file backup should not pause for a password. This note shows a small, reviewable setup: one dedicated key, one rsync command, and one crontab entry.

What you are solving

The web tree and the files directory need a copy on another machine. Logging in by hand does not survive a weekend. Passwordless here means an SSH key with a forced command and a narrow source path, not a key that can open a shell as root on every host you own.

Prepare the key

On the machine that starts the backup, create a key used only for this job. Leave the comment set to the purpose so you can recognise it later in authorized_keys.

ssh-keygen -t ed25519 -f ~/.ssh/drupal_backup -C "drupal-files-backup"

Append the public key on the storage host. Restrict it to rsync if your SSH server supports a forced command. At minimum, pin the key to one user that cannot write outside the backup volume.

command="rsync --server -logDtprze.iLsfxC . /srv/drupal-files",no-port-forwarding,no-agent-forwarding,no-pty ssh-ed25519 AAAA... drupal-files-backup

The copy

Trailing slashes matter. The command below copies the contents of the files directory into the destination directory. --dry-run prints the plan without writing.

#!/bin/sh
set -eu
SRC=/var/www/drupal/sites/default/files
[email protected]:/srv/drupal-files
rsync -a --delete --dry-run -e "ssh -i $HOME/.ssh/drupal_backup" "$SRC/" "$DEST/"

Remove --dry-run only after the file list looks right. --delete makes the destination match the source, including removals, so point it at a backup volume and not at a second live docroot.

Schedule it

Run the script as the same user that owns the files, or as a user that can read them. A root crontab that happens to work is harder to audit.

15 2 * * * /usr/local/sbin/drupal-files-backup.sh >> /var/log/drupal-backup.log 2>&1

Check before you trust it

  • Restore one file from the destination onto a scratch directory and open it.
  • Confirm a deleted test file disappears on the next run, and that the source still has everything you meant to keep.
  • Rotate the log so a silent failure does not fill the disk.

Restored guide for the 26 February 2011 note. The steps are rewritten from ordinary SSH and rsync practice.