cat /u01/sqlbackup.sh
#!/bin/bash
#descripttion:this is a script for backup mysql database
#author:wkx
#data:2019-5-29
#database info
username="backup"
passwd="qingtian_bak"
port="3306"
hostname="127.0.0.1"
#create a dir for backup data
back_time=$(date +%F)
back_dir="/u01/mysql_data_bak/back_mounth"
[ ! -d ${back_dir} ] && mkdir -p ${back_dir}
cd ${back_dir}
mkdir ${back_time}
cd ${back_time}
#backup database
echo "starting backup database , please waiting ......"
for DB_nm in `cat /script/DB_Name_Mounth |grep -v "#"`
do
#mysqldump -h${hostname} -u${username} -p${passwd} ${name} | gzip > ${name}.sql.gz
mysqldump -R --triggers -B -u${username} -p${passwd} -h${hostname} ${DB_nm} > ${DB_nm}.sql 2>>/dev/null
done
echo "backup database is successfull ......"
echo "backup dir is in ${back_dir}/${back_time} ,please check......"
创建定时任务
echo '00 00 * * * /u01/sqlbackup.sh' > /var/spool/cron/root