Linux - EnterpriseThis forum is for all items relating to using Linux in the Enterprise.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to automate backup scripts and I am able to create date specifc directories by using the string:
mkdir /hotbackup/$(date +%m%d%Y)
This creates a directory named /hotbackup/10272004 which is exactly what I want. The issue I am having is I can't figure out how to create/remove a directory from a previous day. I have tried
rm /hotbackup/$(date +%m%d%Y)-1
rm /hotbackup/$(date-1 +%m%d%Y)
rm /hotbackup/$(date +%m%d-1%Y)
and they all fail.
Any help would be greatly appreciated.
Thank you.
You could use:
today=`date +%d`
yesterday=$(($today-1))
Note that you will also need to take into account the 1st of each month, especially in January.
Personally I like to use the unix time (%s) which is the number of seconds since 1970, this way you only need to know the number of seconds in a day etc and not wory about the archaic - near imposible to compute - gregorian calendar.
Distribution: OpenBSD 4.6, OS X 10.6.2, CentOS 4 & 5
Posts: 3,660
Rep:
If you don't do the time since epoch, you could use printf and a lot of manual calendar definition. I did this once in PERL and the results were good, but it takes a lot of thought with different number of days per month, roll over from one month to the next, one year to the next, daylight time, etc, etc... It seems simple until you start thinking about it...
On second thought, that time since epoch idea isn't looking so bad
If you are open to the idea of using perl then there is a "parsedate" function which is pretty good at taking a human readable date, changing it into the seconds since the epoch which can then be modified and turned back into a time using the localtime function.
Normally, you use date in the file name so you can look at it and know when it was created. If you want to do something to that file based on it's creation date or other options, the find tool has several options ( man find ) like mtime , atime , ctime .
Here is an example where the backups and logs which are older than 3 months are deleted.
Edit: Guess I should make the err messages go to a separate file.... 2> /mnt/backup/${filename}err.log
Code:
#!/bin/bash
filename=`date '+%m%d%y'`
/bin/tar -cvzf /mnt/backup/${filename}.tar.gz . \
> /mnt/backup/${filename}.log \
2> /mnt/backup/${filename}err.log
#Delete old files with the following command
find /mnt/backup -type f -name '*.gz' , -name '*.log' , \
-mtime +90 -exec rm {} \;
use the perl module Date::Manip, and create a script that takes in the number of days u want to go back, calculate the date and return it to u. All u need to do then is to call this script and pass it the number of days to go back...... your job is done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.