Skip to content

Instantly share code, notes, and snippets.

@eze-kiel
Created March 3, 2021 19:03
Show Gist options
  • Save eze-kiel/8838437b547df55cd4ed8e3149900f84 to your computer and use it in GitHub Desktop.
Save eze-kiel/8838437b547df55cd4ed8e3149900f84 to your computer and use it in GitHub Desktop.
Script to automate backups to BackBlaze B2 bucket
#!/bin/bash
YELLOW='\033[1;33m'
NOCOLOR='\033[0m'
# Backblaze B2 configuration variables
B2_ACCOUNT="ACCOUNT"
B2_KEY="KEY"
B2_BUCKET="BUCKET-NAME"
# Local directory to backup
LOCAL_DIR="/home/user/Documents"
# Log file
LOG_FILE="/var/log/backups.log"
# Threshold to remove files
OLDER_THAN="30D"
export PASSPHRASE="GPG-PASS"
notif_tag() {
echo -e "${YELLOW}[?]${NOCOLOR} $1"
}
log() {
date=$(date --rfc-3339=s)
echo "[$date] - bucket: $B2_BUCKET - status: $1" >> $LOG_FILE
}
notif_tag "Removing files older than: $OLDER_THAN"
# Remove files older than 90 days
duplicity \
remove-older-than ${OLDER_THAN} --force \
b2://${B2_ACCOUNT}:${B2_KEY}@${B2_BUCKET}
notif_tag "Performing backup to bucket: $B2_BUCKET"
# Perform the backup
duplicity ${LOCAL_DIR} \
b2://${B2_ACCOUNT}:${B2_KEY}@${B2_BUCKET}
if [ $? -eq 0 ]; then
notify-send "Backup succeed on $B2_BUCKET"
log "succeed"
else
notify-send -u critical "Backup failed on $B2_BUCKET"
log "failed"
fi
notif_tag "Cleaning failures"
# Cleanup failures
duplicity \
cleanup --force \
b2://${B2_ACCOUNT}:${B2_KEY}@${B2_BUCKET}
# Show collection-status
duplicity collection-status \
b2://${B2_ACCOUNT}:${B2_KEY}@${B2_BUCKET}
unset YELLOW
unset NOCOLOR
unset LOG_FILE
unset OLDER_THAN
unset B2_ACCOUNT
unset B2_KEY
unset B2_BUCKET
unset LOCAL_DIR
unset PASSPHRASE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment