Start a conversation

Configuring Crontab to be executed for cleaning up regular (old) backup files

Overview

You find out that the daily backup is getting exponentially bigger in terms of size. After investigating this issue, you conclude that the mysqlcluster backup is not getting deleted, so you need help with the crontab to command to schedule the cleanup.

 

Information

The following Crontab command will suffice to clean up the files of a folder:

30 3 * * * find /<folder>/BACKUP/BACKUP* -mtime +7 -exec rm -f {} \; >/dev/null 2>&1

 

Crontab Command Interpretation

You can interpret the command like the following:

30 3 * * *

  • Meaning: Every day at 03:30 am

find /<folder>/BACKUP/BACKUP* -mtime +7

  • Meaning: It looks for a folder named Backup, and all the elements named "Backup something", that has more than 7 days of being modified (time +7).

-exec rm -f {} \;

  • Meaning: The action described in this part of the command resembles the execution of a batch of files deletion (rm), but in Forced more (-f) which means without rm prompting you for confirmation. {} \; means it will find and append the found files to the end of the command rather than invoking it once per file (so that the command is run only once, if possible).

>/dev/null 2>&1

  • MeaningThis part of the command redirects the output of your program to /dev/null, being the 2>&1 section the following:
    • 2 is the file descriptor for Standard Error
    • > is for forwarding
    • & is the symbol for file descriptor (without it, the following 1 would be considered as a filename)
    • 1 is the file descriptor for Standard Output

If by executing this command, you get files removed but blank folders are still showing on the server, you might want to try the command with rm -rf instead of rm -f.

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Priyanka Bhotika

  2. Posted

Comments