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

96 lines
2.5 KiB
Bash
Raw Permalink 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'
BACKUP_PATH='/mnt/backups'
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"
INCLUDE_PATHS=("$APP_PATH/wp-content/uploads")
LOCK_FILE="/tmp/restic_global.lock"
2024-09-18 16:53:20 +00:00
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Logging function
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
2025-01-07 14:31:03 +00:00
log "Starting Media Backup process with tags: media_themes, $CUSTOM_TAG."
2024-11-12 18:15:11 +00:00
2025-01-06 17:22:25 +00:00
# Verify password file exists
if [ ! -f "$PASSWORD_FILE" ]; then
log "ERROR: Password file not found at $PASSWORD_FILE."
2024-11-12 18:15:11 +00:00
exit 1
fi
# Export the password from the file to ensure Restic uses it automatically
export RESTIC_PASSWORD=$(cat "$PASSWORD_FILE")
2025-01-06 17:22:25 +00:00
# Verify repository access
if ! restic -r "$BACKUP_PATH" snapshots > /dev/null 2>&1; then
log "ERROR: Unable to access the Restic repository. Aborting backup."
exit 1
fi
# Acquire a global lock to serialize Restic operations
log "Acquiring global lock for Restic operations..."
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
log "Another Restic operation is running. Exiting."
2024-11-12 18:15:11 +00:00
exit 1
fi
log "Global lock acquired."
# Check and remove stale locks
log "Checking for stale locks in the repository..."
if restic -r "$BACKUP_PATH" list locks | grep -q "lock"; then
log "Stale locks detected. Unlocking the repository..."
restic -r "$BACKUP_PATH" unlock
log "Repository unlocked successfully."
else
log "No stale locks found."
fi
# Check and fix permissions on /mnt/backups
if [ ! -w "$BACKUP_PATH/locks" ]; then
log "Fixing permissions on $BACKUP_PATH/locks"
sudo chown -R litespeed:litespeed "$BACKUP_PATH/locks"
sudo chmod -R u+rw "$BACKUP_PATH/locks"
fi
2024-11-12 18:15:11 +00:00
2025-01-06 17:22:25 +00:00
# Perform the backup
for path in "${INCLUDE_PATHS[@]}"; do
log "Starting backup for $path..."
if restic -r "$BACKUP_PATH" backup "$path" \
2025-01-06 17:22:25 +00:00
--tag media_themes \
--tag "$CUSTOM_TAG" \
--force \
--option b2.connections=4; then
log "Backup completed successfully for $path with tags: media_themes, $CUSTOM_TAG."
2024-09-18 16:53:20 +00:00
else
log "ERROR: Backup failed for $path."
2024-11-12 18:15:11 +00:00
exit 1
2024-09-18 16:53:20 +00:00
fi
done
# Release global lock
exec 9>&-
2025-01-06 17:22:25 +00:00
# End logging
log "Media Backup process finished successfully."