如何在 Shell 脚本中进行日期计算?
我有一个每天晚上运行的 shell 脚本,用于将我的 EC2 站点数据库和 html 备份到 S3,并且当它备份文件夹时,它会在其中附加日期以便于查看。但我希望它也能够删除三天前的相关备份文件夹。如何计算得到 3 天前的日期?
#!/bin/bash
DATE=`date +%m%d%Y`
s3cmd put -r /var/lib/mysql/mydb/ s3://mybucket/mydb-$DATE/
s3cmd put -r /home/ec2-user/public_html/ s3://mybucket/public_html-$DATE/
s3cmd del -r s3://mybucket/mydb-(date 3 days ago)
I have a shell script that runs every night to backup my EC2 sites database and html to S3, and when it backs the folders up, it appends the date to it for easier viewing. But I want it to also be able to delete the relevant backup folders from 3 days before. How can I do the calculations to get the date 3 days ago?
#!/bin/bash
DATE=`date +%m%d%Y`
s3cmd put -r /var/lib/mysql/mydb/ s3://mybucket/mydb-$DATE/
s3cmd put -r /home/ec2-user/public_html/ s3://mybucket/public_html-$DATE/
s3cmd del -r s3://mybucket/mydb-(date 3 days ago)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以对 date 命令使用 -d 标志:
因此,只需将日期变量更改为:
You can use the -d flag for the date command:
So, just change your date variable to:
为什么不使用目录的修改时间?然后你就可以用 find 来搜索它们。例如:
Why don't you use the modification time of the directories? Then you can just search for them with find. For exmaple:
BSD / Mac 上有所不同。您需要使用
-v
选项:It's different on BSD / Mac. You need to use the
-v
option: