WordPress Backup Script

Recently I remembered that I had this little handy script that I used to backup my WordPress blog. The script contains only 2 actual commands.

I used nice while compressing / uncompressing the files to avoid CPU throttling since my blog is running on a shared hosting server.

This WordPress backup script doesn’t work without user interaction since it needs you to enter the password.

Commands used

 mysqldump -u <username> -p <database>: dump the database contents
 nice -n <niceness> <command>: run the command in specified niceness
 gzip -f: compress stdin on the fly and output
 zcat -f <filename>: uncompress the file and send it to output
 tar czf <output file> <input files>: create a gzipped tarball
 tar xzf wp-file-backup.tar.gz -C <directory>: extract gzipped tarball into the specified directory
 mysql -p <database>: Run the commands coming in from stdin inside the database


Note: Don’t forget to change username, database and directory for the files.


# Backup MySQL database and compress with max niceness
mysqldump -u omer -p wordpress | nice -n 19 gzip -f > wp-db-backup.sql.gz
echo "Database backup done"

# Backup Files
# Change directory to where wordpress is contained
cd public_html
# Create a gzipped tarball containing only wordpress files with max niceness
nice -n 19 tar czf ../wp-file-backup.tar.gz wp-* xmlrpc.php index.php .htaccess
echo "File backup done"

Running the script

Save the file as backup-wp.sh and execute it using the following:

sh backup-wp.sh

Restoring from backup

These commands can be used to restore from backup. I didn’t actually put them in a script since it is more likely that you will need a partial restore.

Note 1: While database is being restored all the tables will be dropped and re-imported.

Note 2: Don’t forget to change username, database and directory for the files.

# Restore the database
nice -n 19 zcat -f wp-db-backup.sql.gz | mysql -p wordpress

# Restore the files
nice -n 19 tar xzf wp-file-backup.tar.gz -C public_html

Leave a Reply

Your email address will not be published. Required fields are marked *