Shell-/Bash-Script to delete old backup files by name and specific pattern
2
votes
2
answers
2491
views
every our backup files of a database are created. The files are named like this:
`
prod20210528_1200.sql.gz
pattern: proddate +\%Y%m%d_%H%M
``
The pattern could be adjusted if needed.
I would like to have a script that:
- keeps all backups for the last x (e.g. 3) days
- for backups older than x (e.g. 3) days only the backup from time 00:00 shall be kept
- for backups older than y (e.g. 14) days only one file per week (monday) shall be kept
- for backups older than z days (e.g. 90) only one file per month (1st of each month) shall be kept
- the script should rather use the filename instead of the date (created) information of the file, if that it possible
- the script should run every day
Unfortunately, I have very little knowledge of the shell-/bash-script language.
I would do something like this:
`````
if (file today - (x + 1))
{
if (%H_of_file != 00 AND %M_of_file != 00)
{
delete file
}
}
if (file today - (y + 1))
{
if (file != Monday)
{
delete file
}
}
if (file today - (z + 1))
{
if (%m_of_file != 01)
{
delete file
}
}
Does this makes any sense for you?
Thank you very much!
All the best,
Phantom
Asked by Phantom
(143 rep)
May 28, 2021, 05:25 PM
Last activity: Jul 26, 2025, 04:04 AM
Last activity: Jul 26, 2025, 04:04 AM