mysqldump数据库备份示例
作者:互联网
[root@localhost mdata]# cat mysql_backup.sh #!/bin/bash BACKUP_DIR=/mysql-backup DATE_DIR=`date +%F` [ -d $BACKUP_DIR/$DATE_DIR ] || mkdir $BACKUP_DIR/$DATE_DIR -p &>/dev/null time="$(date +"%Y-%m-%d")" USER= PASS= PORT= #备份数据库,全量备份 DB_LIST=$(/mdata/mysql/bin/mysql -u$USER -p$PASS -P$PORT -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys") for DB in $DB_LIST; do if ! /mdata/mysql/bin/mysqldump -u$USER -p$PASS $DB > $BACKUP_DIR/$DATE_DIR/$DB-${time}.sql 2>/dev/null;then echo "$DATE_DIR $DB-${time}.sql 备份失败" >> /mdata/mysql/mysqlbackup.log fi done #删除2天前的备份 DEL_DIR=`date -d "-2 days" +%F` [ -d $BACKUP_DIR/$DEL_DIR ] && rm -rf $BACKUP_DIR/$DEL_DIR #加入计划任务 (echo "1 01 * * * /mdata/mysql/mysql_backup.sh";crontab -l) |crontab
注:我这里是二进制安装,安装目录也可以自己修改为变量,只做参考
标签:mdata,示例,备份,DB,mysqldump,mysql,DATE,BACKUP,DIR 来源: https://blog.51cto.com/13641616/2438784