Automatically delete archives older than two days using Shell Script


Reading time: 20 minutes | Coding time: 5 minutes

As with time, our database size goes on increasing and our system becomes slower and slower, so it is important to understand the concept of Data Purging and using it to delete old archives.

Data Purging

It refers to the process of freeing up space in database or of deleting obsolete files from the database that is no longer required by the system or user. The purging process can be based on age or type of the stored data.

Key benefits of Data Purging :


1. Improved Performance : Data Purging will help us to achieve faster processing time, decreased backups, speedier lookups and will result in more disk space. All this will be because there will be fewer records to process.

2. Data Management : Less the data, easy to manage...! On an average data duplication takes 25% of system resources. Purging solution allows us to identify this redundant data and helps us remove it without disrupting the end-user.

3. Reduced Backups : With data purging, we can control backup as there is no need to backup old data which is no further required.

NOTE : Purging is used for large number records and deleting is used for small number of records.

Understanding Shell :

Whenever first two bytes of a executable are #!, then the file is run by system call, so we write #!bin/bash at the starting of our shell script where bin/bash gives location of the shell.

#!bin/bash

cd command is used to change directory to specified location.

cd LOCATION

rm command is used to remove the specified file.

rm FILENAME

find command is used to search for a specific pattern and work on them simultaneously.

-mtime is used to check the modified time of the file and argument supplied to -mtime checks if file is that day old. -name is used to check for the pattern in name.

find -mtime +2 -name "*.tar"

Here, the command will search for 2 day old files as +2 is supplied to -mtime.

Shell Script for deleting archives older than two days

#!bin/bash

cd /home/kshitiz/archives

rm $(find -mtime +2 -name "*.tar")
rm $(find -mtime +2 -name "*.tar.gz")
rm $(find -mtime +2 -name "*.tar.bz2")

Scheduling above shell file in Cron Job Scheduler

First of all before scheduling as a cron job we need to make the file executable so we use the command.

chmod +x FILENAME

Here +x adds executable permissions to the file and FILENAME specified the name of file.

Use the command crontab -e to open cron file.

crontab -e

In cron file we use the syntax as :

* * * * * command(s)
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

For scheduling cron jobs write

* * */1 * * ./home/kshitiz/deleting_old_archive.sh

Code for cron job scheduler

Connect with me on LinkedIn and Twitter