Created
August 19, 2018 16:25
-
-
Save praveen-palanisamy/4e2d7aecd5e1113d5242407e10125065 to your computer and use it in GitHub Desktop.
WordPress backup script: A bash script to compress and backup a complete wordpress site including the database
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
#!/usr/bin/env bash | |
# wp-backup-script.sh - Creates a complete, compressed backup of your WordPress database and files. You can then transfer it to your preferred location (local disk, cloud backup storage etc) | |
# Author: Praveen Palanisamy | Twitter: @PraveenPsamy | GitHub: https://github.com/praveen-palanisamy| Website: https://praveenp.com | |
# Dependencies: mailutils | |
# 0. Change the variables below to suit your environment | |
WP_FOLDER="$HOME/public_html/" # Folder where your wordpress root installation is | |
BACKUP_FOLDER="$HOME/backups" # Folder where you want to store the backups | |
EMAIL="[email protected]" # Email ID where you want to receive a confirmation email | |
DATE=$(date +%Y-%m-%d) | |
LOG="${BACKUP_FOLDER}/wp-backup.log" | |
echo "WordPress Backup Log ${DATE}" > ${LOG} | |
echo "" >> ${LOG} | |
if [ -z ${WP_FOLDER} ] || [ -z ${BACKUP_FOLDER} ]; then | |
echo "Cannot find ${WP_Folder} and/or ${BACKUP_FOLDER}" >> ${LOG} | |
exit 1 | |
fi | |
# 1. Check if WP_FOLDER is a valid wordpress installation | |
WP_CONFIG="${WP_FOLDER}/wp-config.php" | |
if ! test -f ${WP_CONFIG}; then | |
echo "ERROR: Cannot detect WordPress installation here... Exiting" >> ${LOG} | |
exit 1 | |
fi | |
# 2. Get the wordpress database information | |
DB_NAME=$(grep -E "^define\('DB_NAME'" ${WP_CONFIG} | cut -d"'" -f4) | |
DB_USER=$(grep -E "^define\('DB_USER'" ${WP_CONFIG} | cut -d"'" -f4) | |
DB_PASSWORD=$(grep -E "^define\('DB_PASSWORD'" ${WP_CONFIG} | cut -d"'" -f4) | |
DB_HOST=$(grep -E "^define\('DB_HOST'" ${WP_CONFIG} | cut -d"'" -f4) | |
# 3. Compress and backup the WordPress DB and the complete website files | |
mysqldump ${DB_NAME} -u${DB_USER} -p${DB_PASSWORD} -h${DB_HOST} | gzip > ${BACKUP_FOLDER}/wp_db-${DATE}.gz; | |
if [ $? -ne 0 ]; then | |
echo "ERROR: Couldn't dump your WordPress database. Check your wp-config credentials and permissions" >> ${LOG} | |
exit 1 | |
fi | |
tar -zcf ${BACKUP_FOLDER}/wp_files-${DATE}.tar.gz ${WP_FOLDER}/ >/dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
echo "ERROR: Couldn't backup your WordPress directory..." >> ${LOG} | |
exit 1 | |
fi | |
echo "Website Backup Completed Successfully" >> ${LOG} | |
echo "" >> ${LOG} | |
echo "WordPress site file list with size (depth=2): \n" >> ${LOG} | |
echo "$(du -h --max-depth 2 ${WP_FOLDER})" >> ${LOG} | |
echo "" >> ${LOG} | |
echo "Backup file information: \n" >> ${LOG} | |
echo "$(du -h ${BACKUP_FOLDER})" >> ${LOG} | |
echo "" >> ${LOG} | |
# email log | |
cat ${LOG} | mail -s "WordPress website Backup Successful" ${EMAIL} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment