forked from PetaByet/cdp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.php
More file actions
146 lines (131 loc) · 4.62 KB
/
cron.php
File metadata and controls
146 lines (131 loc) · 4.62 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
<?php
if (php_sapi_name() != 'cli') {
die();
}
if (!isset($argv[1])) {
die();
}
require('config.php');
$starttime = time();
$log = 'Backup job (' . $argv[1] . ') started' . PHP_EOL;
$backups = json_decode(file_get_contents($config['path'].'/includes/db-backups.json'), true);
$backupjobs = json_decode(file_get_contents($config['path'].'/includes/db-backupjobs.json'), true);
$backupservers = json_decode(file_get_contents($config['path'].'/includes/db-backupservers.json'), true);
function exitcron()
{
global $log;
global $config;
if ($config['sendnotification'] && filter_var($config['adminemail'], FILTER_VALIDATE_EMAIL)) {
mail($config['adminemail'], 'CDP.me backup report', wordwrap($log, 70));
}
else {
echo $log;
}
die();
}
function GetJobDetails($jobid)
{
global $backupjobs;
foreach ($backupjobs as $backupjob) {
if ($backupjob['id'] == $jobid) {
return $backupjob;
}
}
return false;
}
function GetServerDetails($host)
{
global $backupservers;
foreach ($backupservers as $backupserver) {
if ($backupserver['host'] == $host) {
return $backupserver;
}
}
return false;
}
$backupjob = GetJobDetails($argv[1]);
if (!$backupjob) {
$log .= 'Job ID does not exist' . PHP_EOL;
exitcron();
}
$backupserver = GetServerDetails($backupjob['source']);
if (!$backupserver) {
$log .= 'Server does not exist' . PHP_EOL;
exitcron();
}
set_include_path($config['path'].'/phpseclib');
include('Net/SSH2.php');
include('Net/SFTP.php');
include('Crypt/RSA.php');
$ssh = new Net_SSH2($backupserver['host'], $backupserver['port']);
$sftp = new Net_SFTP($backupserver['host'], $backupserver['port']);
if ($backupserver['authtype'] == 'password') {
if (!$ssh->login($backupserver['username'], $backupserver['password'])) {
$log .= 'SSH password login failed' . PHP_EOL;
exitcron();
}
if (!$sftp->login($backupserver['username'], $backupserver['password'])) {
$log .= 'SFTP password login failed' . PHP_EOL;
exitcron();
}
} elseif ($backupserver['authtype'] == 'key') {
$serverkey = explode(' ', $backupserver['password']);
$key = new Crypt_RSA();
if (isset($serverkey[1])){
$key->setPassword($serverkey[1]);
}
$key->loadKey(file_get_contents($serverkey[0]));
if (!$ssh->login($backupserver['username'], $key)) {
$log .= 'SSH key login failed' . PHP_EOL;
exitcron();
}
if (!$sftp->login($backupserver['username'], $key)) {
$log .= 'SFTP key login failed' . PHP_EOL;
exitcron();
}
} else {
$log .= 'SSH login failed' . PHP_EOL;
exitcron();
}
$dirname = 'cdpme-' . date("Y-m-d-H-i-s") . '-' . $backupjob['id'];
$log .= $ssh->exec('mkdir /tmp/' . $dirname) . PHP_EOL;
$log .= $ssh->exec('tar -zcvf ' . $dirname . '.tar.gz ' . $backupjob['directory']) . PHP_EOL;
$log .= $ssh->exec('mv ' . $dirname . '.tar.gz /tmp/' . $dirname) . PHP_EOL;
$log .= $sftp->chdir('/tmp/' . $dirname) . PHP_EOL;
$sftpfiletransfer = $sftp->get($dirname . '.tar.gz', $dirname . '.tar.gz') . PHP_EOL;
if (!$sftpfiletransfer) {
$log .= 'File transfer failed' . PHP_EOL;
exitcron();
} else {
$log .= $sftpfiletransfer;
}
$log .= $ssh->exec('rm -rf /tmp/' . $dirname) . PHP_EOL;
$log .= rename($dirname . '.tar.gz', $config['path'].'/files/' . $dirname . '.tar.gz') . PHP_EOL;
$timetaken = time() - $starttime;
$log .= 'Backup completed in ' . $timetaken . ' seconds.' . PHP_EOL;
$backups[count($backups)] = array(
'id' => $backupjob['id'],
'file' => $dirname . '.tar.gz',
'size' => filesize($config['path'].'/files/' . $dirname . '.tar.gz'),
'time' => $starttime
);
file_put_contents($config['path'].'/includes/db-backups.json', json_encode($backups));
if (isset($backupjob['expiry'])) {
$backups = json_decode(file_get_contents($config['path'].'/includes/db-backups.json'), true);
$log .= 'Processing backup auto-delete'. PHP_EOL;
$expirecutofftime = time() - 86400 * $backupjob['expiry'];
foreach ($backups as $backupkey => $backup) {
if ($backup['id'] == $backupjob['id'] && $backup['time'] < $expirecutofftime) {
if (file_exists($config['path'] . '/files/' . $backup['file']) && unlink($config['path'] . '/files/' . $backup['file'])) {
unset($backups[$backupkey]);
$log .= 'Successfully removed '.$backup['file'].PHP_EOL;
}
else {
$log .= 'Error removing '.$backup['file'].PHP_EOL;
}
}
}
file_put_contents($config['path'].'/includes/db-backups.json', json_encode($backups));
}
exitcron();
?>