Using date in a shell script

I have a backup script that backs up my /var/log/ folder daily. The backup names the tar file date-logs.tgz and works just fine. If I ran it today it would look like 28-Febuary-2013-logs.tgz.

What has me scratching my head is now I want to perform the backup monthly instead of daily. So I was thinking of having the crontab run the script on the beginning of the month however the date would put the current month in as the month when I would prefer the previous month in the file name.

So my monthly script would run tomorrow March 1st and the filename would be Febuary-2013-logs.tgz.

Here is an excerpt from the script:
Code:
#!/bin/sh
present=`date +'%d-%B-%Y'`

/usr/bin/tar cvzf /usr/LogTemp/$present-logs.tgz /var/log

Can anyone point me in the right direction?
 
Try adding -v -1m to the date command. That should subtract one month of the current date before calculating/formatting.
 
The -v flag to date() is where to look.

Code:
date -v-1d +%d-%B-%Y

would give you yesterday's date, whereas

Code:
date -v-1m +%B-%Y

gives you last month.
 
Code:
#!/bin/sh
/bin/date > /home/myhone/date.txt
#
for m in $(awk '{print $2}' /home/my-home/date.txt);do set m=$m;done
#
for d in $(awk '{print $3}' /home/my-home/date.txt);do mkdir /home/my-home/$m-$d && cd /home/my-home/$m-$d;done
#
 
Code:
#!/bin/sh
/bin/date > /home/myhone/date.txt
#
for m in $(awk '{print $2}' /home/my-home/date.txt);do set m=$m;done
#
for d in $(awk '{print $3}' /home/my-home/date.txt);do mkdir /home/my-home/$m-$d && cd /home/my-home/$m-$d;done
#
No. This does the same as your code, but is much easier to read and maintain:
Code:
today=$(date +%b-%d)
mkdir -p ~/$today
cd ~/$today
If you need to have date.txt add
Code:
echo $today > ~/date.txt
 
Hi, can someone give me that script? I have an idea how to use it for my private server in case of emergency. Thanks a lot.
 
Back
Top