How to perform date calculations in Shell Scripting?

I have a shell script that runs every night to back up my database of EC2 and html sites to S3, and when it supports folders up, it adds a date to it to make browsing easier. But I want him to also be able to delete the corresponding folders for backup 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) 
+6
source share
2 answers

You can use the -d flag for the date command:

 -d, --date=STRING display time described by STRING, not 'now' 

So, just change your date variable to:

 DATE=`date +%m%d%Y -d "3 days ago"` 
+7
source

Why don't you use directory modification time? Then you can just find them with find. For exmaple:

 find backups -maxdepth 1 -mtime 3 
+2
source

Source: https://habr.com/ru/post/890957/


All Articles