Backup to S3 with Restic ¶
Setup .restic.env ¶
Put this in your .restic.env file:
export AWS_ACCESS_KEY_ID="<access_key>"
export AWS_SECRET_ACCESS_KEY="<secret_key>"
export RESTIC_REPOSITORY="s3:https://s3.example.com"
export RESTIC_PASSWORD="<password>"
Update permissions:
chmod 600 ~/.restic.env
Create the backup script ¶
#!/usr/bin/env bash
set -euo pipefail
# Location of restic binary
RESTIC_BINARY=/home/private/restic
# Load credentials and repository settings
source /home/private/.restic.env
# Directories to back up (space-separated)
BACKUP_PATHS="/home/protected"
# Optional: path to a file containing exclude patterns (one per line)
EXCLUDE_FILE=""
# Initialize repository if it does not exist yet
if ! $RESTIC_BINARY snapshots > /dev/null 2>&1; then
echo "Initializing new Restic repository..."
$RESTIC_BINARY init
fi
# Run incremental backup
echo "Starting backup..."
if [[ -f "$EXCLUDE_FILE" ]]; then
$RESTIC_BINARY backup $BACKUP_PATHS --exclude-file="$EXCLUDE_FILE"
else
$RESTIC_BINARY backup $BACKUP_PATHS
fi
# Prune old snapshots (keep 7 daily, 4 weekly, 6 monthly)
echo "Applying retention policy..."
$RESTIC_BINARY forget \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6 \
--prune
# Periodically verify repository integrity (checks 5% of data)
echo "Checking repository integrity..."
$RESTIC_BINARY check --read-data-subset=5%
echo "Backup completed successfully."
Update permissions:
chmod +x restic-backup.sh
Run it ¶
./restic-backup.sh
Create a cron job ¶
Run every hour:
0 * * * * /home/private/restic-backup.sh >> /home/logs/restic-backup.log 2>&1