Last active
August 29, 2025 13:47
-
-
Save calvez/f2ee8171472db5d035f1b50f9619e7d3 to your computer and use it in GitHub Desktop.
Simple script what iterates thru runcloud wordpress instances, saves the plugin/theme versions, create db backup and update core, themes, plugins, languages. Feel free to use.
This file contains hidden or 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 | |
# WordPress Backup & Update Script | |
# Runs backups and updates for all WP instances under /home/*/webapps/* | |
# Intended for cron: Friday midnight | |
BACKUP_RETENTION_DAYS=60 | |
HOME_DIR="/home" | |
for USER_DIR in "$HOME_DIR"/*; do | |
USER=$(basename "$USER_DIR") | |
WEBAPPS_DIR="$USER_DIR/webapps" | |
[ -d "$WEBAPPS_DIR" ] || continue | |
for SITE_DIR in "$WEBAPPS_DIR"/*; do | |
[ -f "$SITE_DIR/wp-config.php" ] || continue | |
WP_PATH="$SITE_DIR" | |
OWNER=$(stat -c '%U' "$WP_PATH") | |
BACKUP_DIR="$USER_DIR/$(basename "$SITE_DIR")-backups" | |
mkdir -p "$BACKUP_DIR" | |
chown "$OWNER":"$OWNER" "$BACKUP_DIR" | |
DATE=$(date +%Y-%m-%d_%H-%M-%S) | |
DB_NAME=$(grep DB_NAME "$WP_PATH/wp-config.php" | cut -d "'" -f 4) | |
DB_USER=$(grep DB_USER "$WP_PATH/wp-config.php" | cut -d "'" -f 4) | |
DB_PASS=$(grep DB_PASSWORD "$WP_PATH/wp-config.php" | cut -d "'" -f 4) | |
DB_HOST=$(grep DB_HOST "$WP_PATH/wp-config.php" | cut -d "'" -f 4) | |
BACKUP_FILE="$BACKUP_DIR/${DATE}_${DB_NAME}.sql.gz" | |
SITE_NAME=$(basename "$SITE_DIR") | |
echo "[INFO] Updating site: $USER/$SITE_NAME" | |
echo "[INFO] Creating backup: $BACKUP_FILE" | |
# Export DB as root (mariadb-dump does not require site owner) | |
mariadb-dump -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" | gzip > "$BACKUP_FILE" | |
chown "$OWNER":"$OWNER" "$BACKUP_FILE" | |
if [ $? -eq 0 ]; then | |
echo "[SUCCESS] Backup created: $BACKUP_FILE" | |
else | |
echo "[ERROR] Backup failed for $USER/$SITE_NAME" | |
fi | |
# Backup plugin and theme lists (with version numbers) before update | |
PLUGIN_LIST_FILE="$BACKUP_DIR/${DATE}_${DB_NAME}_plugins.txt" | |
THEME_LIST_FILE="$BACKUP_DIR/${DATE}_${DB_NAME}_themes.txt" | |
sudo -u "$OWNER" bash -c "cd '$WP_PATH' && wp plugin list --format=table > '$PLUGIN_LIST_FILE'" | |
sudo -u "$OWNER" bash -c "cd '$WP_PATH' && wp theme list --format=table > '$THEME_LIST_FILE'" | |
chown "$OWNER":"$OWNER" "$PLUGIN_LIST_FILE" "$THEME_LIST_FILE" | |
echo "[INFO] Plugin list saved: $PLUGIN_LIST_FILE" | |
echo "[INFO] Theme list saved: $THEME_LIST_FILE" | |
# Remove old backups | |
find "$BACKUP_DIR" -type f -mtime +$BACKUP_RETENTION_DAYS -delete | |
# Run WP-CLI updates as site owner | |
sudo -u "$OWNER" bash -c "cd '$WP_PATH' && wp core update && wp plugin update --all && wp theme update --all && wp language core update && wp language plugin update --all && wp language theme update --all" | |
if [ $? -eq 0 ]; then | |
echo "[SUCCESS] Updated site: $USER/$SITE_NAME" | |
else | |
echo "[ERROR] Update failed for $USER/$SITE_NAME" | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment