Skip to content

Instantly share code, notes, and snippets.

@SohaibBazaz
Last active July 10, 2024 15:41
Show Gist options
  • Save SohaibBazaz/a782bf6aa5dc3a89afaab09e4565c149 to your computer and use it in GitHub Desktop.
Save SohaibBazaz/a782bf6aa5dc3a89afaab09e4565c149 to your computer and use it in GitHub Desktop.
This is a straightforward script designed to automate Duplicity for backing up Docker volumes to an external hard drive. It includes a detailed log file for backup information and automatically deletes backups older than 20 days to manage storage efficiently.
#!/bin/bash
# Define backup destination (replace with your external drive path)
BACKUP_DEST="/path/to/backup_drive"
LOG_FILE="/path/to/backup.log"
# Clear log file
echo "Starting backup process at $(date)" > "$LOG_FILE"
echo "" >> "$LOG_FILE"
export PASSPHRASE="" # add GnuPG passphrase
# List of Docker volumes to back up
VOLUMES=("" "") # Add docker volume names here (as many as you want in seperate "")
for volume in "${VOLUMES[@]}"
do
echo "Backing up volume $volume..."
echo "$(date) - Backing up volume $volume..." >> "$LOG_FILE"
# Check if Docker volume exists and can be inspected
if docker volume inspect "$volume" >/dev/null 2>&1; then
echo "$(date) - Performing full backup for $volume." >> "$LOG_FILE"
# Perform full backup
duplicity full /var/lib/docker/volumes/"$volume"/_data file://"$BACKUP_DEST/$volume" >> "$LOG_FILE" 2>&1
if [ $? -eq 0 ]; then
echo "$(date) - Full backup of volume $volume completed successfully." >> "$LOG_FILE"
else
echo "$(date) - Full backup of volume $volume failed." >> "$LOG_FILE"
fi
# Delete backups older than 20 days
duplicity remove-all-but-n-full 20 --force "$BACKUP_DEST/$volume" >> "$LOG_FILE" 2>&1
if [ $? -eq 0 ]; then
echo "$(date) - Deleted old backups for volume $volume successfully." >> "$LOG_FILE"
else
echo "$(date) - Failed to delete old backups for volume $volume." >> "$LOG_FILE"
fi
else
echo "$(date) - Volume $volume does not exist or cannot be inspected." >> "$LOG_FILE"
echo "Volume $volume does not exist or cannot be inspected."
fi
done
unset PASSPHRASE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment