当前位置:网站首页>linux MySQL数据定时dump
linux MySQL数据定时dump
2022-04-23 14:00:00 【白云碎里一蓑舟】
一、备份数据库脚本文件
#!/bin/sh
# 数据库账号信息
DB_USER="root"
DB_PWD="123456"
DB_HOST="192.168.110.195"
DB_PORT="3305"
# MYSQL所在目录
MYSQL_DIR="/usr/local/mysql"
# 备份文件存放目录
BAK_DIR="/data/backups"
# 时间格式化,如 20200902
DATE=`date +%Y%m%d`
# 备份脚本保存的天数
DEL_DAY=10
# 要备份的数据库,空格分隔
DATABASES=("sales_release")
# 创建日期目录
mkdir $BAK_DIR/$DATE
echo "-------------------$(date +%F_%T) start ---------------" >>${BAK_DIR}/aggrega
te_backup.log
for database in "${DATABASES[@]}"
do
# 执行备份命令
$MYSQL_DIR/bin/mysqldump --opt -u$DB_USER -p$DB_PWD -h$DB_HOST -P$DB_PORT ${database} > $BAK_DIR/$DATE/${database}.sql
done
echo "--- backup file created: $BAK_DIR/aggregate_backup_$DATE.tar.gz" >>${BAK_DIR}/aggregate_backup.log
# 将备份好的sql脚本压缩到aggregate_backup_yyyyMMdd.tar.gz
tar -czf $BAK_DIR/aggregate_backup_$DATE.tar.gz $BAK_DIR/$DATE
# 压缩后,删除压缩前的备份文件和目录
rm -f $BAK_DIR/$DATE/*
rmdir $BAK_DIR/$DATE
# 至此备份操作已完成,下面的脚本命令用于清理之前备份的旧文件,以释放磁盘空间
# 遍历备份目录下的压缩文件
LIST=$(ls ${
BAK_DIR}/aggregate_backup_*)
# 获取10天前的时间,用于作比较,早于该时间的文件将删除
SECONDS=$(date -d "$(date +%F) -${DEL_DAY} days" +%s) for index in ${LIST} do # 对文件名进行格式化,取命名末尾的时间,格式如 20200902 timeString=$(echo ${
index} | egrep -o "?[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]") if [ -n "$timeString" ]
then
indexDate=${timeString//./-}
indexSecond=$( date -d ${
indexDate} +%s )
# 与当天的时间做对比,把早于10天的备份文件删除
if [ $(( $SECONDS- $indexSecond )) -gt 0 ]
then
rm -f $index
echo "---- deleted old backup file : $index " >>${BAK_DIR}/aggregate_backup.log
fi
fi
done
echo "-------------------$(date +%F_%T) end ---------------" >>${BAK_DIR}/aggregate_backup.log
注意:如果是Windows文件直接传到Linux上需要转换格式
sed -i 's/\r$//' aggregateBak.sh
二、给脚本赋执行权限
chmod 777 aggregateBak.sh
三、 利用Linux系统的crontab定时任务,去实现固定时间自动执行脚本
crontab -e #编辑
#输入以下代码
00 03 * * * /data/mysql_aggregate/aggregateBak.sh#在 03:00 a.m 运行
:wq #保存退出
service crond restart #重启服务,如果是ubuntu系统,则是cron
备份是个好习惯,一个不会让你赔钱的习惯
版权声明
本文为[白云碎里一蓑舟]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_49513507/article/details/123580181
边栏推荐
- Function executes only the once function for the first time
- YARN线上动态资源调优
- Tensorflow & pytorch common error reporting
- Tensorflow Download
- Oracle alarm log alert Chinese trace and trace files
- Modify the Jupiter notebook style
- JS 烧脑面试题大赏
- 专题测试05·二重积分【李艳芳全程班】
- Solution of discarding evaluate function in surprise Library
- Using Jupiter notebook in virtual environment
猜你喜欢
随机推荐
低频量化之明日涨停预测
STM32学习记录0007——新建工程(基于寄存器版)
Port occupied 1
JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
[code analysis (7)] communication efficient learning of deep networks from decentralized data
Android interview theme collection
Decentralized Collaborative Learning Framework for Next POI Recommendation
大专的我,闭关苦学 56 天,含泪拿下阿里 offer,五轮面试,六个小时灵魂拷问
[code analysis (1)] communication efficient learning of deep networks from decentralized data
go 语言 数组,字符串,切片
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
蓝绿发布、滚动发布、灰度发布,有什么区别?
函数只执行第一次的执行一次 once函数
Express ② (routage)
Problems encountered in the project (V) understanding of operating excel interface poi
神经元与神经网络
3300万IOPS、39微秒延迟、碳足迹认证,谁在认真搞事情?
Scientists say Australian plan to cull up to 10,000 wild horses doesn’t go far enough
Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)
Neuron and neural network








