Auto Delete Backup Files Using Shell Script Cron Job

Introduction

In this shell programming example, I am going to show you how to delete backup files automatically using shell script cron job. So, I will create a shell script in Unix programming and setup a cron job to automate the delete process.

This automatic job will save your manual efforts of deleting files from the folders. You don’t need to remember everyday at what time you have to connect to server, and you have to execute the command to delete the unnecessary files or folder.

For example, you may want to keep last five backups of your web site files and database files and rest of the backup files you want to delete. This will also make sure that you do not run out of disk space due to too many backup files.

auto delete backups using shell script cron job

Related Post:

Prerequisites

Knowledge of Shell Programming, Unix Operating System

Auto Delete Shell Script

Now I will write a shell script (delete-backup.sh) under the /home/<username>/scripts folder, where <username> should be replaced by your username.

Create a directory scripts under your user profile (/home/<username>) by executing the command:

$ mkdir scripts

Let’s create a shell script delete-backup.sh under the scripts folder:

$ vi scripts/delete-backup.sh

Now press i to change as INSERT mode.

Now copy and paste the following code into the script file.

#!/bin/bash

backup_path="/home/<username>/backup"

# delete all but 5 recent database backups (files having .sql extension) in backup folder.
find $backup_path -maxdepth 1 -name "*.sql" -type f | xargs -x ls -t | awk 'NR>5' | xargs -L1 rm -f

# delete all but 5 recent database backups (files having .zip extension) in backup folder.
find $backup_path -maxdepth 1 -name "*.zip" -type f | xargs -x ls -t | awk 'NR>5' | xargs -L1 rm -f

Now escape from the INSERT mode from the editor by pressing escape key and type :wq to save the script file.

In the above script, the backup path from where you want to delete your backup files. Make sure you have created the directory backup under your user profile:

backup_path="/home/<username>/backup"

The following line in the above script file deletes all but keeps last 5 backup files:

find $backup_path -maxdepth 1 -name "*.sql" -type f | xargs -x ls -t | awk 'NR>5' | xargs -L1 rm -f

I have used find command to find the required file in the $backup_path.

The maxdepth is used in find for limiting search to a specific directory. So it will descend at most the levels (non-negative integer) of directories below starting point. maxdepth 1 tells find not to descend into subdirectories. If you want subdirectories to be included then omit this option.

-name option is passed to find the file name and here I have used *.sql or *.zip. So, any files that are having extension .sql or .zip will be deleted.

-type f determines the regular file.

xargs takes the input and converts it into a command argument for another command.

ls -t sort files by modification time, newest first, followed by if count is greater than 5 files.

rm -f forcefully deletes file, ignore if non-existent and never prompt.

Setup Auto Delete Cron Job

Now I will setup a cron job to automate the backup process.

Open the crontab using the command:

$ crontab -e

And put the following line in it:

05 12 * * * /home/<username>/scripts/delete-backup.sh

Now escape from the INSERT mode from the editor by pressing escape key and type :wq to save the file.

The above line means that the delete-backup.sh file will be executed at 12:05 am every day. You can adjust the time according to your needs.

Note that you don’t need sudo while you are opening crontab because you are running this shell script using your username not as a root user.

Even you can run the above shell script to delete the backup files leaving last 5 files in the backup folder.

Shell Script Permission

Now you can change the execution permission of your shell script because by default the shell script does not have the execute permission.

$ chmod 760 scripts/delete-backup.sh

Testing Auto Delete Script

Execute the following command to run the shell script and deletion will be done:

$ ./scripts/delete-backup.sh

You will see that files got deleted from backup folder.

Source Code

Download

Leave a Reply

Your email address will not be published.