Skip to content

Instantly share code, notes, and snippets.

@sarahmonster
Last active August 29, 2015 14:04
Show Gist options
  • Save sarahmonster/3c66c3171c971c0e871f to your computer and use it in GitHub Desktop.
Save sarahmonster/3c66c3171c971c0e871f to your computer and use it in GitHub Desktop.
Create WordPress backup file, save to Dropbox, then pull to development copy and replace values as required.
#!/bin/sh
export DATE=`date "+%Y-%m-%d"`
export DB_HOST=localhost
export DB_NAME=
export DB_USER=
export PASSWORD=
export DROPBOX_UPLOADER=.Dropbox-Uploader/dropbox_uploader.sh
export SCRIPTS_DIR=''
# Make sure we're in the scripts dir
cd $SCRIPTS_DIR
# Make a backup of the database!
mysqldump --opt -h$DB_HOST -u$DB_USER -p$PASSWORD $DB_NAME > backup/blog-database-$DATE.sql
# Make a backup of the files!
#tar -cf backup/blog-files-$DATE.tar ../public_html/blog/*
# Send to Dropbox
#$DROPBOX_UPLOADER upload backup/blog-files-$DATE.tar blog-files-$DATE.tar
$DROPBOX_UPLOADER upload backup/blog-database-$DATE.sql blog-database-$DATE.sql
# Remove older files from Dropbox
DELETE=`date --date="7 days ago" "+%Y-%m-%d"`
$DROPBOX_UPLOADER delete blog-files-$DELETE.tar.gz
$DROPBOX_UPLOADER delete blog-database-$DELETE.tar.gz
# Remove old backups
rm backup/blog-files-$DATE.tar
rm backup/blog-database-$DATE.sql
#!/bin/sh
export DATE=`date "+%Y-%m-%d"`
export DB_HOST=localhost
export DB_NAME=
export DB_USER=
export PASSWORD=
export DROPBOX_UPLOADER=./Dropbox-Uploader/dropbox_uploader.sh
export WP_PREFIX=wp
export SITE_URL='http://'
export PLUGIN_STRING='a:12:{i:0;s:37:"advanced-excerpt/advanced-excerpt.php";i:1;s:19:"akismet/akismet.php";i:4;s:27:"hover-image/hover-image.php";i:5;s:17:"iframe/iframe.php";i:7;s:45:"no-comments-on-pages/no-comments-on-pages.php";i:8;s:31:"post-snippets/post-snippets.php";i:9;s:27:"redirection/redirection.php";i:10;s:45:"taxonomy-terms-order/taxonomy-terms-order.php";i:11;s:37:"tinymce-advanced/tinymce-advanced.php";i:12;s:19:"viglink/viglink.php";i:13;s:51:"wordpress-popular-posts/wordpress-popular-posts.php";i:15;s:42:"yet-another-related-posts-plugin/yarpp.php";}'
# Download most recent backup from Dropbox
#DELETE=`date --date="1 day ago" "+%Y-%m-%d"`
$DROPBOX_UPLOADER download blog-database-$DATE.sql
# Make a backup of the database first!
mysqldump --opt -h$DB_HOST -u$DB_USER -p$PASSWORD $DB_NAME > backup/staging-blog-database-$DATE.sql
# Replace old database with new
mysql -u$DB_USER -p$PASSWORD $DB_NAME < blog-database-$DATE.sql
# Change the hostnames
mysql -u$DB_USER -p$PASSWORD $DB_NAME --execute="UPDATE ${WP_PREFIX}_options SET option_value='$SITE_URL' WHERE option_name='siteurl';"
mysql -u$DB_USER -p$PASSWORD $DB_NAME --execute="UPDATE ${WP_PREFIX}_options SET option_value='$SITE_URL' WHERE option_name='home';"
# Only activate plugins that make sense
mysql -u$DB_USER -p$PASSWORD $DB_NAME --execute="UPDATE ${WP_PREFIX}_options SET option_value='$PLUGIN_STRING' WHERE option_name='active_plugins';"
# Delete downloaded backup file
rm blog-database-$DATE.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment