#!/bin/bash
#DB服务器IP
DB_HOST="127.0.0.1"
#database name
DB_NAME="mysql"
#database username
DB_USER="USER"
#database password
DB_PASS="password"
#需要备份的表名
TBL_NAME_1=user
#需要备份的表名
TBL_NAME_2=db
#当前日期时间字符串 例:2010-12-20-10-12-30 (年月日时分秒)
DATE=`date +%Y-%m-%d-%H-%M-%S`
#执行mysql命令的参数
SQL_OPT="-u$DB_USER -p$DB_PASS -h $DB_HOST $DB_NAME"
/usr/bin/mysql $SQL_OPT << EOF
#将单个表的数据导出到文件中,
#FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' 这些选项是把数据用逗号分隔,双引号引起来,换行用\n;如果不用,可以将这些选项删除。
select * from $TBL_NAME_1 into outfile "/tmp/bakfile/$TBL_NAME_1.$DATE.txt" FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
select * from $TBL_NAME_2 into outfile "/tmp/bakfile/$TBL_NAME_2.$DATE.txt" FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
quit
EOF
echo 'Backup success'
|