Centos regularly backs up MySQL database

1. Write the database backup script backupmysql.sh

#!/bin/bash
# Name:bakmysql.sh
# This is a ShellScript For Auto DB Backup and Delete old Backup
 
#备份地址
backupdir=/data/mysql/dwy/mysqlbackup;
 
#备份文件后缀时间
time=_` date +%Y_%m_%d_%H_%M_%S`
 
#需要备份的数据库名称
db_name=database
 
#mysql 用户名
db_user=root
 
#mysql 密码
db_pass=password
 
/usr/local/mysql/bin/mysqldump -u$db_user -p$db_pass $db_name|gzip>$backupdir/$db_name$time.sql.gz
 
#删除7天前备份文件
find $backupdir -name "$backupdir_*.sql.gz" -type f -mtime +7 -exec rm -rf {} \; > /dev/null 2>&1

Second, grant script authorization

chmod +x backupmysql.sh

Three, write scheduled backup tasks

# 输入如下命令:
crontab -e
 
# 在页面中编写如下内容:
00 03 * * * /root/backupmysql.sh

Four, achievable effects

It can automatically execute the written script at three o'clock in the morning every day, back up the MySQL database to /data/mysql/dwy/mysqlbackup, and only keep the database backups of the last 7 days.

https://mp.weixin.qq.com/s/1oHK0TJxI8B8qqXZ95o_LA

Guess you like

Origin blog.csdn.net/qq_40907977/article/details/114868768