Forum Moderators: coopster
I have written a PHP API that works with Amazon S3 simple storage API to automatically backup my two servers all sites and their databases and create a .tar.gz file for each server and shift it to Amazon Server.
The script creates a BUCKET (used for new folder / directory on amazon server) for each month as
monthName-YYYY
e.g
Sep-2007 for current month and will create Oct-2007
,Nov-2007 for upcoming two months respectively.
Each month folder has .tar.gz files for each server and for each day as backup is taken daily.
Now the problem with which I am stuck is that I want to keep LAST SIX MONTH backup on Amazon server and delete older backups.
for this I have written a script which works under cronjob every six month and will delete all folders / directory / bucket that is more than six months older.
I need help about counting the months difference between current month (in which the cron job runs) and the month which is used in folder / directory / bucket name as explained above.
e.g
if i have a backup of
Jan-2007
Feb-2007
and my cronjob runs today i.e in Sep, then I would want to calculate that above two backups are more than six months older and delete them.
I just need help about counting this NUMBER OF MONTHS because the year can be different too as in future the folder names will be with Jan-2008 etc
thank you very much.
The best way to handle this is to convert your dates to timestamps.
I have just responded to one similar thread of finding difference in age (years) [webmasterworld.com].
You can do the same thing for months. There seems nothing to be either than handling dates with timestamps.
Habtom
$ find /path/to/dir -mtime +190 -exec rm -r '{}'\;
could be used to recursively delete stuff.
Put it into a cron job and let it run once every month.
However, you should carefully test this stuff with an ... -exec ls -lr '{}'\; or ... -print first, so that the recursive `rm -r` will not blow away more than you want.
You may search for find+mtime+exec with Yahoo or Google for more.
Disclaimer: exercise caution, do extensive tests in a non-productive environment with no real data, and of course you are using this on your own risk.
Kind regards,
R.
I found a work around by using mktime() and a switch statement.
function IntMonth($m) {
$month = '';
switch($m) {
case "Jan":
$month = 1;
break;
case "Feb":
$month = 2;
break;
case "Mar":
$month = 3;
break;
case "Apr":
$month = 4;
break;
case "May":
$month = 5;
break;
case "Jun":
$month = 6;
break;
case "Jul":
$month = 7;
break;
case "Aug":
$month = 8;
break;
case "Sep":
$month = 9;
break;
case "Oct":
$month = 10;
break;
case "Nov":
$month = 11;
break;
case "Dec":
$month = 12;
break;
} //end switch
return $month;
} //end function
and
$d1=mktime(0,0,0,IntMonth(trim($dbox[0])),0,$dbox[1]);
$d2=mktime(0,0,0,date('m'),date('d'),date('Y'));
$months = floor(($d2-$d1)/2628000);
where dbox[1] had year value in it.
I tested it and it worked great.
thank you very much for your replies.