useage
mysqldump.sh
1#!/bin/sh
2
3DUMP=/usr/bin/mysqldump
4BAK_DIR=/production/backup
5
6DB_HOST=localhost
7DB_PORT=3306
8if [ ! -n "$1" ]; then
9 DB_NAME=dbname
10else
11 DB_NAME="$1"
12fi
13DB_USER=root
14DB_PASS=root
15DB_CHARSET=utf8
16DAYS=7
17
18DATE=`date +%Y%m%d_%H`
19PREFIX="bak_${DB_NAME}"
20BAK_SQL="${PREFIX}_${DATE}.sql"
21BAK_TAR="${PREFIX}_${DATE}.tar.gz"
22
23cd $BAK_DIR
24
25# back db
26$DUMP --host=$DB_HOST --port=$DB_PORT --user=$DB_USER --password=$DB_PASS $DB_NAME --default-character-set=$DB_CHARSET --opt -Q -R --skip-lock-tables > $BAK_SQL
27# tar .sql to .tar.gz
28tar -czf $BAK_TAR $BAK_SQL
29
30# remove .sql file
31rm $BAK_SQL
32
33# remove backup files $DAYS ago
34find $BAK_DIR -name "${PREFIX}*" -type f -mtime +$DAYS -exec rm {} \;