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
|
<?php
echo(" Scanning...\n");
$root = array_filter(scandir("data"), function ($i) {
return !str_starts_with($i, ".");
});
$files = [];
$data = [
"date" => date('c'),
"files" => []
];
foreach ($root as $file) {
if ($file === "backup.poniesbackup" || $file === "backup.ponieskey" || $file === "encrypted" || str_ends_with($file, ".poniesbackup")) continue;
echo(" /$file\n");
if (is_dir("data/$file")) {
foreach (array_filter(scandir("data/$file"), function ($i) {
return !str_starts_with($i, ".");
}) as $dirfile) {
if ($dirfile === "backup.poniesbackup" || $dirfile === "backup.ponieskey" || $dirfile === "encrypted" || str_ends_with($dirfile, ".poniesbackup")) continue;
echo(" /$dirfile/$file\n");
$files[] = [
"dir" => $file,
"file" => $dirfile
];
}
} else {
echo(" /$file\n");
$files[] = [
"dir" => "",
"file" => $file
];
}
}
echo(" Reading files...\n");
foreach ($files as $file) {
echo(" /$file[dir]/$file[file]\n");
$file["mime"] = mime_content_type("data/$file[dir]/$file[file]");
$file["checksum"] = [
sha1_file("data/$file[dir]/$file[file]"),
md5_file("data/$file[dir]/$file[file]")
];
$file["content"] = base64_encode(file_get_contents("data/$file[dir]/$file[file]"));
$data["files"][] = $file;
}
function pkcs7_pad($data, $size) {
$length = $size - strlen($data) % $size;
return $data . str_repeat(chr($length), $length);
}
if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/backup.ponieskey")) {
echo(" Creating encryption key...\n");
$key = openssl_random_pseudo_bytes(512);
$iv = openssl_random_pseudo_bytes(16);
file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/backup.ponieskey", base64_encode(json_encode([
"iv" => bin2hex($iv),
"key" => bin2hex($key)
])));
} else {
echo(" Reading encryption key...\n");
$key_raw = json_decode(base64_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/backup.ponieskey")), true);
$key = hex2bin($key_raw["key"]);
$iv = hex2bin($key_raw["iv"]);
}
echo(" Encrypting...\n");
$payload = json_encode($data);
$encrypted = openssl_encrypt(pkcs7_pad($payload, 16), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
echo(" Writing backup...\n");
file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/backup.poniesbackup", $encrypted);
@mkdir($_SERVER['DOCUMENT_ROOT'] . "/includes/data/encrypted");
$id = str_replace(":", "-", date('c'));
copy($_SERVER['DOCUMENT_ROOT'] . "/includes/data/backup.poniesbackup", $_SERVER['DOCUMENT_ROOT'] . "/includes/data/encrypted/" . $id . ".poniesbackup");
echo(" Uploading to servers...\n");
echo(" zephyrheights... upload\n");
exec("scp -P 2222 " . $_SERVER['DOCUMENT_ROOT'] . "/includes/data/encrypted/" . $id . ".poniesbackup root@zephyrheights.equestria.dev:/opt/ponies");
echo(" zephyrheights... cleaning up\n");
exec('ssh -p 2222 root@zephyrheights.equestria.dev bash -c "cd /opt/ponies; ls -tp | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"');
if (file_exists("/opt/ponies")) {
echo(" bridlewood... copy\n");
copy($_SERVER['DOCUMENT_ROOT'] . "/includes/data/encrypted/" . $id . ".poniesbackup", "/opt/ponies/" . $id . ".poniesbackup");
echo(" bridlewood... cleaning up\n");
exec('bash -c "cd /opt/ponies; ls -tp | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"');
}
echo(" Cleaning up...\n");
unlink($_SERVER['DOCUMENT_ROOT'] . "/includes/data/encrypted/" . $id . ".poniesbackup");
|