Shell Script To Backup MySQL Database
Beware this content is over 6 years old and considered stale. It may no longer be accurate and/or reflect the understanding of the author but remains here for reference only. Please keep this in mind as you use this content.
This is connected to my previous Dropbox post.
While I use Git to deploy my websites and therefore don’t require backing up of the entire site. Some folders with user-generated content or media may need to be specifically backed up and therefore this script could be used as the basis for that sort of functionality. Feel free to edit as needed.
The example script below uses a typical WordPress configuration as an example where we need to backup the
wp-content/uploads directory. It also assumes you’ll be running this as
root with Dropbox installed for the
root user account.
The best place to store this script is in the Dropbox directory (
~/Dropbox/wordpress) you want the backups to be stored. That way you can write multiple backup scripts for different sites.
Name the file
Next, make the script executable.
$ sudo chmod +x ~/Dropbox/wordpress/backup.sh
Run it from the command line to test the output.
cd ~/Dropbox/wordpress && ./backup.sh
All being well it should have created your backup files. You’ll want to download them and give the archives a test by extracting and importing the content.
While a backup script is great its not much use if the last backup was several months or years ago and you end up losing large amounts of data since the site went live.
Save yourself the hassle of having to remember and link the script above to a
cron job and let the server do the hard work.
$ sudo crontab -e