diff options
author | Minteck <contact@minteck.org> | 2022-09-21 22:42:33 +0200 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-09-21 22:42:33 +0200 |
commit | e95b208af90cc386a7d8d1bcab426727cf4f0121 (patch) | |
tree | b293e0c644513fbeb6e9ad29be97fa6347a55e9f /includes/backup.php | |
parent | b5f589c323f415bb42ea7069cb4d1a8a2233dd69 (diff) | |
download | pluralconnect-e95b208af90cc386a7d8d1bcab426727cf4f0121.tar.gz pluralconnect-e95b208af90cc386a7d8d1bcab426727cf4f0121.tar.bz2 pluralconnect-e95b208af90cc386a7d8d1bcab426727cf4f0121.zip |
Update time
Diffstat (limited to 'includes/backup.php')
-rw-r--r-- | includes/backup.php | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/includes/backup.php b/includes/backup.php index 18ed6a4..ad09617 100644 --- a/includes/backup.php +++ b/includes/backup.php @@ -1,5 +1,6 @@ <?php +echo(" Scanning...\n"); $root = array_filter(scandir("data"), function ($i) { return !str_starts_with($i, "."); }); @@ -11,19 +12,22 @@ $data = [ 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; + if ($dirfile === "backup.poniesbackup" || $dirfile === "backup.ponieskey" || $dirfile === "encrypted" || $dirfile === "oldactions" || str_ends_with($dirfile, ".poniesbackup")) continue; + echo(" /$dirfile/$file\n"); $files[] = [ "dir" => $file, "file" => $dirfile ]; } } else { + echo(" /$file\n"); $files[] = [ "dir" => "", "file" => $file @@ -31,7 +35,9 @@ foreach ($root as $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]"), @@ -48,6 +54,7 @@ function pkcs7_pad($data, $size) { } if (!file_exists("./data/backup.ponieskey")) { + echo(" Reading encryption key...\n"); $key = openssl_random_pseudo_bytes(512); $iv = openssl_random_pseudo_bytes(16); file_put_contents("./data/backup.ponieskey", base64_encode(json_encode([ @@ -55,27 +62,41 @@ if (!file_exists("./data/backup.ponieskey")) { "key" => bin2hex($key) ]))); } else { + echo(" Creating encryption key...\n"); $key_raw = json_decode(base64_decode(file_get_contents("./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("./data/backup.poniesbackup", $encrypted); @mkdir("./data/encrypted"); $id = str_replace(":", "-", date('c')); copy("./data/backup.poniesbackup", "./data/encrypted/" . $id . ".poniesbackup"); +echo(" Uploading to servers...\n"); + +echo(" bridlewood... upload\n"); exec("scp ./data/encrypted/" . $id . ".poniesbackup fedora@bridlewood.equestria.dev:/opt/ponies"); -exec('ssh fedora@bridlewood.equestria.dev bash -c "cd /opt/ponies; ls -tp | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"'); +echo(" bridlewood... cleaning up\n"); +exec('ssh fedora@bridlewood.equestria.dev bash /opt/clean.sh'); +echo(" canterlot... upload\n"); exec("scp ./data/encrypted/" . $id . ".poniesbackup root@canterlot.equestria.dev:/opt/ponies"); -exec('ssh root@canterlot.equestria.dev bash -c "cd /opt/ponies; ls -tp | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"'); +echo(" canterlot... cleaning up\n"); +exec('ssh root@canterlot.equestria.dev bash /opt/clean.sh'); -copy("./data/encrypted/" . $id . ".poniesbackup", "/opt/ponies/" . $id . ".poniesbackup"); -exec('bash -c "cd /opt/ponies; ls -tp | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"'); +if (file_exists("/opt/ponies")) { + echo(" zephyrheights... copy\n"); + copy("./data/encrypted/" . $id . ".poniesbackup", "/opt/ponies/" . $id . ".poniesbackup"); + echo(" zephyrheights... cleaning up\n"); + exec('bash -c "cd /opt/ponies; ls -tp | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"'); +} +echo(" Cleaning up...\n"); unlink("./data/encrypted/" . $id . ".poniesbackup");
\ No newline at end of file |