Database Backup Script

I just wrote a little script to backup the database of this blog. This script isĀ absolutely not good for a production server and I do not warranty this to work correctly, but if you want something for backing up a DB this might not be a terrible way to start.

I have this script setup in /etc/cron.daily/ so it will run everyday at midnight. Keep in mind that the script must be executable for it to run.

Script

#!/bin/bash
# Backups WordPress database to the path defined below.
# Variables.
TIMESTAMP=$(date "+%Y-%m-%d-%H%M")
FILENAME=$TIMESTAMP.sql
FULLPATH=/path/to/db/exports/
DBUSER=username
DBPASS=password
DBSCHEMA=databasename
# Setting working directory to FULLPATH provided above.
cd $FULLPATH
# Execute DB backup command.
mysqldump -u $DBUSER -p$DBPASS $DBSCHEMA > $FILENAME

If the script is deployed like mine is, it’s executed by root, so you shouldn’t have to worry about permissions issues. Again, just remember the script is executable by doing this:

chmod +x /path/to/script.sh

Again, I don’t warranty this and it’s by no means a production quality script.

 

Leave a Reply

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