当前位置:网站首页>linux定时备份数据库脚本
linux定时备份数据库脚本
2022-07-23 09:29:00 【杰拉德·皮克·谢】
创建脚本
- 创建脚本 (意思在文件中有备注)
vim dbName.sh
- vim 使用
i插入:输入命令
#!/bin/bash
#备份目录
BACKUP=/home/mysqlBackup/db_name
#获取当前时间
DATATIME=$(date +'%Y-%m-%d_%H%M%S')
#打印时间
echo $DATATIME
#数据库地址
HOST=localhost
#数据库用户名
DB_USER=root
#数据库密码
DB_PW=123456
#备份数据库名
DATABASE=db_name
#创建备份目录,如果不存在就创建
[ ! -d "${BACKUP}/${DATATIME}/${DATATIME}" ] && mkdir -p "${BACKUP}/${DATATIME}"
#备份数据库
mysqldump -u${
DB_USER} -p${
DB_PW} --host=${
HOST} -q -R --databases ${
DATABASE} | gzip > ${
BACKUP}/${
DATETIME}/$DATABASE.sql.gz
#将文件处理成tar.gz
cd ${
BACKUP}
tar -zcvf $DATABASE.tar.gz ${
DATATIME}
#删除对应的备份目录
rm -rf ${
BACKUP}/${
DATATIME}
#删除10天前的备份文件
find ${
BACKUP} -atime +30 -name "+.tar.gz" -exec rm -rf {
} \;
echo "备份数据库${DATABASE} 成功~"
设置定时
- 输入命令
crontab -e/ 路径地址
1.用数值表示 时间信息
00 02 * * * 备份文件
2.利用特殊符号表示时间信息
* * * * * 备份文件
/分钟 /小时 /天 /月 /周
=========================================
PS:定时任务最短执行的周期为 每分钟
*/5 */ 6 */3 */1 */2
每隔5分钟 每隔6小时 每隔3日 每隔1月 每隔2周
=========================================
其它 写法:
01-05 02 * * * 每2日的01、02、03、04、05 执行一遍
指定不连续的时间范围:
00 14,20 * * * * 每天14点,20点执行一次
边栏推荐
- Using shell script to block IP with high scanning frequency
- 【测试平台开发】21. 完成发送接口请求显示响应头信息
- 运维高级作业02
- R language practical application case: drawing part (III) - drawing of multiple combination patterns
- FFmpeg 1 - 概览/安装
- Is it risky and safe to open a mobile stock account?
- 【刷题记录】19. 删除链表的倒数第 N 个结点
- Due to resource constraints, the namenode fails to start with an error unable to create new native thread
- [pyqt5 installation and use]
- Solve a series of problems in using Bert encoder
猜你喜欢
![Looking for peak [Abstract dichotomy exercise]](/img/99/122e79784f0f07120680d2cbcf89da.png)
Looking for peak [Abstract dichotomy exercise]

转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。

固定资产管理系统哪家好?固定资产管理平台有哪些?

Program design of dot matrix Chinese character display of basic 51 single chip microcomputer

21 - 二叉树的垂直遍历

Using shell script to block IP with high scanning frequency

Typora图床配置详细教程

利用shell脚本实现封禁扫描频率过高的ip

優化華為雲服務器采用Key登陸

基于EFR32MG24的AI 加速度姿势识别体验
随机推荐
(重链剖分)魔法树
运维高级作业03
mysql 之general_log日志
[applet automation minium] III. element positioning - use of wxss selector
Uni app knowledge points and records of problems and solutions encountered in the project
CSDN writing method (II)
Chapter 3 complex query
How can manual testing turn to automated testing? Byte 5 years of automation experience talk about
JS calendar style pie chart statistics plug-in
Solve a series of problems in using Bert encoder
FastAPI应用加入Nacos
C language implements StrCmp, strstr, strcat, strcpy
Opencv calculation outsourcing rectangle
[array & String & Macro exercise]
[applet automation minium] i. framework introduction and environment construction
转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。
The win11 installation system prompts that VirtualBox is incompatible and needs to uninstall the solution of virtual, but the uninstall list cannot find the solution of virtual
Quick introduction to PKI system
直播课堂系统01-数据库表设计
Generate order number