I wrote this little script the other day to back up my WordPress install. Because I use Navicat, I had always been pretty good about backing up the database, but I didn’t backup the install base nearly as often as I should have. I’m sure it won’t be useful for everyone because it requires access to the command line, and mysqldump, but it’s nice to know that my blog is getting backed up.
It’s really just a simple shell script that is executed nightly by cron. You can set up the backup directories in any way you like, but if you store your database backups in a subdirectory of the WordPress install, make darn sure that the directory is not readable by the web server.
The example below assumes the following:
- WordPress (your web root) is: /webserver/wordpress
- Your dumps directory is: /home/backups/wp-backups
- The name of your WordPress database is wpdata
- Your WordPress database user is: dbuser
- Your WordPress database password is: dbpassword
Remember to change these variables for your install.
echo “Creating Database Backup”;
mysqldump -u dbuser -pdbpassword wpdata | gzip > /home/backups/wp-backups/wordpress-$Date.sql.gz;
echo “Creating Filesystem Backup”;
/usr/bin/tar -czf /home/backups/wp-backups/wordpress-$Date.tgz wordpress;
echo “Backup Complete”;
Each time the script is run, it will create two timestamped files in /home/backups/wp-backups. The first one: wordpress-TIMESTAMP.sql.gz is a compressed export of your database. The second file: wordpress-TIMESTAMP.tgz is a compressed tar archive of your WordPress install.