Last active
January 1, 2026 19:50
-
-
Save salvatorecapolupo/c6889a72b37c396ceca01f938cc2c140 to your computer and use it in GitHub Desktop.
A single Bash script to backup a WordPress site. It automatically extracts MySQL credentials from wp-config.php, creates a full database dump optimized for large databases, and archives the entire WordPress directory into a compressed file. Ready to run via SSH with minimal configuration.
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 | |
| # ===== CONFIGURAZIONE BASE ===== | |
| WP_PATH="/var/www/html" | |
| BACKUP_DIR="$WP_PATH" | |
| TIMESTAMP=$(date +"%Y%m%d_%H%M%S") | |
| BACKUP_NAME="wordpress_backup_$TIMESTAMP" | |
| DUMP_FILE="$BACKUP_DIR/${BACKUP_NAME}.sql" | |
| ARCHIVE_FILE="$BACKUP_DIR/${BACKUP_NAME}.tar.gz" | |
| # ===== CONTROLLI ===== | |
| if [ ! -f "$WP_PATH/wp-config.php" ]; then | |
| echo "ERRORE: wp-config.php non trovato in $WP_PATH" | |
| exit 1 | |
| fi | |
| # ===== ESTRAZIONE CREDENZIALI DB ===== | |
| DB_NAME=$(grep "DB_NAME" "$WP_PATH/wp-config.php" | awk -F"'" '{print $4}') | |
| DB_USER=$(grep "DB_USER" "$WP_PATH/wp-config.php" | awk -F"'" '{print $4}') | |
| DB_PASSWORD=$(grep "DB_PASSWORD" "$WP_PATH/wp-config.php" | awk -F"'" '{print $4}') | |
| DB_HOST=$(grep "DB_HOST" "$WP_PATH/wp-config.php" | awk -F"'" '{print $4}') | |
| if [[ -z "$DB_NAME" || -z "$DB_USER" || -z "$DB_PASSWORD" || -z "$DB_HOST" ]]; then | |
| echo "ERRORE: impossibile estrarre correttamente le credenziali del database" | |
| exit 1 | |
| fi | |
| echo "Credenziali DB trovate:" | |
| echo " DB_NAME=$DB_NAME" | |
| echo " DB_USER=$DB_USER" | |
| echo " DB_HOST=$DB_HOST" | |
| # ===== DUMP DATABASE (OTTIMIZZATO PER DB GRANDI) ===== | |
| echo "Avvio dump del database..." | |
| mysqldump \ | |
| --user="$DB_USER" \ | |
| --password="$DB_PASSWORD" \ | |
| --host="$DB_HOST" \ | |
| --single-transaction \ | |
| --quick \ | |
| --routines \ | |
| --triggers \ | |
| --events \ | |
| --default-character-set=utf8mb4 \ | |
| "$DB_NAME" > "$DUMP_FILE" | |
| if [ $? -ne 0 ]; then | |
| echo "ERRORE durante il dump del database" | |
| exit 1 | |
| fi | |
| echo "Dump database completato: $DUMP_FILE" | |
| echo "Backup database WordPress completato correttamente." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment