备份数据库的shell
#!/bin/bash #定义备份数据库名 dbname=yourdbname #定义备份数据库的用户名和密码 dbuser=yourdbuser dbpasswd=yourdbpasswd #数据库备份路径 backuppath=/home/dbback/sqlzip/ #数据库备份日志文件存储的路径 logfile=/home/dbback/log/db_backup.log #获取当前日期,用作数据库名 now=`date +%Y-%m-%d` #拼接文件名 dumpfile=db-$now #使用下面函数备份数据库 back_db(){ echo "------"$(date +%Y-%m-%d%t%A%t%T)" Beginning database "${dbname}" backup--------" >>${logfile} mysqldump --skip-opt -u${dbuser} -p${dbpasswd} --default-character-set=utf8 --quick --force --routines --add-drop-database --add-drop-table ${dbname} >${backuppath}${dumpfile}.sql 2>> ${logfile} echo "mysqldump --skip-opt -u${dbuser} -p${dbpasswd} --default-character-set=utf8 --quick --force --routines --add-drop-database --add-drop-table ${dbname} >${backuppath}${dumpfile}.sql 2>> ${logfile}" echo $(date +%Y-%m-%d%t%A%t%T)" Beginning zip ${backuppath}${dumpfile}.sql" >>${logfile} tar zcvf ${dumpfile}.tar.gz ${dumpfile}.sql && rm ${dumpfile}.sql 2>> ${logfile} echo "backup file name:"${dumpfile}".tar.gz" >>${logfile} echo -e "-------"$(date +%Y-%m-%d%t%A%t%T)" Ending database "${dbname}" backup-------\n" >>${logfile} } #删除过期文件 rm_oldfile(){ find /home/dbback/sqlzip/ -type f -mtime +3 -exec rm {} \; } cd ${backuppath} back_db rm_oldfile
如果执行报错,说明文件格式错误了,看这个:https://www.cnblogs.com/yuwensong/p/10942623.html