1.准备
mysql 8.0
crontab
2.mysql备份位置/data/db_backup/mysql
3.脚本文件vim /data/db_backup/dbbackup.sh
4.内容db_user="root"
db_password="root"
db_name="digital_countryside_project"
#the directory for story your backup file.you shall change this dir
backup_dir="/data/db_backup/mysql"
#date format for backup file (dd-mm-yyyy)
time="$(date +"%Y%m%d%H%M%S")"
#注意:mysqldump 要为绝对路径,例如 /root/mysql/bin/mysqldump
/usr/bin/mysqldump -udb_user -pdb_password $db_name > backup_dir/db_name"_"$time.sql
#!/bin/bash (对备份进行压缩)
#/usr/bin/mysqldump -udb_user -pdb_password $db_name | gzip > backup_dir/db_name""$time.sql.gz
#删除七天之前的备份
find $backup_dir -name $db_name"*.sql" -type f -mtime +7 -exec rm -rf {} \; > /dev/null 2>&1
#删除一分钟之前的备份
#find $backup_dir -name $db_name"*.sql" -type f -mmin +1 -exec rm -rf {} \; > /dev/null 2>&1
5.权限chmod 777 dbbackup.sh
6.定时编辑
crontab -e #打开并编辑cron任务
7.内容00 01 * * * /data/db_backup/dbbackup.sh(每天凌晨一点备份数据库)