Skip to content

Commit

Permalink
Update snapshot.php
Browse files Browse the repository at this point in the history
Add `duration` option to automatically delete old snapshots
  • Loading branch information
jbelien committed Dec 4, 2017
1 parent e867bde commit 8a8b730
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ applicationKey: <ovh_application_key>
applicationSecret: <ovh_application_secret>
consumerKey: <ovh_consumer_key>
duration: <date-interval>
projects:
- id: "<project-1-id>"
instances:
Expand All @@ -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 : <http://php.net/manual/en/dateinterval.construct.php>

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`
Expand Down
19 changes: 19 additions & 0 deletions snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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').')'
Expand Down

0 comments on commit 8a8b730

Please sign in to comment.