Subversion Repositories oidplus

Compare Revisions

Regard whitespace Rev 1351 → Rev 1352

/trunk/dev/backup_wip.phps
23,7 → 23,7
 
header('Content-Type:text/html; charset=UTF-8');
 
require_once __DIR__ . '/includes/oidplus.inc.php';
require_once __DIR__ . '/../includes/oidplus.inc.php';
 
set_exception_handler(array(OIDplusGui::class, 'html_exception_handler'));
 
42,11 → 42,12
 
// ================ Backup ================
 
function oidplus_backup_db(string $backup_file, bool $export_objects=true, bool $export_ra=true): void {
$num_rows = [
"objects" => 0,
"asn1id" => 0,
"iri" => 0,
"ra" => 0,
"objects" => $export_objects ? 0 : "n/a",
"asn1id" => $export_objects ? 0 : "n/a",
"iri" => $export_objects ? 0 : "n/a",
"ra" => $export_ra ? 0 : "n/a",
"log" => "n/a", // No backup for this table!
"log_object" => "n/a", // No backup for this table!
"log_user" => "n/a", // No backup for this table!
54,14 → 55,19
];
 
if (BACKUP_RECOVERY_SPECIAL_TEST) {
if ($export_objects) {
OIDplus::db()->query("delete from ###objects where id like '%_CLONE'");
OIDplus::db()->query("delete from ###asn1id where oid like '%_CLONE'");
OIDplus::db()->query("delete from ###iri where oid like '%_CLONE'");
}
if ($export_ra) {
OIDplus::db()->query("delete from ###ra where email like '%_CLONE'");
}
}
 
// Backup objects (Tables objects, asn1id, iri)
$objects = [];
if ($export_objects) {
$res = OIDplus::db()->query("select * from ###objects order by id");
$rows = [];
while ($row = $res->fetch_array()) {
107,9 → 113,11
"iris" => $iris
];
}
}
 
// Backup RAs (Table ra)
$ra = [];
if ($export_ra) {
$res = OIDplus::db()->query("select * from ###ra order by email");
while ($row = $res->fetch_array()) {
$num_rows["ra"]++;
132,6 → 140,7
"last_login" => $row["last_login"]
];
}
}
 
// Put everything together
$json = [
148,11 → 157,8
OIDplus::logger()->log("V2:[INFO]A", "Created backup of Objects and RAs");
 
 
$backup_file = 'oidplus-'.date('Y-m-d-H-i-s').'.bak.json';
 
$encoded_data = json_encode($json, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
if (!is_dir(OIDplus::localpath().'/userdata/backups/')) @mkdir(OIDplus::localpath().'/userdata/backups/');
if (@file_put_contents(OIDplus::localpath().'/userdata/backups/'.$backup_file, $encoded_data) === false) {
if (@file_put_contents($backup_file, $encoded_data) === false) {
throw new OIDplusException("Could not write file to disk: $backup_file");
}
 
164,6 → 170,7
//echo '<pre>';
//echo htmlentities($encoded_data);
//echo '</pre>';
}
 
 
 
170,11 → 177,12
 
// ================ Recovery ================
 
function oidplus_restore_db(string $backup_file, bool $import_objects=true, bool $import_ra=true): void {
$num_rows = [
"objects" => 0,
"asn1id" => 0,
"iri" => 0,
"ra" => 0,
"objects" => $import_objects ? 0 : "n/a",
"asn1id" => $import_objects ? 0 : "n/a",
"iri" => $import_objects ? 0 : "n/a",
"ra" => $import_ra ? 0 : "n/a",
"log" => "n/a", // No backup for this table!
"log_object" => "n/a", // No backup for this table!
"log_user" => "n/a", // No backup for this table!
181,7 → 189,7
"config" => "n/a" // No backup for this table!
];
 
$cont = @file_get_contents(OIDplus::localpath().'/userdata/backups/'.$backup_file);
$cont = @file_get_contents($backup_file);
if ($cont === false) throw new OIDplusException("Could not read file from disk: $backup_file");
$json = @json_decode($cont,true);
if ($json === false) throw new OIDplusException("Could not decode JSON structure of $backup_file");
188,6 → 196,8
 
if (OIDplus::db()->transaction_supported()) OIDplus::db()->transaction_begin();
try {
 
if ($import_objects) {
if (!BACKUP_RECOVERY_SPECIAL_TEST) {
OIDplus::db()->query("delete from ###objects");
OIDplus::db()->query("delete from ###asn1id");
232,7 → 242,9
);
}
}
}
 
if ($import_ra) {
if (!BACKUP_RECOVERY_SPECIAL_TEST) {
OIDplus::db()->query("delete from ###ra");
}
261,6 → 273,7
$row["last_login"]??null)
);
}
}
 
echo "<p>Backup restore done: $backup_file</p>";
foreach ($num_rows as $table_name => $cnt) {
275,10 → 288,16
if (OIDplus::db()->transaction_supported()) OIDplus::db()->transaction_rollback();
throw $e;
}
}
 
 
 
if (!is_dir(OIDplus::localpath().'/userdata/backups/')) @mkdir(OIDplus::localpath().'/userdata/backups/');
$backup_file = OIDplus::localpath().'/userdata/backups/oidplus-'.date('Y-m-d-H-i-s').'.bak.json';
oidplus_backup_db($backup_file, true, true);
oidplus_restore_db($backup_file, true, true);
 
 
 
 
OIDplus::invoke_shutdown();