Docs / Automating backups with cron
Automating backups with cron
Run unattended incremental sends on a schedule with a small wrapper script and a cron entry.
1. Use a dedicated SSH key
For unattended runs, generate a passphrase-less key used only for this
backup, add its public half to your conduit, and reference it explicitly
with
ssh -i. Keep the private key readable
only by the user that runs the cron job.
2. A snapshot-and-send script
Save this as
/usr/local/bin/conduit-backup.sh
and make it executable. It snapshots, sends the delta since the last run,
and records the new base.
#!/bin/sh
set -eu
DATASET=tank/data
REMOTE="[email protected]"
PORT=2201
DEST=conduits/sub_abc123/data
KEY=$HOME/.ssh/conduit_backup
STATE=/var/lib/conduit-backup/last_snap
NEW="${DATASET}@$(date +%Y-%m-%dT%H%M%S)"
zfs snapshot -r "$NEW"
SSH="ssh -i $KEY -p $PORT $REMOTE"
# First run: full send. Later runs: incremental from the recorded base.
if [ -f "$STATE" ]; then
BASE="$(cat "$STATE")"
zfs send -RwI "$BASE" "$NEW" | $SSH zfs recv -F "$DEST"
else
zfs send -Rw "$NEW" | $SSH zfs recv -F "$DEST"
fi
echo "$NEW" > "$STATE"
set -eu aborts on any error so a failed
send won't advance the recorded base. Adjust the variables at the top for
your dataset and conduit.
3. Schedule it
Add a crontab entry (
crontab -e) to run it
nightly at 02:00 and log the output:
0 2 * * *
/usr/local/bin/conduit-backup.sh
>> /var/log/conduit-backup.log 2>&1
Consider wrapping the script with
flock to
prevent overlapping runs, and alert yourself if the log shows failures.