Skip to content

Instantly share code, notes, and snippets.

@AtomicSmash
Last active December 23, 2015 18:39
Show Gist options
  • Save AtomicSmash/6677273 to your computer and use it in GitHub Desktop.
Save AtomicSmash/6677273 to your computer and use it in GitHub Desktop.
Sync to and from the uploads folder and database of a local Wordpress site to Dropbox
#!/bin/bash
#get folder name
echo "Enter the root folder name of Wordpress"
read name
#Get the database details from wp-config file
WPDBNAME=`cat ~/Sites/$name/wp-config.php | grep DB_NAME | cut -d \' -f 4`
WPDBUSER=`cat ~/Sites/$name/wp-config.php | grep DB_USER | cut -d \' -f 4`
WPDBPASS=`cat ~/Sites/$name/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`
#Get computer's name
HN=`hostname | awk -F. '{print $1}'`
echo "1:Sync files to Dropbox 2:Sync files from Dropbox 3:Skip"
read filedirection
#Sync files
if [ -n "$name" ]; then
#Check to see if folder exists
if [ -d "Sites/$name" ]; then
if [ "$filedirection" = "1" ]; then
echo "Syncing $name"
mkdir -p ~/Dropbox/WP/$name/files
rsync -r -v -d ~/Sites/$name/wp-content/uploads/ ~/Dropbox/WP/$name/files
#Check to see if folder exists
if [ -a "Sites/$name/.git/ftpdata" ]; then
echo "Syncing ftpdata file"
rsync -r -v -d ~/Sites/$name/.git/ftpdata ~/Dropbox/WP/$name/
else
echo "No ftpdata file found!"
fi
fi
if [ "$filedirection" = "2" ]; then
echo "Syncing $name"
rsync -r -v -d ~/Dropbox/WP/$name/files/ ~/Sites/$name/wp-content/uploads/
fi
if [ "$filedirection" = "3" ]; then
echo "Just the database then:"
fi
else
echo "Folder not found :( Check you are in your home directory!"
fi
else
echo "Give me folder name dumbass..."
fi
echo "1:Sync Database to Dropbox 2:Sync Database from Dropbox 3:Exit"
read databasedirection
#Sync files
if [ -n "$name" ]; then
if [ -d "Sites/$name" ]; then
if [ "$databasedirection" = "1" ]; then
echo "Exporting $WPDBNAME"
mkdir -p ~/Dropbox/WP/$name/SQL/
#Sync standard time stamped DB copy
/Applications/MAMP/Library/bin/mysqldump --user=$WPDBUSER --password=$WPDBPASS --opt $WPDBNAME > "Dropbox/WP/$name/SQL/sqlbackup-$WPDBNAME-$(date +%Y-%m-%d--%H-%M).sql";
#Sync machine specific DB copy
/Applications/MAMP/Library/bin/mysqldump --user=$WPDBUSER --password=$WPDBPASS --opt $WPDBNAME > "Dropbox/WP/$name/SQL/sqlbackup-$WPDBNAME-$HN-latest.sql";
#Sync 'latest' machine specific DB copy
/Applications/MAMP/Library/bin/mysqldump --user=$WPDBUSER --password=$WPDBPASS --opt $WPDBNAME > "Dropbox/WP/$name/SQL/sqlbackup-$WPDBNAME-latest.sql";
echo "Databases synced to Dropbox from $HN on $(date +%Y-%m-%d--%H-%M)" >> ~/Dropbox/WP/$name/log.log
fi
if [ "$databasedirection" = "2" ]; then
if [ -a "Dropbox/WP/$name/SQL/sqlbackup-$WPDBNAME-latest.sql" ]; then
echo "Backing up $name"
/Applications/MAMP/Library/bin/mysqldump --user=$WPDBUSER --password=$WPDBPASS --opt $WPDBNAME | gzip > "Dropbox/WP/$name/SQL/sqlbackup-$WPDBNAME-pre-sync-backup.gz";
echo "Removing current tables $name"
mysqldump -uroot -proot --add-drop-table --no-data urbanplanters | grep ^DROP | mysql -uroot -proot urbanplanters
echo "Syncing latest version of $name"
mysql -u$WPDBUSER -p$WPDBPASS $WPDBNAME < Dropbox/WP/$name/SQL/sqlbackup-$WPDBNAME-latest.sql
echo "Databases synced from Dropbox to $HN on $(date +%Y-%m-%d--%H-%M)" >> ~/Dropbox/WP/$name/log.log
else
echo "Database not found"
fi
fi
if [ "$databasedirection" = "3" ]; then
echo "See you later then..."
fi
else
echo "Folder not found :("
fi
else
echo "Give me folder name dumbass..."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment