diff --git a/README.md b/README.md index 7b20233..99e6911 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ applicationKey: applicationSecret: consumerKey: +duration: + projects: - id: "" instances: @@ -49,6 +51,15 @@ projects: ... ``` +#### Configure `duration` + +To determine after how many days/weeks/months/... you want snapshots to be delete, use `duration` option. +This option uses PHP `DateInterval` format : + +The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T. + +Here are some simple examples. Two days is `P2D`. Two seconds is `PT2S`. Six years and five minutes is `P6YT5M`. + ## Run `php snapshot.php` diff --git a/snapshot.php b/snapshot.php index ebb8fe6..b5fa4de 100644 --- a/snapshot.php +++ b/snapshot.php @@ -12,6 +12,25 @@ echo '--------------------------------------------------'.PHP_EOL; echo 'PROJECT: '.$p['id'].PHP_EOL; + if (isset($config['duration']) && !empty($config['duration'])) { + $list = $ovh->get('/cloud/project/'.$p['id'].'/snapshot'); + + $time = new DateTime(); + $time->sub(new DateInterval($config['duration'])); + echo 'Delete snapshots older than '.$time->format('Y-m-d H:i:s').PHP_EOL; + + $count = 0; + foreach ($list as $snapshot) { + $snapshot_time = new DateTime($snapshot['creationDate']); + if ($snapshot_time < $time) { + $ovh->delete('/cloud/project/'.$p['id'].'/snapshot/'.$snapshot['id']); + echo 'Delete snapshot "'.$snapshot['name'].'" ('.$snapshot_time->format('Y-m-d H:i:s').')'.PHP_EOL; + $count++; + } + } + echo sprintf('%d deleted snapshot(s)', $count).PHP_EOL; + } + foreach ($p['instances'] as $instance) { $snapshot = $ovh->post('/cloud/project/'.$p['id'].'/instance/'.$instance['id'].'/snapshot', array( 'snapshotName' => $instance['name'].' ('.date('Y-m-d H:i:s').')'