首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于递归sql元数据备份的Shell脚本

问用于递归sql元数据备份的Shell脚本
EN

Stack Overflow用户
提问于 2015-12-19 20:04:58
回答 1查看 60关注 0票数 1

我每天都在编写一个shell脚本来自动化sql元数据备份。我被夹在中间。我的方法是每天备份sql元数据,删除旧备份,为安全起见保留最后3个备份。请用命令帮助我删除以前的备份,保留最后3个备份。

代码语言:javascript
复制
FILE= mysql_metadata_backup_$( date '+%Y-%m-%d_%H-%M-%S' ).sql
mysqldump -u userID --all-databases > ${FILE}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-19 20:50:02

我想您可以使用bash参数扩展来实现您的目标。

将下面的bash脚本放在包含备份的文件夹中。

代码语言:javascript
复制
#!/bin/bash
file_list=($( ls -t mysql_metadata_backup*))
# The '-t' parameter of the 'ls' sorts the output according to modification time with newest displayed first.
rm $( echo ${file_list[@]:3})
# The ':3' does the magic slicing the array from the index 3
# All the files except the newest three are passed to the "rm" command.

运行它将删除所有备份,除了最新的三。

也许您可以考虑添加一个cron作业来实现自动化。

参考:

Bash参数展开

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34374853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档