Wednesday, June 4, 2014

A simple MySQL daily backup script

Using the MySQL export script I've showed from last post (assume you saved it in a file named "$HOME/"), you may now perform a daily backup with your crontab service like this.

DIR=`dirname $0`
echo "Backup DB with export script."

echo "Moving exported files into backup dir."

DB_BAK_DIR=$HOME/mysql-backups/`date +%a`
echo "Removing old file (if exists) and saving new backup into $DB_BAK_DIR"
if [[ -e $DB_BAK_DIR ]]; then
  rm -fv $DB_BAK_DIR/*
  mkdir -p $DB_BAK_DIR
cp -v $DIR/*.sql $DB_BAK_DIR

This script should create a daily folder under your home directory, for example like "$HOME/mysql-backups/Mon", "Tue", "Wed" etc. It should save up to 7 days in a week if you run this with a daily cron job.

# crontab -e
@daily $HOME/ > /dev/null 2>&1

This is not the most robust way of backing up your DB, but it's a simple solution if you just want something quick up and running without worry too much.