#!/bin/bash
# Define variables
DATE=$(date +'%Y.%m.%d')
ARCHIVE_NAME="/var/backups/server/${DATE}_sites.zip"
SOURCE_DIRS=(
"/var/www/site1"
"/var/www/site2"
)
# Create a temporary directory
TMP_DIR=$(mktemp -d)
# Exclude patterns
EXCLUDES=(
"--exclude=wp-admin"
"--exclude=wp-includes"
"--exclude=*cache*"
"--exclude=*backup*"
"--exclude=*temp*"
"--exclude=*tmp*"
"--exclude=*logs*"
"--exclude=node_modules"
"--exclude=.well-known"
)
# Copy files to the temporary directory while excluding specific folders
for SOURCE_DIR in "${SOURCE_DIRS[@]}"; do
# Get the base name of the source directory (to create a subdirectory)
DIR_NAME=$(basename "$SOURCE_DIR")
# Create a corresponding directory in the temporary directory
mkdir -p "$TMP_DIR/$DIR_NAME"
# Copy files to the corresponding subdirectory
timeout 15m rsync -av --prune-empty-dirs --max-size=256m "${EXCLUDES[@]}" "$SOURCE_DIR/" "$TMP_DIR/$DIR_NAME/"
done
# Change to the temporary directory to ensure zip archives contents directly
cd "$TMP_DIR" || exit
# Create the archive
zip -r "$ARCHIVE_NAME" ./*
# Check if the archive was created successfully
if [ -f "$ARCHIVE_NAME" ]; then
echo "Backup of sites completed successfully: $ARCHIVE_NAME"
else
echo "Failed to create the site backup archive."
fi
# Clean up
rm -rf "$TMP_DIR"