Last active
October 23, 2017 05:35
-
-
Save liuyigh/5e73313221e911991f25a460f54ec651 to your computer and use it in GitHub Desktop.
On-demand back up WordPress to Google Drive with Bash Script. Adapted frpm [email protected] for serverpilot server. Removed some scripts for automatic backup, such as old backup deletion, directory checking etc.
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 | |
# Source: https://guides.wp-bullet.com | |
# Author: Mike | |
#define local path for backups | |
BACKUPPATH="/srv/users/serverpilot/bak/tmp" | |
#define remote backup path | |
BACKUPPATHREM="gBackup" | |
#path to WordPress installations, no trailing slash | |
SITESTORE="/srv/users/serverpilot/apps" | |
#date prefix | |
DATEFORM=$(date +"%Y-%m-%d") | |
#create array of sites based on folder names | |
for site_path in $( cd $SITESTORE ; find . -mindepth 3 -maxdepth 3 -type d -name 'wp-admin' | sort -n | cut -d / -f 2 ); do | |
SITELIST="${SITELIST} ${site_path}" | |
done | |
#make sure the backup folder exists | |
mkdir -p $BACKUPPATH | |
#check remote backup folder exists on gdrive | |
BACKUPSID=$(gdrive list --no-header | grep $BACKUPPATHREM | grep dir | awk '{ print $1}') | |
if [ -z "$BACKUPSID" ]; then | |
gdrive mkdir $BACKUPPATHREM | |
BACKUPSID=$(gdrive list --no-header | grep $BACKUPPATHREM | grep dir | awk '{ print $1}') | |
fi | |
#start the loop | |
for SITE in ${SITELIST[@]}; do | |
# create the local backup folder if it doesn't exist | |
if [ ! -e $BACKUPPATH/$SITE ]; then | |
mkdir $BACKUPPATH/$SITE | |
fi | |
#enter the WordPress folder | |
cd $SITESTORE/$SITE | |
#back up the WordPress folder | |
tar -czf $BACKUPPATH/$SITE/$DATEFORM-$SITE.tar.gz public | |
#back up the WordPress database, compress and clean up | |
cd $SITESTORE/$SITE/public | |
wp db export $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql --add-drop-table --skip-themes --skip-plugins | |
cat $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql | gzip > $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql.gz | |
rm $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql | |
#get current folder ID | |
SITEFOLDERID=$(gdrive list --no-header | grep $SITE | grep dir | awk '{ print $1}') | |
#create folder if doesn't exist | |
if [ -z "$SITEFOLDERID" ]; then | |
gdrive mkdir --parent $BACKUPSID $SITE | |
SITEFOLDERID=$(gdrive list --no-header | grep $SITE | grep dir | awk '{ print $1}') | |
fi | |
#upload WordPress tar | |
gdrive upload --parent $SITEFOLDERID --delete $BACKUPPATH/$SITE/$DATEFORM-$SITE.tar.gz | |
#upload wordpress database | |
gdrive upload --parent $SITEFOLDERID --delete $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql.gz | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment