-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3backups.php
More file actions
330 lines (236 loc) · 9.05 KB
/
Copy paths3backups.php
File metadata and controls
330 lines (236 loc) · 9.05 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/******************************************************************************
* Path settings
******************************************************************************/
define('DOMAINS_PATH', '/var/www/vhosts/');
define('BACKUP_PATH', "/home/backup/backups/");
define('HTTP_FOLDER', "/httpdocs");
/******************************************************************************
* S3 settings
******************************************************************************/
define('PYTHONPATH', "/usr/bin/python". ":" . "/usr/lib/python2.6");
define('S3CFG_FILE_PATH', "/root/.s3cfg");
define('S3CMD_FILE', "/usr/bin/s3cmd");
define('S3_REMOTE_PATH', "my-bucket-name/myfolder/");
/******************************************************************************
* DB settings
******************************************************************************/
define('DB_HOST', "localhost");
define('DB_USER', "admin");
// define('DB_PASS', 'password');
define('DB_PASS_SHADOW', '/etc/psa/.psa.shadow');
/******************************************************************************
* EXECUTE
******************************************************************************/
date_default_timezone_set('UTC');
$datestamp = date("Y-m-d-H-i-s", time());
$webspaces = getWebspaces();
$domains = getDomains($webspaces);
$mysqlpassword = defined('DB_PASS_SHADOW') ? trim(file_get_contents(DB_PASS_SHADOW)) : DB_PASS;
$link = new mysqli(DB_HOST, DB_USER, $mysqlpassword);
$dbs = getDatabases($link);
$tasks = array();
createBaseTasks();
createDatabasesTask($dbs, $datestamp);
createDomainsTasks($domains, $datestamp);
// Uncomment to use Files Tasks.
// This is required if you are using symlinks to a storage folder
// Since we switched to mounting (--mount), these additional tasks are not required anymore.
// createFilesTasks($domains, $datestamp);
// Uncomment to use Mail Tasks.
// createMailTasks($webspaces, $datestamp);
ksort($tasks);
saveDomainsList($domains);
echo outputShell();
exit;
//saveShell();
//execute();
function saveShell() {
$fp = fopen(BACKUP_PATH . "script.sh", 'w');
fwrite($fp, outputShell());
fclose($fp);
}
function outputShell() {
global $tasks;
$output = "";
foreach($tasks as $task => $cmds) {
$output .= "echo \"\nProcessing " . $task . " ...\"\n";
$output .= implode("\n", $cmds);
$output .= "\n";
}
return $output;
}
function execute() {
$output = "";
exec("sh " . BACKUP_PATH . "script.sh", $output, $result);
}
function createBaseTasks() {
global $tasks;
// Go to domains
$cmds[] = "cd " . BACKUP_PATH;
// Clear backup dir
$cmds[] = "rm *.sql";
$cmds[] = "rm *.tar.gz";
// Add task
$tasks['maintenance tasks'] = $cmds;
}
function createDomainsTasks($domains, $datestamp) {
global $tasks;
// Output command
foreach($domains as $key => $domain) {
$file = $domain["name"] . ".tar";
$cmds = array();
// Go to domains
// $cmds[] = "cd " . DOMAINS_PATH . $domain;
// Create archive file (no compression)
$cmds[] = 'echo "--> Creating archive"';
$cmds[] = "tar -cPf \"" . BACKUP_PATH . $file . "\" \"" . $domain["path"] . "\"";
// Zip file
// $cmds[] = "tar -zcPf " . BACKUP_PATH . $file . " " . $domain["path"];
// Upload to s3
$cmds[] = 'echo "--> Uploading archive"';
$cmds[] = "export PYTHONPATH=" . PYTHONPATH . "; " . S3CMD_FILE . " -c " . S3CFG_FILE_PATH . " -H put ".BACKUP_PATH ."$file s3://".S3_REMOTE_PATH.$datestamp."/".$file." --storage-class=STANDARD_IA --no-check-md5 --no-progress";
// Remove file
$cmds[] = "rm " . BACKUP_PATH . $file;
// Add task
$tasks[$domain["name"]." DOMAIN"] = $cmds;
}
}
function createFilesTasks($domains, $datestamp) {
global $tasks;
// Output command
foreach($domains as $key => $domain) {
$file = $domain["name"] . ".files.tar";
$storage = "/home/storage" . str_replace("httpdocs/", "", $domain["path"]);
$cmds = array();
// Go to domains
// $cmds[] = "cd " . DOMAINS_PATH . $domain;
// Conditional
$cmds[] = "if [ -d \"{$storage}\" ]; then";
// Create archive file (no compression)
$cmds[] = 'echo "--> Creating archive"';
$cmds[] = "tar -cPf \"" . BACKUP_PATH . $file . "\" \"" . $storage . "\"";
// Zip file (with compression level at 1 using --fast)
// $cmds[] = "gzip -v --fast " . BACKUP_PATH . $file;
// $file = $file . ".gz";
// Upload to s3
$cmds[] = 'echo "--> Uploading archive"';
$cmds[] = "export PYTHONPATH=" . PYTHONPATH . "; " . S3CMD_FILE . " -c " . S3CFG_FILE_PATH . " -H put ".BACKUP_PATH ."$file s3://".S3_REMOTE_PATH.$datestamp."/".$file." --storage-class=STANDARD_IA --no-check-md5 --no-progress";
// Remove file
$cmds[] = "rm " . BACKUP_PATH . $file;
// Conditional
$cmds[] = "fi";
// Add task
$tasks[$domain["name"]." FILES"] = $cmds;
}
}
function createDatabasesTask($dbs, $datestamp) {
global $tasks;
// Output command
foreach($dbs as $key => $db) {
$file = $db . ".sql.tar";
$cmds = array();
// Defin which mysql password to use
$mysqlPassword = (defined('DB_PASS_SHADOW')) ? '`cat ' . DB_PASS_SHADOW . '`' : DB_PASS;
// Backup db
// $cmds[] = "mysqldump -h " . DB_HOST . " -u " . DB_USER . " -p" . $mysqlPassword . " " . $db. " > " . BACKUP_PATH . $db . ".sql";
$cmds[] = "mysqldump -h " . DB_HOST . " -u " . DB_USER . " -p" . $mysqlPassword . " " . $db . " --default-character-set=utf8 --result-file=" . BACKUP_PATH . $db . ".sql";
// Go to backup
$cmds[] = "cd " . BACKUP_PATH;
// Create archive file (no compression)
$cmds[] = 'echo "--> Creating archive"';
$cmds[] = "tar -cPf \"" . $file . "\" \"" . $db . ".sql\"";
// Zip file
// $cmds[] = "tar -zcPf " . $file . " " . $db . ".sql";
// Upload to s3
$cmds[] = 'echo "--> Uploading archive"';
$cmds[] = "export PYTHONPATH=" . PYTHONPATH . "; " . S3CMD_FILE . " -c " . S3CFG_FILE_PATH . " -H put ".BACKUP_PATH ."$file s3://".S3_REMOTE_PATH.$datestamp."/".$file." --storage-class=STANDARD_IA --no-check-md5 --no-progress";
// Remove file
$cmds[] = "rm " . BACKUP_PATH . $file;
$cmds[] = "rm " . BACKUP_PATH . $db . ".sql";
// Add task
$tasks[$db." DB"] = $cmds;
}
}
function createMailTasks($webspaces, $datestamp)
{
global $tasks;
// Output command
foreach($webspaces as $key => $webspace) {
$file = $webspace.".mail.tar.gz";
$cmds = array();
// Conditional
$cmds[] = "if [ -f /usr/local/psa/bin/pleskbackup ]; then";
// Zip file
$cmds[] = 'echo "--> Creating archive"';
$cmds[] = "/usr/local/psa/bin/pleskbackup domains-name ".$webspace." --only-mail --output-file=".BACKUP_PATH.$file;
// $cmds[] = "/usr/local/psa/bin/pleskbackup domains-name ".$webspace." -v --only-mail --output-file=".BACKUP_PATH.$file;
// Upload to s3
$cmds[] = 'echo "--> Uploading archive"';
$cmds[] = "export PYTHONPATH=" . PYTHONPATH . "; " . S3CMD_FILE . " -c " . S3CFG_FILE_PATH . " -H put ".BACKUP_PATH ."$file s3://".S3_REMOTE_PATH.$datestamp."/".$file." --storage-class=STANDARD_IA --no-check-md5 --no-progress";
// Remove file
$cmds[] = "rm " . BACKUP_PATH . $file;
// Conditional
$cmds[] = "fi";
// Add task
$tasks[$webspace . " MAIL"] = $cmds;
}
}
function getWebspaces()
{
global $argc;
global $argv;
if($argc > 1) {
$webspaces = $argv;
array_shift($webspaces);
} else {
$webspaces = array();
}
return $webspaces;
}
function getDomains($webspaces)
{
clearstatcache();
foreach ($webspaces as $webspace) {
// Open websace folder and list its files
$webspaceHttpFolder = DOMAINS_PATH . $webspace . HTTP_FOLDER;
$dh = opendir($webspaceHttpFolder);
while (($filename = readdir($dh)) != false)
{
// Define current site full path
$sitepath = $webspaceHttpFolder . "/" . $filename;
// Add to domains list if it is a valid folder
if (is_dir($sitepath) && !is_link($sitepath) && $filename != "." && $filename != ".." )
{
$path = $sitepath;
$domains[] = array("name"=>$filename, "webspace"=>$webspace, "path"=>$path);
}
}
}
// Sort by domain in alphabetical order (this groups subdomains)
usort($domains, "compareDomains");
return $domains;
}
function compareDomains($a, $b)
{
$pattern = '/[^.]+\.[^\.]+$/';
preg_match($pattern, $a["name"], $a_domain);
preg_match($pattern, $b["name"], $b_domain);
return strcmp(reset($a_domain), reset($b_domain));
}
function getDatabases($link) {
$list = Array();
$dbs = $link->query("SHOW DATABASES", MYSQLI_USE_RESULT);
while ($row = mysqli_fetch_assoc($dbs)) {
if ($row["Database"] != 'information_schema') { $list[] = $row["Database"]; }
}
return $list;
}
function saveDomainsList($domains) {
$fp = fopen(BACKUP_PATH . "domains.txt", 'w');
foreach ($domains as $domain) {
fwrite($fp, $domain["path"] . "\n");
}
fclose($fp);
}
?>