#!/bin/bash # backup_daily.sh - Daily backup script using restic to Backblaze B2 set -euo pipefail # Configuration export B2_ACCOUNT_ID="your_b2_account_id" export B2_ACCOUNT_KEY="your_b2_account_key" export RESTIC_REPOSITORY="b2:your-bucket-name:/backups" export RESTIC_PASSWORD="your_restic_password" # Backup targets BACKUP_DIRS=( "/var/lib/docker/volumes/homeassistant/_data" "/var/lib/docker/volumes/portainer/_data" "/var/lib/docker/volumes/nextcloud/_data" "/mnt/nas/models" ) # Logging LOG_FILE="/var/log/restic_backup.log" exec > >(tee -a "$LOG_FILE") 2>&1 echo "=== Restic Backup Started at $(date) ===" # Check if repository is initialized if ! restic snapshots &>/dev/null; then echo "Repository not initialized. Initializing..." restic init fi # Perform backup echo "Backing up directories: ${BACKUP_DIRS[*]}" restic backup "${BACKUP_DIRS[@]}" \ --tag homelab \ --verbose # Prune old backups (keep last 7 daily, 4 weekly, 12 monthly) echo "Pruning old backups..." restic forget \ --keep-daily 7 \ --keep-weekly 4 \ --keep-monthly 12 \ --prune # Check repository integrity (monthly) DAY_OF_MONTH=$(date +%d) if [ "$DAY_OF_MONTH" == "01" ]; then echo "Running repository check..." restic check fi echo "=== Restic Backup Completed at $(date) ==="