Bash

A Simple MySQL Backup Script

It was time to deploy the project into the production server. Almost everything is complete. The pages are running smoothly, the scheduler for current weather forecast and the database is fully optimized.

One lacking is the daily database backup. I decided it to be daily since it is not that critical, so here is my script for the daily MySQL Backup.

#!/bin/bash
filename='db_name'
date=`date +%Y-%m-%d-%H-%M-%S`
backup="/home/user/test_db_backup/$filename.$date.gz"
mysqldump rani -u db_user -pthePassw0rd | gzip -c > $backup

It simply backs up the database using mysqldump utility, and pipe it to gzip utility to compress the file and finally saves into the backup path. By the way it uses the current date and time appended to the filename.

Schedule it every midnight as specified by @daily here:

http://blog.lysender.com/2009/09/executing-php-script-via-cron-and-curl/

Leave a reply

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