WordPress/Piwik backup script

Migrating to DigitalOcean has given me more flexibility with my applications, but I’ve lost a lot of the automated tasks that a host performs, including backups. As such, I’ve written a small Bash script to backup the following:

  • One or more WordPress databases
  • One or more Piwik databases
  • WordPress files
  • Piwik files
  • Nginx config files

The generate_backups.ksh script collects all the database information from config files and performs a mysqldump on the databases. The rest of the files are backed up using tar. The script is over at GitHub, but also pasted below (check GitHub first).

#!/bin/bash
#################################################################
#Ver.  Date        Name        Description                      #
#v0.1  2015-06-01  Marchione   Initial script                   #
#                                                               #
#                                                               #
#                                                               #
#################################################################

#Set variables
WP_FOLDER=/var/www
PIWIK_FOLDER=/var/www
BACKUP_FOLDER=/backup/backup

#Make backup directory if it doesn't exist
mkdir -p ${BACKUP_FOLDER}

#Enter loop to backup WordPress databases
#Look for wp-config.php files
for WP_CONFIG in `find $WP_FOLDER -name wp-config.php -print`; do
  #Get database information from files
  DB_NAME=$(grep -E "^define\('DB_NAME'" ${WP_CONFIG} | cut -d"'" -f4 | xargs)
  DB_USER=$(grep -E "^define\('DB_USER'" ${WP_CONFIG} | cut -d"'" -f4 | xargs)
  DB_PASSWORD=$(grep -E "^define\('DB_PASSWORD'" ${WP_CONFIG} | cut -d"'" -f4 | xargs)
  DB_HOST=$(grep -E "^define\('DB_HOST'" ${WP_CONFIG} | cut -d"'" -f4 | xargs)

  #Backup the databases
  mysqldump --opt ${DB_NAME} -u${DB_USER} -p${DB_PASSWORD} -h${DB_HOST} | gzip > ${BACKUP_FOLDER}/$(date +%Y%m%d_%H%M)_${DB_NAME}.gz;
done

#Enter loop to backup Piwik databases
#Look for config.ini.php files
for PIWIK_CONFIG in `find $PIWIK_FOLDER -name config.ini.php -print`; do
  #Get database information from files
  DB_NAME=$(grep -E "^dbname" ${PIWIK_CONFIG} | cut -d"=" -f2 | xargs)
  DB_USER=$(grep -E "^username" ${PIWIK_CONFIG} | cut -d"=" -f2 | xargs)
  DB_PASSWORD=$(grep -E "^password" ${PIWIK_CONFIG} | cut -d"=" -f2 | xargs)

  #Backup the databases
  #Piwik defaults to InnoDB, using --single-transaction so application isn't blocked
  mysqldump --opt --single-transaction ${DB_NAME} -u${DB_USER} -p${DB_PASSWORD} | gzip > ${BACKUP_FOLDER}/$(date +%Y%m%d_%H%M)_${DB_NAME}.gz;
done

#Backup WordPress files
tar -czf ${BACKUP_FOLDER}/$(date +%Y%m%d_%H%M)_wp_files.tar.gz  ${WP_FOLDER}/  >/dev/null  2>&1

#Backup Piwik files
#Since my WordPress/Piwik are in the same directory, I'm leaving this commented out
#tar -czf ${BACKUP_FOLDER}/$(date +%Y%m%d_%H%M)_piwik_files.tar.gz  ${PIWIK_FOLDER}/  >/dev/null  2>&1

#Backup Nginx config files
tar -czf ${BACKUP_FOLDER}/$(date +%Y%m%d_%H%M)_nginx.tar.gz  /etc/nginx/  >/dev/null 2>&1

#Remove backups older than 7 days
find ${BACKUP_FOLDER} -name "*.*" -type f -mtime +7 -exec rm {} \;

exit

The script runs out of root’s crontab at 5am every morning.

0 5 * * * /backup/generate_backups.ksh >/dev/null 2>&1

The download_backups.ksh script is run on your local machine and uses rsync over SSH to download the files from the server.

#!/bin/bash
#################################################################
#Ver.  Date        Name        Description                      #
#v0.1  2015-06-01  Marchione   Initial script                   #
#                                                               #
#                                                               #
#                                                               #
#################################################################

rsync -avzhe 'ssh -p ####' --progress username@hostname.com:/backup ~/Desktop

 

-Logan

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.