要注意mysql.sock和bin路径,BAKPATH为备份文件存放目录
#! /bin/bash BAKPATH=/mnt/data1/mysql/backup/data MYUSER=root MYPASS="hw7777901" SOCKET=/mnt/data1/mysql/mysql.sock MYCMD="/usr/local/src/mysql-5.5.62/bin/mysql -u$MYUSER -p$MYPASS -S $SOCKET" MYDUMP="mysqldump -u$MYUSER -p$MYPASS -S $SOCKET -x -F -R" DBLIST=`$MYCMD -e "show databases;" |sed "1d" |egrep -v "_schema|test"` backtime=`date +%Y%m%d%H%M%S` [ ! -d $BAKPATH ] && mkdir -p $BAKPATH for dbname in $DBLIST do TLIST=`$MYCMD -e "show tables from $dbname;" |sed "1d"` for tname in $TLIST do mkdir -p $BAKPATH/$dbname/${backtime} find $BAKPATH/$dbname/${backtime} -type f -mtime +7 -exec rm -f {} ; $MYDUMP $dbname $tname > $BAKPATH/$dbname/${backtime}/${tname}_$(date +%F).sql done done