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
- Meaning: This 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
- 2 is the file descriptor for Standard Error
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
.