Skip to content

Instantly share code, notes, and snippets.

@pelmered
Last active August 29, 2015 14:26
Show Gist options
  • Save pelmered/3931c6f1c516f9940e8c to your computer and use it in GitHub Desktop.
Save pelmered/3931c6f1c516f9940e8c to your computer and use it in GitHub Desktop.
WP-CLI snippets and scripts
# Only database backup
declare -a SITES=('site1' 'site2' 'site3' 'site4');
SITES_PATH_BASE=/var/www/
SITES_PATH_SUFIX=/repo/public
SITES_BACKUP_FOLDER=backups
CURRENT_DATE=`date +%Y-%m-%d`;
for SITE in ${SITES[@]}
do
echo "";
echo "Taking backup of ${SITE}..."
BACKUP_FOLDER=${SITES_PATH_BASE}${SITE}/${SITES_BACKUP_FOLDER};
# Create backup folder if it does not exist
if [ ! -d $BACKUP_FOLDER ]; then
echo "Creating folder ${BACKUP_FOLDER}";
mkdir -p $BACKUP_FOLDER;
fi
BACKUP_FILENAME=${BACKUP_FOLDER}/db_backup-${CURRENT_DATE}.sql;
# Export db and save the backup relative to the public dir
wp db export ${BACKUP_FILENAME} --path=${SITES_PATH_BASE}${SITE}${SITES_PATH_SUFIX} --allow-root;
done
# Backups files and databases + updating WordPress and plugins with extra paramter ( "./wp-maintenance.sh update" )
declare -a SITES=('site1' 'site2' 'site3' 'site4');
SITES_PATH_BASE=/var/www/
SITES_PATH_SUFIX=/repo/public
SITES_BACKUP_FOLDER=backups
CURRENT_DATE=`date +%Y-%m-%d`;
PARAM1=$1;
for SITE in ${SITES[@]}
do
SITE_PUBLIC_DIR=${SITES_PATH_BASE}${SITE}${SITES_PATH_SUFIX}
echo "";
echo "Taking backup of ${SITE}..."
BACKUP_FOLDER=${SITES_PATH_BASE}${SITE}/${SITES_BACKUP_FOLDER};
# Create backup folder if it does not exist
if [ ! -d $BACKUP_FOLDER ]; then
echo "Creating folder ${BACKUP_FOLDER}";
mkdir -p $BACKUP_FOLDER;
fi
echo "Backing up the database..."
SQL_BACKUP_FILENAME=${BACKUP_FOLDER}/${SITE}_db_backup-${CURRENT_DATE}.sql;
# Export db and save the backup relative to the public dir
wp db export ${SQL_BACKUP_FILENAME} --path=${SITE_PUBLIC_DIR} --allow-root;
echo "Backing up the files..."
FILE_BACKUP_FILENAME=${BACKUP_FOLDER}/${SITE}_file_backup-${CURRENT_DATE}.tar.gz;
tar -czpf ${FILE_BACKUP_FILENAME} -C ${SITE_PUBLIC_DIR} .;
echo "Files backup saved to ${FILE_BACKUP_FILENAME}";
echo "Backup completed";
if [ "${PARAM1}" == "update" ]; then
echo "Updating WordPress...";
wp core update --path=${SITE_PUBLIC_DIR} --allow-root;
echo "Updating plugins...";
wp plugin update --all --path=${SITE_PUBLIC_DIR} --allow-root;
fi
echo "Done";
echo "";
done
@pelmered
Copy link
Author

First configure SITES_PATH_BASE, SITES_PATH_SUFIX and SITES_BACKUP_FOLDER to reflect you environment and your preferences.
Then the SITES variable on the first line. This should contain the folder names to the sites you want to backup.
Then add the script to cron.

This example takes DB backup of the following sites:
/var/www/site1/repo/public/
/var/www/site2/repo/public/
/var/www/site3/repo/public/
/var/www/site4/repo/public/

And saves backups in (respectively):
/var/www/site1/backups/db_backup-{date}.sql
/var/www/site2/backups/db_backup-{date}.sql
/var/www/site3/backups/db_backup-{date}.sql
/var/www/site4/backups/db_backup-{date}.sql

To run the script just run ./wp-maintenance.sh or /path/to/script/wp-maintenance.sh

To update, simple add "update" after the command, like this: "./wp-maintenance.sh update"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment