I’m creating backup of my database every day.
For hard disk saving how can I delete files older than 2 weeks AND (day of month) % 14 != 0 from shell script?
So I’m looking for command like find / -mtime +14 -exec rm {} \;, with addition of not deleteting files created in 14’th or 28’th day of any month.
I’d like to have daily backups for last 2 weeks and once every 2 weeks(almost) afterwards?
PHP code in loop would be:
$mtime = filemtime($file); // Last modified date of file (created)
$day_mtime = date('j', $mtime); // Day of month (1-31)
$two_weeks_ago = time() - 60 * 60 * 24 * 14;
if ($mtime < $two_weeks_ago && $day_mtime % 14 != 0) {
// Delete file older than 2 weeks
// AND not modified not 14'th nor 28'th day of month
}
This is the bash equivalent. A couple of point to note when moving from php to bash:
$means “the value of…” somtimeis the variable and$mtimeis the value of the variable$(...)means run the command in the parenthesis and capture with its outputSo,
$(stat -c %Y $file)is replaced with the output ofstat -c %Y $fileThere can be no spaces around the assignment operators
a=bworks,a = bdoes not