mysqldump定时备份数据库
#!/bin/bash
CONTAINER_NAME_OR_ID='mysql'
MYSQL_USER='root'
MYSQL_PASSWORD='123456'
DB_NAME='XXX'
SAVE_TIME=$(date "+%Y%m%d%H%M")
DIR="/opt/backup_mysql"
if [ ! -d "$DIR" ];then
mkdir $DIR
echo "创建文件夹成功"
else
echo "文件夹已经存在"
fi
BACKUP_FILE="/opt/backup_mysql/${DB_NAME}_${SAVE_TIME}.sql"
echo $MYSQL_PASSWORD | docker exec -i $CONTAINER_NAME_OR_ID mysqldump -u$MYSQL_USER -p $DB_NAME > $BACKUP_FILE
if [ $? -eq 0 ]; then
echo "Backup completed successfully: $BACKUP_FILE"
else
echo "Backup failed"
fi
添加定时任务crontab -e
* * * * *
- - - - -
| | | | |
| | | | +----------星期中星期几 (0 - 6) (星期天 为0)
| | | +---------------月份 (1 - 12)
| | +--------------------一个月中的第几天 (1 - 31)
| +-------------------------小时 (0 - 23)
+------------------------------分钟 (0 - 59)
添加定时任务(每天06:18执行)
18 6 * * * /opt/backup_mysql/backup_mysql.sh