Skip to content

Instantly share code, notes, and snippets.

@fintanmm
Last active December 9, 2024 15:06
Show Gist options
  • Select an option

  • Save fintanmm/48918f794ced721c5bcd10120af36d0d to your computer and use it in GitHub Desktop.

Select an option

Save fintanmm/48918f794ced721c5bcd10120af36d0d to your computer and use it in GitHub Desktop.
simple script to download/restore a backup of a WP site
#!/bin/bash
# Variables
BITNAMI_DIR="/home/bitnami"
STACK_DIR="$BITNAMI_DIR/stack/wordpress/wp-content"
DB_PASSWORD_FILE="$BITNAMI_DIR/bitnami_application_password"
DB_NAME="bitnami_wordpress"
USER=""
PASSWORD=""
# Check if required arguments are provided and get optional user argument
while getopts ":u:" opt; do
case ${opt} in
u )
USER=$OPTARG
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit 1
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ -z "$PASSWORD" ]; then
echo -n "Password: "
read -sr PASSWORD
echo
fi
URL=$1
ZIP_FILENAME=$(basename "$URL" | cut -d? -f1)
# Function to check if apt update/upgrade was done recently
is_update_needed() {
# Define threshold for "recently" (e.g., 1 day)
local threshold=$((24 * 60 * 60)) # 24 hours in seconds
# Get timestamp of last update and current time
local last_update=$(stat -c %Y /var/lib/apt/periodic/update-stamp)
local current_time=$(date +%s)
# Compare timestamps
local time_diff=$((current_time - last_update))
# Return true if update is needed (not done recently)
((time_diff > threshold))
}
# Check if commands exist
commands=("wget" "unzip" "mysql" "mysqldump" "wp")
for cmd in "${commands[@]}"; do
if ! command -v $cmd &> /dev/null; then
# Check if apt update is needed
if is_update_needed; then
# Update system packages
echo "Performing apt update..."
sudo apt update && sudo apt upgrade -y
else
echo "Skipping apt update (recently done)."
fi
# Install necessary packages
sudo apt install -y unzip wget rsync apparmor-profiles apparmor-profiles-extra apparmor-utils
fi
done
# Error handling function
handle_error() {
status=$?
if [ $status -ne 0 ]; then
echo "Error: Script failed at line $1"
exit $status
fi
}
# Trap to ensure cleanup
trap 'rm -rf "$TEMP_DIR"' EXIT
# Download and unzip WordPress files
cd $BITNAMI_DIR || handle_error $LINENO
if [ -n "$PASSWORD" ]; then
curl --user "user:$PASSWORD" "$URL" --output "$ZIP_FILENAME" || handle_error $LINENO
else
curl "$URL" --output "$ZIP_FILENAME" || handle_error $LINENO
fi
# Create a temporary directory for unzipping
TEMP_DIR=$(mktemp -d)
unzip -q "$ZIP_FILENAME" -d "$TEMP_DIR" || handle_error $LINENO
# Move the necessary files to the appropriate locations
sudo rsync -av "$TEMP_DIR/bitnami/wordpress/wp-content/" "$STACK_DIR/" || handle_error $LINENO
# Set appropriate permissions
sudo find $STACK_DIR -type f -exec chmod 664 {} \; || handle_error $LINENO
sudo find $STACK_DIR -type d -exec chmod 775 {} \; || handle_error $LINENO
sudo find $STACK_DIR -type d -exec chown -R daemon:daemon {} \; || handle_error $LINENO
sudo find $STACK_DIR -type f -exec chown -R bitnami:daemon {} \; || handle_error $LINENO
# Find the SQL file
SQL_FILE=$(find "$TEMP_DIR" -name "*.sql" | head -n 1)
if [ -z "$SQL_FILE" ]; then
echo "Error: No SQL file found in the archive."
exit 1
fi
# Move the SQL file to the appropriate location
sudo rsync -av "$SQL_FILE" "$STACK_DIR/mwp_db/" || handle_error $LINENO
# Import database
mariadb -u root --password="$(< $DB_PASSWORD_FILE)" $DB_NAME < "$SQL_FILE" || handle_error $LINENO
# Restart services
sudo /opt/bitnami/ctlscript.sh restart || handle_error $LINENO
# Update WordPress core, plugins, and themes
sudo wp core update && sudo wp core update-db || handle_error $LINENO
sudo wp plugin update --all || handle_error $LINENO
sudo wp theme update --all || handle_error $LINENO
# Configure AppArmor
sudo wget -O /etc/apparmor.d/local/wordpress https://gist.githubusercontent.com/fintanmm/e50d8ad63a94137a73ab2bf558beef4a/raw/3facf8fa5d7c13f1332bce773d3984c98a08439b/wordpress || handle_error $LINENO
# Clean up
rm -rf "${STACK_DIR:?}/mwp_db/bitnami_wordpress.sql" "${BITNAMI_DIR:?}/${ZIP_FILENAME:?}" || handle_error $LINENO
# View MySQL logs
sudo grep "ERROR" /opt/bitnami/mariadb/logs/mysqld.log || handle_error $LINENO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment