cd /usr/local/mysqlbackup/
vi mysql_backup.sh
#!/bin/bash
db_user='root'
db_password='密码'
db_name='数据库名'
backup_dir='/usr/local/mysqlbackup/' #你的路径
current_time=$(date +'%Y-%m-%d_%H%M%S')
filepath=$backup_dir$current_time'.sql.gz'
echo '开始导出数据库...'
mysqldump -u$db_user -p$db_password $db_name | gzip > $filepath
echo '导出成功,文件名为: '$filepath
chmod +x mysql_backup.sh
sh ./mysql_backup.sh
[root@iZ2zeavn7cbxg2g9bs5donZ mysqlbackup]# sh ./mysql_backup.sh
开始导出数据库...
导出成功,文件名为: /usr/local/mysqlbackup/2021-07-21_101944.sql.gz
[root@iZ2zeavn7cbxg2g9bs5donZ mysqlbackup]# ll
total 868
-rw-r--r-- 1 root root 881615 Jul 21 10:19 2021-07-21_101944.sql.gz
-rwxr-xr-x 1 root root 329 Jul 21 10:19 mysql_backup.sh
每天凌晨2点定时备份
crontab -e
0 0 2 * * ? * /usr/local/mysqlbackup/mysql_backup.sh