Skip to content

Commit

Permalink
Update snapshot.php
Browse files Browse the repository at this point in the history
  • Loading branch information
jbelien committed Feb 26, 2018
1 parent 0d34897 commit ddefd66
Showing 1 changed file with 87 additions and 54 deletions.
141 changes: 87 additions & 54 deletions snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,101 +26,131 @@
echo count($p['instances']).' INSTANCE(S)'.PHP_EOL;
echo count($p['volumes']).' VOLUME(S)'.PHP_EOL;

try {
if (isset($config['duration']) && !empty($config['duration'])) {
$time = new DateTime();
$time->sub(new DateInterval($config['duration']));
echo 'Delete snapshots older than '.$time->format('Y-m-d H:i:s').PHP_EOL;

if ($dryrun !== true) {
$log->debug('PROJECT: {project} - Delete snapshots older than '.$time->format('Y-m-d H:i:s'), [
'project' => $p['id'],
]);
}
if (isset($config['duration']) && !empty($config['duration'])) {
$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;
if ($dryrun !== true) {
$log->debug('PROJECT: {project} - Delete snapshots older than '.$time->format('Y-m-d H:i:s'), [
'project' => $p['id'],
]);
}

$count = 0;

$list = $ovh->get('/cloud/project/'.$p['id'].'/snapshot');
foreach ($list as $snapshot) {
$snapshot_time = new DateTime($snapshot['creationDate']);
if ($snapshot_time < $time) {
if ($dryrun !== true) {
$list = $ovh->get('/cloud/project/'.$p['id'].'/snapshot');
foreach ($list as $snapshot) {
$snapshot_time = new DateTime($snapshot['creationDate']);
if ($snapshot_time < $time) {
if ($dryrun !== true) {
try {
$url = '/cloud/project/'.$p['id'].'/snapshot/'.$snapshot['id'];
$delete = $ovh->delete($url);

$log->debug('PROJECT: {project} - Delete snapshot "'.$snapshot['name'].'" ('.$snapshot_time->format('Y-m-d H:i:s').')', [
$log->debug('PROJECT: {project} - Deleted snapshot "'.$snapshot['name'].'" ('.$snapshot_time->format('Y-m-d H:i:s').')', [
'project' => $p['id'],
'snapshot' => $snapshot,
'delete' => $delete,
'url' => $url,
]);
} catch (ServerException $exception) {
$log->error($exception->getMessage(), [
'project' => $p['id'],
'snapshot' => $snapshot,
'delete' => $delete,
'url' => $url,
'exception' => $exception,
]);
}
}

echo 'Delete snapshot "'.$snapshot['name'].'" ('.$snapshot_time->format('Y-m-d H:i:s').')'.PHP_EOL;
echo 'Delete snapshot "'.$snapshot['name'].'" ('.$snapshot_time->format('Y-m-d H:i:s').')'.PHP_EOL;

$count++;
}
$count++;
}
}

$list = $ovh->get('/cloud/project/'.$p['id'].'/volume/snapshot');
foreach ($list as $snapshot) {
$snapshot_time = new DateTime($snapshot['creationDate']);
if ($snapshot_time < $time) {
if ($dryrun !== true) {
$list = $ovh->get('/cloud/project/'.$p['id'].'/volume/snapshot');
foreach ($list as $snapshot) {
$snapshot_time = new DateTime($snapshot['creationDate']);
if ($snapshot_time < $time) {
if ($dryrun !== true) {
try {
$url = '/cloud/project/'.$p['id'].'/volume/snapshot/'.$snapshot['id'];
$delete = $ovh->delete($url);

$log->debug('PROJECT: {project} - Delete snapshot "'.$snapshot['name'].'" ('.$snapshot_time->format('Y-m-d H:i:s').')', [
$log->debug('PROJECT: {project} - Deleted snapshot "'.$snapshot['name'].'" ('.$snapshot_time->format('Y-m-d H:i:s').')', [
'project' => $p['id'],
'snapshot' => $snapshot,
'delete' => $delete,
'url' => $url,
]);
} catch (ServerException $exception) {
$log->error($exception->getMessage(), [
'project' => $p['id'],
'snapshot' => $snapshot,
'delete' => $delete,
'url' => $url,
'exception' => $exception,
]);
}
}

echo 'Delete snapshot "'.$snapshot['name'].'" ('.$snapshot_time->format('Y-m-d H:i:s').')'.PHP_EOL;
echo 'Delete snapshot "'.$snapshot['name'].'" ('.$snapshot_time->format('Y-m-d H:i:s').')'.PHP_EOL;

$count++;
}
$count++;
}

echo sprintf('%d deleted snapshot(s)', $count).PHP_EOL;
}

if ($dryrun !== true) {
$log->debug('PROJECT: {project} - Create snapshot of '.count($p['instances']).' instance(s)', [
'project' => $p['id'],
]);
}
echo sprintf('%d deleted snapshot(s)', $count).PHP_EOL;
}

if ($dryrun !== true) {
$log->debug('PROJECT: {project} - Created snapshot of '.count($p['instances']).' instance(s)', [
'project' => $p['id'],
]);
}

foreach ($p['instances'] as $instance) {
echo 'INSTANCE: '.$instance['name'].PHP_EOL;
foreach ($p['instances'] as $instance) {
echo 'INSTANCE: '.$instance['name'].PHP_EOL;

if ($dryrun !== true) {
if ($dryrun !== true) {
try {
$url = '/cloud/project/'.$p['id'].'/instance/'.$instance['id'].'/snapshot';
$snapshot = $ovh->post($url, [
'snapshotName' => $instance['name'].' ('.date('Y-m-d H:i:s').')',
]);

$log->debug('PROJECT: {project} - Create snapshot instance "'.$instance['name'].'"', [
$log->debug('PROJECT: {project} - Created snapshot instance "'.$instance['name'].'"', [
'project' => $p['id'],
'instance' => $instance,
'snapshot' => $snapshot,
'url' => $url,
]);
} catch (ServerException $exception) {
$log->error($exception->getMessage(), [
'project' => $p['id'],
'instance' => $instance,
'snapshot' => $snapshot,
'url' => $url,
'exception' => $exception,
]);
}
}
}

if ($dryrun !== true) {
$log->debug('PROJECT: {project} - Create snapshot of '.count($p['volumes']).' volume(s)', [
'project' => $p['id'],
]);
}
if ($dryrun !== true) {
$log->debug('PROJECT: {project} - Create snapshot of '.count($p['volumes']).' volume(s)', [
'project' => $p['id'],
]);
}

foreach ($p['volumes'] as $volume) {
echo 'VOLUME: '.$volume['name'].PHP_EOL;
foreach ($p['volumes'] as $volume) {
echo 'VOLUME: '.$volume['name'].PHP_EOL;

if ($dryrun !== true) {
if ($dryrun !== true) {
try {
$url = '/cloud/project/'.$p['id'].'/volume/'.$volume['id'].'/snapshot';
$snapshot = $ovh->post($url, [
'name' => $volume['name'].' ('.date('Y-m-d H:i:s').')',
Expand All @@ -132,13 +162,16 @@
'snapshot' => $snapshot,
'url' => $url,
]);
} catch (ServerException $exception) {
$log->error($exception->getMessage(), [
'project' => $p['id'],
'instance' => $instance,
'snapshot' => $snapshot,
'url' => $url,
'exception' => $exception,
]);
}
}
} catch (ServerException $exception) {
$log->error($exception->getMessage(), [
'project' => $p['id'],
'exception' => $exception,
]);
}
}

Expand Down

0 comments on commit ddefd66

Please sign in to comment.