Archiving files older than 7 days using Cronjob
Most of us
know how to zip files manually from the command prompt. Before going how to
automate this zipping of files in a particular directory, lets brush up how do
we zip from command prompt.
If the files in one directory to be zipped, the command is as follows:
zip <<desired file
name>>.zip <<files to be zipped>>
For Ex:-
zip
compressed_file.zip *.log
From the
above command, compressed_file.zip is the desired filename and *.log represents
all the log files in the current directory.
In order to zip files, we have to login to Unix and run this command whenever there is a need for this operation. It would be really handy if this job is automated by cron.
Here comes the pseudo idea for making this operation automated. Let’s start to create a Shell script and then incorporate this in cron.
SHELL
SCRIPT FOR ZIPPING
Open command
prompt and navigate to the directory where you want to create the script.
Create a file using the below command.
$vi
automated_script.sh
And once the
file is opened, insert the following lines in the file.
#!/bin/sh
set -x
zip
/tmp/compressed_logfiles/compressed-log-`date '+%Y%m%d-%H%M%S'`.zip `find .
-name '*.log' -mtime +7`
rm `find .
-name 'ods*.log' -mtime +7`
Little bit
confused, let me explain one by one.
· Starting from the first, as
usual, ZIP command syntax, starts with the keyword ZIP.
·
The
path and name of the file where the zipped file has to be created.
·
Finding
all the files which are older than 7 days. These files will be zipped.
Since we are
automating this job by cron, it will call this script and zips the selected files.
There would be a chance of replacing the previous zipped file. In order to
avoid this, I have appended the zipped file with timestamp. Hence, each and
every time there will be no chance of replacing the older file.
EDITING
CRONJOB
Login to the
user and open the crontab file by the following command.
$crontab –e
# The
crontab gets over-written with every install, any changes / additions to
crontab will be lost
# m h dom mon dow
command
0 8 * * 3 cd
/var/log/crlods/;/bin/sh automated_script.sh
>>/var/log/crlods/cron_alc.log
--------
--------
--field allowed values
--minute 0-59
--hour 0-23
--day of month 1-31
--month 1-12 (or names, see below)
--day of week 0-7 (0 or 7 is Sun, or use names)
The above
cron job will run on every Tuesday at 8:00 AM.
Please make
sure of the file permissions before running the job in cron.
Hope this clarifies all the points below:
- Basic shell script file.
- Automated file archive via cron job.
- Appending the file with time stamp.
- Find command using mtime attribute which finds the older log files.
Now the ball is in your court. Explore yourself and enjoy!
Comments
Post a Comment