[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 # delete the backup 2 days ago DONE fi `DATE = -d DEL_DIR" -2 Days "+% F` [-d $ BACKUP_DIR / $ DEL_DIR] && $ BACKUP_DIR RM -rf / $ DEL_DIR # Join the task (echo" 1 01 * * * / mdata / mysql / mysql_backup.sh "; crontab -l) | crontab
Note: I have here is a binary installation, the installation directory can also modify their own variables, only for reference