#!/bin/bash

export RESTIC_PASSWORD='Shan33779488@@'
export RESTIC_REPOSITORY='/mnt/raid1/backup/restic'

LOG_FILE="/var/log/restic_backup.log"
BACKUP_DATE=$(date +%F)

# Safety check
if ! mountpoint -q /mnt/raid1; then
    echo "$(date): RAID not mounted. Aborting." >> "$LOG_FILE"
    exit 1
fi

# Init repo if not exists
if [ ! -d "$RESTIC_REPOSITORY" ]; then
    restic init >> "$LOG_FILE" 2>&1
fi

{
    echo "$(date): Starting Restic backup..."
    restic backup /var/lib/nextcloud /var/lib/docker --tag "$BACKUP_DATE" --verbose

    echo "$(date): Forgetting old backups..."
    restic forget --keep-last 1 --prune

    echo "$(date): Checking repo integrity..."
    restic check

    echo "$(date): Backup complete."
} >> "$LOG_FILE" 2>&1
