Skip to content

Instantly share code, notes, and snippets.

@WhaleJ84
Created December 1, 2021 22:03
Show Gist options
  • Select an option

  • Save WhaleJ84/3351d8793813b87ec513177aacac92dd to your computer and use it in GitHub Desktop.

Select an option

Save WhaleJ84/3351d8793813b87ec513177aacac92dd to your computer and use it in GitHub Desktop.
A shell script to automatically backup persistent Netbox data, encrypt them with a recipients GPG key and email the files with a SHA256 hash of the attachments.
#!/bin/sh
# #
# A modified backup script based off the official documentation #
# https://netbox.readthedocs.io/en/stable/ #
# #
# Consider adding this to your crontab with something like: #
# 0 18 * * 0 /path/to/netbox_backup.sh email@example.com #
# #
set -e
[ ! "$1" ] && echo 'No email entered' && exit 1
EMAIL="$1"
DB='netbox.sql'
MEDIA='netbox_media.tar.gz'
cd /opt/netbox
# Create backups
sudo -u postgres pg_dump netbox > "$DB"
tar -czf "$MEDIA" netbox/media/
# Encrypt files
gpg --encrypt --armor -r "$EMAIL" --yes "$DB"
gpg --encrypt --armor -r "$EMAIL" --yes "$MEDIA"
# Get hashes
hashes=$(sha256sum "$DB" "$MEDIA")
# Send email
echo "$hashes" | mail -A "$DB".asc -A "$MEDIA".asc -s "$(date) Netbox backup" "$EMAIL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment