Skip to content

Instantly share code, notes, and snippets.

@Jontem
Last active July 5, 2017 14:16
Show Gist options
  • Save Jontem/51974d3f02bf98350cd400146ea22f97 to your computer and use it in GitHub Desktop.
Save Jontem/51974d3f02bf98350cd400146ea22f97 to your computer and use it in GitHub Desktop.
ZFS replication backup script from FreeNAS to Amazon s3
#!/bin/sh
# The first argument should be the name of the dataset to backup
if test -z $1; then
echo "Please specify a valid dataset"
exit 1
fi
# Passphrase file
passphrase_file=/root/snapshot-gpg-passphrase
# Local directory to syn with Amazon S3
bucket_dir=/root/s3_sync_bucket
# The S3 bucket url
s3_bucket="s3://your-bucket/"
# Generate a snapshot name
snapshot_name="$1@$(date +%Y-%m-%d)"
# Convert to valid filename
filename=$(echo $snapshot_name | sed "s/\//-/g").gpg
echo "Using bucket_dir: $bucket_dir"
echo "Using s3 bucket: $s3_bucket"
echo "Using snapshot_name: $snapshot_name"
echo "Using filename: $filename"
# Create the snapshot, send it to the jail, encrypt it
zfs snapshot $snapshot_name
zfs send -v $snapshot_name | jexec backup gpg --batch --symmetric --cipher-algo AES256 --passphrase-file $passphrase_file --output $bucket_dir/$filename
zfs destroy $snapshot_name
# Remove old snapshots
search_word=$(echo $filename | sed "s/@.*$//g")
echo "Searching for \"$search_word\" in $bucket_dir older than 7 days"
jexec backup find $bucket_dir -name $search_word* -mtime +7 -exec rm {} \;
# Sync to s3
jexec backup s3cmd sync --delete-removed $bucket_dir $s3_bucket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment