mb-backup-manager/scripts/imports/backup_media.sh

69 lines
2.0 KiB
Bash
Raw Normal View History

2024-09-18 16:53:20 +00:00
#!/bin/bash
2025-01-06 17:22:25 +00:00
# Exit on error
set -e
2024-11-12 18:15:11 +00:00
# Validate input parameters
2024-09-18 16:53:20 +00:00
if [ "$#" -ne 2 ]; then
2025-01-06 17:22:25 +00:00
echo "Usage: $0 <RESTIC_PASSWORD> <CUSTOM_TAG>"
2024-09-18 16:53:20 +00:00
exit 1
fi
2025-01-06 17:22:25 +00:00
# Assign arguments to variables
2024-09-18 16:53:20 +00:00
RESTIC_PASSWORD="$1"
2025-01-06 17:22:25 +00:00
CUSTOM_TAG="$2"
2024-09-18 16:53:20 +00:00
# Configuration
APP_PATH='/var/www/webroot/ROOT'
backupPath='/mnt/backups'
2024-11-12 18:15:11 +00:00
password_file="/etc/restic-password"
2024-09-18 16:53:20 +00:00
LOG_DIR="/home/jelastic/mb-backups/logs/manual/media"
LOG_FILE="${LOG_DIR}/backup_media_$(date +'%Y-%m-%d').log"
includePaths=("$APP_PATH/wp-content/uploads")
# Ensure log directory exists
mkdir -p "$LOG_DIR"
2025-01-07 14:31:03 +00:00
# Check and fix permissions on /mnt/backups
if [ ! -w "$backupPath/locks" ]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Fixing permissions on $backupPath/locks" | tee -a "$LOG_FILE"
sudo chown -R litespeed:litespeed "$backupPath/locks"
sudo chmod -R u+rw "$backupPath/locks"
fi
2024-09-18 16:53:20 +00:00
# Set Restic environment variables
export RESTIC_REPOSITORY="$backupPath"
export RESTIC_PASSWORD
2025-01-06 17:22:25 +00:00
# Start logging
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Media Backup with tags: media_themes, $CUSTOM_TAG" | tee -a "$LOG_FILE"
2024-11-12 18:15:11 +00:00
2025-01-06 17:22:25 +00:00
# Verify password file exists
2024-11-12 18:15:11 +00:00
if [ ! -f "$password_file" ]; then
echo "ERROR: Password file not found at $password_file" | tee -a "$LOG_FILE"
exit 1
fi
2025-01-06 17:22:25 +00:00
# Verify repository access
if ! restic snapshots > /dev/null 2>&1; then
echo "ERROR: Unable to access the restic repository. Aborting backup." | tee -a "$LOG_FILE"
2024-11-12 18:15:11 +00:00
exit 1
fi
2025-01-06 17:22:25 +00:00
# Perform the backup
2024-09-18 16:53:20 +00:00
for path in "${includePaths[@]}"; do
2025-01-06 17:22:25 +00:00
if restic backup "$path" \
--tag media_themes \
--tag "$CUSTOM_TAG" \
--force \
--option b2.connections=4; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup completed successfully for $path with tags: media_themes, $CUSTOM_TAG." | tee -a "$LOG_FILE"
2024-09-18 16:53:20 +00:00
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Backup failed for $path." | tee -a "$LOG_FILE"
2024-11-12 18:15:11 +00:00
exit 1
2024-09-18 16:53:20 +00:00
fi
done
2025-01-06 17:22:25 +00:00
# End logging
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Media Backup process finished." | tee -a "$LOG_FILE"