May 28, 2022

The Blog of a Chronic Content Creator

All-in-One Blog Backup Script

If you’re like me, you like backing up things to keep them safe. This blog is a great example – including my old LiveJournal posts which I imported a few years ago, I’ve got material here from 2004 on. I like to keep it safe, however since it’s a blog running on WordPress, I can’t just backup a bunch of HTML files and call it a day. My solution is to use the following script, which downloads a SQL data dump of the full database, then creates a compressed archive (using Tar and bzip) of the blog files as well as the data dump. If worse came to worst, I could blow away the “damaged” files and database, and use these files and the database backup to get back to where I was.

Here’s the script:

 DATE=`date +%F`
 /usr/bin/mysqldump --opt -h DBHOST -u DBUSER --password=DBPASS  DBNAME > `date +%F`-blog.sql
 tar -cf $TARFILE ./blogfiles ./`date +%F`-blog.sql
 bzip2 $TARFILE

To get it up and running, replace DBHOST with your database server address, DBUSER with the username for the database, DBPASS with the database password (make sure it’s right after the =, no space), and DBNAME with the database name. Then on the line that begins with “tar”, replace ./blogfiles with the path to your blog’s files. The end result should be a file with a .bz2 extension, which you can download to your computer for safe keeping.