Last active
February 26, 2024 12:08
-
-
Save fintanmm/48918f794ced721c5bcd10120af36d0d to your computer and use it in GitHub Desktop.
simple script to download a backup of wp site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 [ $# -ne 1 ]; then | |
echo "Usage: $0 [-u user] <url>" | |
exit 1 | |
fi | |
if [ -n "$USER" ]; 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 | |
} | |
# Download and unzip WordPress files | |
cd $BITNAMI_DIR || handle_error $LINENO | |
if [ -n "$USER" ]; then | |
curl --user "$USER:$PASSWORD" "$URL" --output "$ZIP_FILENAME" || handle_error $LINENO | |
else | |
curl "$URL" --output "$ZIP_FILENAME" || handle_error $LINENO | |
fi | |
unzip -q "$ZIP_FILENAME" "bitnami/wordpress/wp-content/*" "mwp_db/bitnami_wordpress.sql" -d $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 chown -R bitnami:daemon $STACK_DIR || handle_error $LINENO | |
# Import database | |
mysql -u root --password="$(< $DB_PASSWORD_FILE)" $DB_NAME < "$STACK_DIR/mwp_db/bitnami_wordpress.sql" || handle_error $LINENO | |
# Restart services | |
sudo /opt/bitnami/ctlscript.sh restart || handle_error $LINENO | |
# View MySQL logs | |
sudo grep "ERROR" /stack/mariadb/logs/mysqld.log || 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment