MySQL database backup FAQ: Can you share a Linux shell script that I can use to make a MySQL backup?
I currently have a collection of websites on several different servers (including GoDaddy and A2 Hosting web servers), so I was just spending some time trying to automate my MySQL database backups. To that end, I just created a MySQL shell script that I use on each Linux server to make my database backups, and I thought I'd share that script here.
Here's the source code for my MySQL database backup shell script. An explanation follows the shell script source code.
#!/bin/sh #---------------------------------------------------- # a simple mysql database backup script. # version 2, updated March 26, 2011. # copyright 2011 alvin alexander, http://devdaily.com #---------------------------------------------------- # This work is licensed under a Creative Commons # Attribution-ShareAlike 3.0 Unported License; # see http://creativecommons.org/licenses/by-sa/3.0/ # for more information. #---------------------------------------------------- # (1) set up all the mysqldump variables FILE=minime.sql.`date +"%Y%m%d"` DBSERVER=127.0.0.1 DATABASE=XXX USER=XXX PASS=XXX # (2) in case you run this more than once a day, remove the previous version of the file unalias rm 2> /dev/null rm ${FILE} 2> /dev/null rm ${FILE}.gz 2> /dev/null # (3) do the mysql database backup (dump) # use this command for a database server on a separate host: #mysqldump --opt --protocol=TCP --user=${USER} --password=${PASS} --host=${DBSERVER} ${DATABASE} > ${FILE} # use this command for a database server on localhost. add other options if need be. mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > ${FILE} # (4) gzip the mysql database dump file gzip $FILE # (5) show the user the result echo "${FILE}.gz was created:" ls -l ${FILE}.gz
Here's a quick overview of what this shell script does:
If you make this shell script executable and then place an entry in your crontab file, hopefully this will make a decent little MySQL database backup shell script.
On a related note, if you're not familiar with the Linux crontab facility, here are some of my crontab tutorials:
As a quick note, if you're familiar with crontab, you know you'll want to create an entry that looks something like this to run your MySQL database backup script:
0 5 * * * /home/al/bin/mysql-database-dump.sh
In this crontab example, the MySQL database dump shell script will be run every day, at five minutes after midnight. Once again, adjust this as desired.
Of course there are other things you can do with this MySQL database backup script, but I wanted to make sure I shared this MySQL backup script to help get you started.
At some point I'll share a companion shell script that will delete the old backup files that will accumulate from this approach.
I hope this example MySQL database backup shell script has been helpful. If you have any suggestions that will improve this script for everyone, just leave a note in the Comments section below.
more complete? :)
would be sweet if you added support for Amazon S3 using s3-bash
and site data backup
http://code.google.com/p/s3-bash/
so its like this
Backup MySQL
Backup Site Data
Transfer Backup to Amazon S3 or FTP (s3-put)
that way you backup everything and as a added bonus it works on most systems even godaddy and is more offsite than in another directory :P
Post new comment