William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Bash: MySQL Database backup

Here is a complete example of MySQL Database backup script (dump out & compress):

#!/bin/bash
# 1. if exists, quit
# 2. if not, do the backup
# 3. compress
# put into cron job, every week based.
DB="DATABASE"
USER="test"
PASS='test'
DIR='/home/test/DBs/'

DATE=`date '+%d%h%Y'`
FILE="${DIR}/${DB}${DATE}.sql"
ZFILE="${FILE}.gz"

if [ -f ${ZFILE} ]; then
 echo "$ZFILE already exists."
 exit;
fi

mysqldump -u ${USER} -p"${PASS}" -h localhost ${DB} > ${FILE}
gzip -f $FILE

Put this script into crontab will make the backup automatically. such as:

$ crontab -e
0 0 * * 0 $HOME/bin/this_script.sh
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: