forked from ishan3350/DigitalOceanSnapshotHourlyAutomation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot_automation.php
134 lines (93 loc) · 3.85 KB
/
snapshot_automation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
//change default time zone
date_default_timezone_set('US/Eastern');
$apiUrl="https://api.digitalocean.com/v2/";
$token = "Your DigitalOcean Token";
$dropletId="Your DigitalOcean DropletID";
//listing all snapshots
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl . 'droplets/' . $dropletId . '/snapshots');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response);
if ($result) {
//print_r($result->snapshots);
//get the snapshot id
$snapshot_id= $result->snapshots[0]->id;
//get snapshot name
$snapshot_name= $result->snapshots[0]->name;
}
//echo $snapshot_id;
//echo $snapshot_name;
//get the date and time of old snapshot
$snapshot_time = strtotime($snapshot_name);
//get current date and time
$current_time = strtotime(date("Y/m/d h:i:sa"));
// check difference between current time an last snapshot time in minutes
$time_difference = ($current_time - $snapshot_time)/60;
$time_difference = round($time_difference);
//echo $time_difference;
// if snapshot id is empty that means no snapshot exist
if(empty($snapshot_id))
{
// no snapshots exist
//creating new snapshot
echo "no snapshots found. Creating new snapshot";
$data = array("type" => "snapshot", "name" => date("Y/m/d h:i:sa"));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl . 'droplets/' . $dropletId . '/actions');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $token
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_exec($ch);
curl_close($ch);
return;
}
// if time different is greater than or equal to 60 minutes create new snapshot and delete old snapshot
if($time_difference>=60)
{
// if it has been already an hour create a new snapshot and delete old snapshot
//creating new snapshot
$data = array("type" => "snapshot", "name" => date("Y/m/d h:i:sa"));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl . 'droplets/' . $dropletId . '/actions');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $token
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_exec($ch);
curl_close($ch);
//deleting old snapshot
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl . 'images/' . $snapshot_id);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $token
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_exec($ch);
curl_close($ch);
}
else
{
echo "Snapshot is already taken ".$time_difference. " minutes before";
return;
}
?>