-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackupall.php
88 lines (85 loc) · 2.87 KB
/
backupall.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
<?PHP
include "/var/www/html/data/config.inc.php";
function getBetween($content, $start, $end)
{
$r = explode($start, $content);
if (isset($r[1]))
{
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
$db_handle = mysqli_connect($DBServer, $DBUser, $DBPassword);
$db_found = mysqli_select_db($db_handle, $DBName);
$SQL = "select * from devices order by id asc";
$result = mysqli_query($db_handle, $SQL);
$errorcount = 0;
while ($db_field = mysqli_fetch_assoc($result))
{
$id = $db_field['id'];
$ip = $db_field['ip'];
$password = $db_field['password'];
$name = $db_field['name'];
$savename = preg_replace('/\s+/', '_', $name);
$savename = preg_replace('/[^A-Za-z0-9\-]/', '', $savename);
if (!file_exists('data/backups/' . $savename))
{
$oldmask = umask(0);
mkdir('data/backups/' . $savename, 0777, true);
umask($oldmask);
}
$backupurl = "http://admin:" . $password . "@" . $ip . "/dl";
$date = date('Y-m-d H:i:s');
$savedate = preg_replace('/\s+/', '_', $date);
$savedate = preg_replace('/[^A-Za-z0-9\-]/', '', $savedate);
$url = 'http://' . $ip . '/cm?cmnd=status%202&user=admin&password=' . $password;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$version = "'" . getBetween($data, '"Version":"', '"') . "'";
$saveto = "data/backups/" . $savename . "/" . $savedate . ".dmp";
$fp = fopen($saveto, 'w+');
if ($fp === false)
{
throw new Exception('Could not open: ' . $saveto);
}
$ch = curl_init($backupurl);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_exec($ch);
if (curl_errno($ch))
{
throw new Exception(curl_error($ch));
}
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
if ($statusCode == 200)
{
$directory = "data/backups/" . $savename . "/";
// Initialize filecount variavle
$filecount = 0;
$files2 = glob($directory . "*");
if ($files2)
{
$noofbackups = count($files2);
#echo $noofbackups;
}
$sql2 = "UPDATE devices SET version = $version, lastbackup = '$date', noofbackups = '$noofbackups' WHERE id = '$id'";
if (mysqli_query($db_handle, $sql2))
{
}
else
{
$errorcount = $errorcount + 1;
}
}
else
{
}
}
?>