Mysql Backup – The simple script

There is only ONE  “Golden Rule of IT” : BACK IT UP OR LOOSE IT..

1)There is no such thing as security, be it firewalls, hard drives that spin in the speed of light and have 324 years guarantee or even a pact with western digital’s CEO…When it comes to crucial data stored in databases the only thing you can rely on is a : BACKUP.

2)Testing that the precious backup, you are hiding away in the swiss vault somewhere, is ACTUALLY working will prove beneficial in the long run.

And thats it.

Here is a really simple script that works in virtually every linux distro have come across and produces nice gziped backups of MySql databases.

Enjoy!

################################################################
#!/bin/bash
#18/12/2003
#Simple script to backup a specific database
#Backup script by datetime
#USE AT YOUR OWN RISK
### MySQL Server Login Info ###

DB=”Important_DB_Name_Here”

MUSER=”root”
MPASS=’mypasswd’
MHOST=”localhost”
MYSQL=”$(which mysql)”
MYSQLDUMP=”$(which mysqldump)”
BAK=”/mysqlbackups”
GZIP=”$(which gzip)”
NOW=$(date +”%d-%m-%Y”)

[ ! -d $BAK ] && mkdir -p $BAK

##ACTUAL WORK TAKES PLACE HERE##
FILE=$BAK/Desired_static_namepart-$NOW.gz
$MYSQLDUMP -u $MUSER -h $MHOST –password=$MPASS $DB1 | $GZIP -9 > $FILE

#########################################################################
# Uncomment the following line to delete files older than 10 days in the backup directory
#find  /mysqlbackups/* -mtime +10 -exec rm -f {} \;

#########################################################################

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS