96 lines
2.5 KiB
Bash
96 lines
2.5 KiB
Bash
#!/bin/bash
|
|
|
|
# Exit on error
|
|
set -e
|
|
|
|
# Validate input parameters
|
|
if [ "$#" -ne 2 ]; then
|
|
echo "Usage: $0 <RESTIC_PASSWORD> <CUSTOM_TAG>"
|
|
exit 1
|
|
fi
|
|
|
|
# Assign arguments to variables
|
|
RESTIC_PASSWORD="$1"
|
|
CUSTOM_TAG="$2"
|
|
|
|
# Configuration
|
|
APP_PATH='/var/www/webroot/ROOT'
|
|
BACKUP_PATH='/mnt/backups'
|
|
PASSWORD_FILE="/etc/restic-password"
|
|
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"
|
|
|
|
# Ensure log directory exists
|
|
mkdir -p "$LOG_DIR"
|
|
|
|
# Logging function
|
|
log() {
|
|
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
log "Starting Media Backup process with tags: media_themes, $CUSTOM_TAG."
|
|
|
|
# Verify password file exists
|
|
if [ ! -f "$PASSWORD_FILE" ]; then
|
|
log "ERROR: Password file not found at $PASSWORD_FILE."
|
|
exit 1
|
|
fi
|
|
|
|
# Export the password from the file to ensure Restic uses it automatically
|
|
export RESTIC_PASSWORD=$(cat "$PASSWORD_FILE")
|
|
|
|
# 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."
|
|
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
|
|
|
|
# Perform the backup
|
|
for path in "${INCLUDE_PATHS[@]}"; do
|
|
log "Starting backup for $path..."
|
|
if restic -r "$BACKUP_PATH" backup "$path" \
|
|
--tag media_themes \
|
|
--tag "$CUSTOM_TAG" \
|
|
--force \
|
|
--option b2.connections=4; then
|
|
log "Backup completed successfully for $path with tags: media_themes, $CUSTOM_TAG."
|
|
else
|
|
log "ERROR: Backup failed for $path."
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
# Release global lock
|
|
exec 9>&-
|
|
|
|
# End logging
|
|
log "Media Backup process finished successfully."
|