一个转存mysql数据库的shell脚本,代码:
复制代码 代码示例:
#!/bin/bash
# 一个简单的备份mysql数据库的脚本
# 每隔一小时转存mysql数据库,从raid10磁盘调协到到/nas/mysql中。
#
# Directory: /nas/mysql/mm-dd-yyyy
# File: mysql-DBNAME.04-25-2008-14:23:40.gz
# Full path: /nas/mysql/mm-dd-yyyy/mysql-DBNAME.04-25-2008-14:23:40.gz
#site www.jb200.com
NOW=$(date +"%m-%d-%Y") # mm-dd-yyyy format
FILE="" # used in a loop
NASBASE="/nas" # NAS Mount Point
BAK="${NAS}/mysql/${NOW}" # Path to backup dir on $NAS
### 服务器信息 ###
#* mysql用户 *#
MUSER="root"
#* mysql用户密码 *#
MPASS="YOUR-PASSWORD"
#* 主机信息 *#
MHOST="127.0.0.1"
#* MySQL binaries *#
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
# 确认nas设备是否挂载
mount | awk '{ print $3}' |grep -w $NASBASE >/dev/null
if [ $? -ne 0 ]
then
echo "Error: NAS not mounted at $NASBASE, please mount NAS server to local directory and try again."
exit 99
fi
### NAS MUST BE MOUNTED in Advance ###
# assuming that /nas is mounted via /etc/fstab
if [ ! -d $BAK ]; then
mkdir -p $BAK
else
:
fi
# 取得所有数据库的列表
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
# 转储数据
for db in $DBS
do
FILE=$BAK/mysql-$db.$NOW-$(date +"%T").gz
# gzip compression for each backup file
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done