HEX
Server: LiteSpeed
System: Linux CentOS-79-64-minimal 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: vishn3436 (5293)
PHP: 8.0.15
Disabled: NONE
Upload Files
File: //scripts/backup_all.sh
#!/bin/bash
# =====================================================================
# Restic Backup Script: One repo per folder/file, nested under server name
# Path: /scripts/backup_all.sh
# =====================================================================

# --- Configuration ---
SERVER_NAME="5.9.111.147-Server"   # ๐Ÿ‘ˆ change this per server
WASABI_ACCESS_KEY="OZOEE33UABEMOJGPFCKW"
WASABI_SECRET_KEY="kO74bgpngFF7y2zjFQFfEjLkrsahpqAzcgo0ggju"
WASABI_REGION="eu-central-2"
WASABI_BUCKET="karmanye-5.9.111.147"
WASABI_ENDPOINT="https://s3.eu-central-2.wasabisys.com"
RESTIC_PASSWORD="SOs#708%Stjs"

BACKUP_LIST="/scripts/backup-dir.list"
LOG_DIR="/scripts/logs"
RETENTION_SNAPSHOTS=21   # keep last 21 snapshots

# --- Export environment variables for Restic ---
export AWS_ACCESS_KEY_ID=$WASABI_ACCESS_KEY
export AWS_SECRET_ACCESS_KEY=$WASABI_SECRET_KEY
export AWS_DEFAULT_REGION=$WASABI_REGION
export RESTIC_PASSWORD=$RESTIC_PASSWORD

# --- Setup log directory ---
mkdir -p "$LOG_DIR"
TIMESTAMP=$(date +'%Y-%m-%d-%H-%M-%S')
LOG_FILE="$LOG_DIR/restic_backup_$TIMESTAMP.log"

# --- Logging header ---
echo "============================================================" | tee -a "$LOG_FILE"
echo "๐Ÿ•’ Backup Started: $(date)" | tee -a "$LOG_FILE"
echo "============================================================" | tee -a "$LOG_FILE"

# --- Read folder/file list ---
if [[ ! -f "$BACKUP_LIST" ]]; then
    echo "โŒ Backup list not found at $BACKUP_LIST" | tee -a "$LOG_FILE"
    exit 1
fi

items=()
while IFS= read -r item; do
    [[ -z "$item" || "$item" =~ ^# ]] && continue
    items+=("$item")
done < "$BACKUP_LIST"

# --- Function: Initialize repo ---
initialize_repo() {
    local item_path="$1"
    local relative_path=$(echo "$item_path" | sed 's#^/##')
    #local repo="s3:$WASABI_ENDPOINT/$WASABI_BUCKET/$SERVER_NAME/$relative_path"
    local repo="s3:$WASABI_ENDPOINT/$WASABI_BUCKET/$relative_path"

    echo "๐Ÿ” Checking Restic repo for $relative_path..." | tee -a "$LOG_FILE"
    if ! restic -r "$repo" snapshots > /dev/null 2>&1; then
        echo "โš™๏ธ Repo not found. Initializing $relative_path..." | tee -a "$LOG_FILE"
        restic -r "$repo" init 2>&1 | tee -a "$LOG_FILE"
        if [[ $? -ne 0 ]]; then
            echo "โŒ Failed to initialize repo for $relative_path" | tee -a "$LOG_FILE"
            return 1
        fi
    else
        echo "โœ… Repo already exists for $relative_path" | tee -a "$LOG_FILE"
    fi
}

# --- Function: Backup folder/file ---
backup_item() {
    local item_path="$1"
    local relative_path=$(echo "$item_path" | sed 's#^/##')
    #local repo="s3:$WASABI_ENDPOINT/$WASABI_BUCKET/$SERVER_NAME/$relative_path"
    local repo="s3:$WASABI_ENDPOINT/$WASABI_BUCKET/$relative_path"

    if [[ -e "$item_path" ]]; then
        echo "------------------------------------------------------------" | tee -a "$LOG_FILE"
        if [[ -f "$item_path" ]]; then
            echo "๐Ÿ“„ Backing up file: $item_path -> $repo" | tee -a "$LOG_FILE"
        elif [[ -d "$item_path" ]]; then
            echo "๐Ÿ“ Backing up folder: $item_path -> $repo" | tee -a "$LOG_FILE"
        fi

        restic -r "$repo" backup "$item_path" --tag "$SERVER_NAME-$relative_path" 2>&1 | tee -a "$LOG_FILE"
        if [[ $? -eq 0 ]]; then
            echo "โœ… Backup successful for: $item_path" | tee -a "$LOG_FILE"
        else
            echo "โŒ Backup failed for: $item_path" | tee -a "$LOG_FILE"
        fi

        echo "๐Ÿงน Applying retention policy for: $relative_path" | tee -a "$LOG_FILE"
        restic -r "$repo" forget --keep-last $RETENTION_SNAPSHOTS --prune 2>&1 | tee -a "$LOG_FILE"
    else
        echo "โš ๏ธ Skipping: Path not found - $item_path" | tee -a "$LOG_FILE"
    fi
}

# --- Main Backup Loop ---
for item in "${items[@]}"; do
    initialize_repo "$item"
    backup_item "$item"
done

# --- Clean old logs (older than 7 days) ---
find "$LOG_DIR" -type f -name "*.log" -mtime +7 -delete

# --- Footer ---
echo "============================================================" | tee -a "$LOG_FILE"
echo "โœ… Backup Completed: $(date)" | tee -a "$LOG_FILE"
echo "============================================================" | tee -a "$LOG_FILE"

exit 0