Skip to content

Instantly share code, notes, and snippets.

@michaelbrazell
Last active February 24, 2017 21:52
Show Gist options
  • Save michaelbrazell/444e826a1813c90802754c6151403745 to your computer and use it in GitHub Desktop.
Save michaelbrazell/444e826a1813c90802754c6151403745 to your computer and use it in GitHub Desktop.
A simple shell script for backing up your WP Database and WP Files using the wp-cli
#!/usr/local/bin/bash
# Backup script using wp-cli
DBMESSAGE="Backing up database..."
FSMESSAGE="Backing up file system..."
DONEMESSAGE="Done."
MOVEMESSAGE="Moving files to public location..."
FINISHED="Backup complete."
echo $DBMESSAGE
/path/to/wp-cli/wp db export wordpress_$(date +%Y%m%d).sql --path=/var/www/html
echo $DONEMESSAGE
echo $FSMESSAGE
tar --exclude='./wordpress_dev' -zcf wordpress_$(date +%Y%m%d).tar.gz /var/www/html/.
echo $DONEMESSAGE
echo $MOVEMESSAGE
mv ./web_standards_$(date +%Y%m%d).sql /path/to/another/location/
mv ./web_standards_$(date +%Y%m%d).tar.gz /path/to/another/location/
echo $DONEMESSAGE
echo $FINISHED
@michaelbrazell
Copy link
Author

michaelbrazell commented Feb 24, 2017

wp-cli Backup Script

A simple shell script to backup a local instance of WordPress, database and static files. Ideally this shouldn't be run more than once a day because the file names will have dates at the end. This could be solved by adding a random string or a timestamp, but date is enough for our uses. Feel free to modify or reuse however you like, email me or comment if you have questions or modifications of your own.

Instructions

  1. Set the path to your bash on line 1. In my example, I'm running bash as a user on a server, so I'm using my local bash. You can get this by running $ which bash
  2. On line 11, set the path to your wp-cli. If you've setup wp-cli correctly you can probably just run wp and you won't need the full path. The permissions on my server were strict so I had to point to the full path.
  3. Also on line 11, set the path to your WordPress root directory. For a standard apache install, it's usually --path=/var/www/html but your mileage may vary
  4. On line 14, I have an --exclude='./wordpress_dev' because I have a development instance within my WordPress root. You probably don't have this, so feel free to remove that.
  5. On line 17 and 18 I have mv commands that move the exported files from where I run them to a public location within my company, so others can grab them if need be. You could be creative and add an scp here pushing the files up to an Amazon S3 bucket!
  6. If you want to remove the echo statements updating status, go for it. Lines 3-7 and any line beginning with echo can be removed if you want to slim it down.

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