Created
March 10, 2025 23:42
-
-
Save emiliodallatorre/58f2ced0718ab8a25f72459775124b77 to your computer and use it in GitHub Desktop.
Backup a WordPress server with TAR and WP CLI
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 | |
# Configuration | |
WEBSITE_PATH="/var/www/jeitaly.org" | |
BACKUP_DIR="$HOME/backup" | |
TIMESTAMP=$(date +"%Y%m%d_%H%M%S") | |
BACKUP_FOLDER="$BACKUP_DIR/$TIMESTAMP" | |
DB_DUMP_FILE="jeitaly_db_$TIMESTAMP.sql" | |
WEBSITE_ARCHIVE="jeitaly_website_$TIMESTAMP.tar.gz" | |
# Ensure backup directory exists | |
mkdir -p "$BACKUP_FOLDER" | |
# Archive website files | |
echo "Archiving website files..." | |
tar -czvf "$BACKUP_FOLDER/$WEBSITE_ARCHIVE" -C "$WEBSITE_PATH" . | |
# Dump WordPress database | |
echo "Dumping WordPress database..." | |
if command -v wp &> /dev/null; then | |
wp db export "$BACKUP_FOLDER/$DB_DUMP_FILE" --path="$WEBSITE_PATH" | |
else | |
echo "wp-cli not found. Please install wp-cli." | |
exit 1 | |
fi | |
# Verify the files were created | |
if [ -f "$BACKUP_FOLDER/$WEBSITE_ARCHIVE" ] && [ -f "$BACKUP_FOLDER/$DB_DUMP_FILE" ]; then | |
echo "Backup completed successfully." | |
echo "Website archive: $BACKUP_FOLDER/$WEBSITE_ARCHIVE" | |
echo "Database dump: $BACKUP_FOLDER/$DB_DUMP_FILE" | |
else | |
echo "Backup failed. Check the logs." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment