mirror of
https://github.com/itflow-org/itflow
synced 2026-11-23 12:57:24 +00:00
Admin Backup restores a backup from its menu or an upload of up to 5 GB sent in pieces, checked in the Restore window and confirmed with the password and RESTORE, then queued for the Backup cron job, which takes a Before restore backup, restores, keeps both in the list, runs the database updates and ends the cycle while other dispatches wait; the page shows it waiting with Cancel, running and how it went; 26.11.9 changelog entries
This commit is contained in:
109
admin/backup.php
109
admin/backup.php
@@ -114,8 +114,22 @@ if (!empty($_SESSION['backup_master_key_reveal'])) {
|
||||
unset($_SESSION['backup_master_key_reveal']);
|
||||
}
|
||||
|
||||
// The page reloads while a backup is on its way - but never out from under a key shown once
|
||||
$reload = $state === 'working' && $master_key_reveal === '';
|
||||
// A restore - waiting for cron, being done, or how the last one went, for a week (functions/backup_restore.php)
|
||||
$restore_status = backupRestoreStatus();
|
||||
$restore_state = (string) ($restore_status['state'] ?? '');
|
||||
if ($restore_state === 'queued' && !backupRestoreRequest()) {
|
||||
$restore_state = 'running'; // cron took the request and is about to say so
|
||||
}
|
||||
if ($restore_state === 'running' && !backupRestoreInProgress() && strtotime((string) ($restore_status['started'] ?? $restore_status['at'] ?? '')) < strtotime('-6 hours')) {
|
||||
$restore_state = 'lost';
|
||||
}
|
||||
if (in_array($restore_state, ['done', 'failed', 'cancelled'], true) && strtotime((string) ($restore_status['at'] ?? '')) < strtotime('-7 days')) {
|
||||
$restore_state = '';
|
||||
}
|
||||
$restore_open = intval($_GET['restore'] ?? 0);
|
||||
|
||||
// The page reloads while a backup or a restore is on its way - but never out from under a key shown once
|
||||
$reload = ($state === 'working' || in_array($restore_state, ['queued', 'running'], true)) && $master_key_reveal === '';
|
||||
|
||||
// A time as "3 hours ago", with the date and time behind it
|
||||
$ago = function ($datetime) {
|
||||
@@ -123,13 +137,7 @@ $ago = function ($datetime) {
|
||||
};
|
||||
// Who a backup is down to, from its row
|
||||
$made_by = function ($backup) {
|
||||
if ($backup['backup_source'] === 'Cron') {
|
||||
return 'Scheduled';
|
||||
}
|
||||
if ($backup['backup_source'] === 'Adopted') {
|
||||
return 'Found on this server';
|
||||
}
|
||||
return (string) $backup['backup_created_by'] !== '' ? 'By ' . $backup['backup_created_by'] : 'By hand';
|
||||
return backupMadeBy($backup);
|
||||
};
|
||||
$schedule_text = $schedule_on ? "Every night at $schedule_time" : 'Off';
|
||||
// A type as it reads in a sentence - "a Full Backup", "a Database Only backup"
|
||||
@@ -196,6 +204,38 @@ $status_looks = [
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($restore_state !== '') {
|
||||
$restore_tone = ['queued' => 'info', 'running' => 'primary', 'done' => empty($restore_status['warnings']) ? 'success' : 'warning', 'failed' => 'danger', 'lost' => 'danger', 'cancelled' => 'secondary'][$restore_state];
|
||||
$restore_icon = ['queued' => 'fa-hourglass-half', 'running' => 'fa-circle-notch fa-spin', 'done' => 'fa-check-circle', 'failed' => 'fa-exclamation-triangle', 'lost' => 'fa-exclamation-triangle', 'cancelled' => 'fa-ban'][$restore_state];
|
||||
$restore_label = (string) ($restore_status['label'] ?? 'a backup'); ?>
|
||||
<!-- A restore, and how the last one went (functions/backup_restore.php) -->
|
||||
<div class="alert alert-<?= $restore_tone ?> d-flex gap-3 align-items-start" data-backup-restore-state="<?= $restore_state ?>"<?= $reload && in_array($restore_state, ['queued', 'running'], true) ? ' data-itflow-reload-seconds="10"' : '' ?>>
|
||||
<i class="fas fa-fw <?= $restore_icon ?> mt-1" aria-hidden="true"></i>
|
||||
<div class="flex-grow-1 min-w-0">
|
||||
<?php if ($restore_state === 'queued') { ?>
|
||||
<strong>A restore is waiting to start</strong> - the <?= escapeHtml($restore_label) ?>, asked for by <?= escapeHtml((string) ($restore_status['who'] ?? '')) ?> <?= $ago((string) ($restore_status['queued'] ?? $restore_status['at'] ?? '')) ?>.
|
||||
<?= $cron_is_running ? 'Cron starts it within a minute.' : 'Cron isn\'t running, so it starts when cron does - and not at all once ' . BACKUP_RESTORE_WAIT_MINUTES . ' minutes have gone by.' ?>
|
||||
<div class="mt-2">
|
||||
<a class="btn btn-sm btn-light border confirm-link" href="post.php?cancel_restore=1&csrf_token=<?= $csrf ?>" data-confirm-title="Cancel the restore?" data-confirm-text="Nothing is changed, and nothing is restored." data-confirm-button="Cancel it"><i class="fas fa-fw fa-ban me-1"></i>Cancel the restore</a>
|
||||
</div>
|
||||
<?php } elseif ($restore_state === 'running') { ?>
|
||||
<strong>Restoring</strong> the <?= escapeHtml($restore_label) ?> - started <?= $ago((string) ($restore_status['started'] ?? $restore_status['at'] ?? '')) ?> by <?= escapeHtml((string) ($restore_status['who'] ?? '')) ?>.
|
||||
ITFlow is backed up as it is first, then replaced. Anyone working meanwhile may see errors, and you may be signed out - sign in again once it's done.
|
||||
<?php } elseif ($restore_state === 'lost') { ?>
|
||||
<strong>The last restore didn't say it finished.</strong> The <?= escapeHtml($restore_label) ?> was started <?= $ago((string) ($restore_status['started'] ?? $restore_status['at'] ?? '')) ?> by <?= escapeHtml((string) ($restore_status['who'] ?? '')) ?> - the Backup job may have been stopped part way.
|
||||
Check that ITFlow works, and <a href="cron.php">Maintenance > Cron</a> and <a href="app_logs.php">App Logs</a> for what happened. The <strong>Before restore</strong> backup, if it's in the list, is ITFlow as it was.
|
||||
<?php } else { ?>
|
||||
<strong><?= ['done' => 'Last restore', 'failed' => "The last restore didn't work", 'cancelled' => 'Last restore cancelled'][$restore_state] ?></strong>
|
||||
<span class="text-body-secondary">(<?= escapeHtml((string) ($restore_status['who'] ?? '')) ?>, <?= $ago((string) ($restore_status['at'] ?? '')) ?>)</span> -
|
||||
<?= escapeHtml((string) ($restore_status['message'] ?? '')) ?>
|
||||
<?php foreach ((array) ($restore_status['warnings'] ?? []) as $restore_warning) { ?>
|
||||
<div class="mt-1"><i class="fas fa-fw fa-exclamation-triangle me-1"></i><?= escapeHtml((string) $restore_warning) ?></div>
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<div class="row g-3">
|
||||
|
||||
<div class="col-lg-8">
|
||||
@@ -368,6 +408,11 @@ $status_looks = [
|
||||
<?php if ($status === 'Complete' && (string) $backup['backup_sha256'] !== '') { ?>
|
||||
<button type="button" class="dropdown-item clipboardjs" data-clipboard-text="<?= escapeHtml($backup['backup_sha256']) ?>" title="To check a copy is the same file"><i class="fas fa-fw fa-fingerprint text-secondary me-2"></i>Copy its SHA-256 checksum</button>
|
||||
<?php } ?>
|
||||
<?php if ($status === 'Complete' && $backup['backup_type'] !== BACKUP_TYPE_MASTER_KEY) { ?>
|
||||
<button type="button" class="dropdown-item ajax-modal" data-modal-url="modals/backup/backup_restore.php?backup_id=<?= $backup_id ?>" data-backup-restore-open="<?= $backup_id ?>">
|
||||
<i class="fas fa-fw fa-undo text-secondary me-2"></i>Restore
|
||||
</button>
|
||||
<?php } ?>
|
||||
<?php if ($status === 'Complete') { ?>
|
||||
<div class="dropdown-divider"></div>
|
||||
<?php } ?>
|
||||
@@ -587,21 +632,40 @@ $config_dev_mail_to = '<?= escapeHtml($dev_mail_to !== '' ? $dev_mail_to : 'dev@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Restoring - not from here -->
|
||||
<div class="card mb-3">
|
||||
<!-- Restoring - a backup in the list, one uploaded, or from the command line (functions/backup_restore.php) -->
|
||||
<div class="card mb-3" id="restoring">
|
||||
<div class="card-header py-3">
|
||||
<h3 class="card-title"><i class="fas fa-fw fa-undo me-2"></i>Restoring</h3>
|
||||
</div>
|
||||
<div class="card-body small">
|
||||
<p class="text-body-secondary">Restoring replaces the database - and, from a Full Backup, everything uploaded - with what's in the backup. It isn't done from this page, on purpose: a running ITFlow shouldn't drop its own tables from a browser.</p>
|
||||
<p class="fw-semibold mb-1">From the command line</p>
|
||||
<div class="position-relative">
|
||||
<pre class="itflow-command bg-body-tertiary border rounded p-2 pe-5 mb-2"><code><?= escapeHtml($cli_command) ?></code></pre>
|
||||
<button type="button" class="btn btn-sm btn-link position-absolute top-0 end-0 clipboardjs" title="Copy" data-clipboard-text="<?= escapeHtml($cli_command) ?>"><i class="far fa-copy"></i></button>
|
||||
</div>
|
||||
<p class="text-body-secondary">Works for a backup of any size. Add <code>--inspect</code> to see what's in one without changing anything, and <code>--key=</code> for a backup from another server, which needs that server's key. A backup's menu copies its path.</p>
|
||||
<p class="fw-semibold mb-1">On a new server</p>
|
||||
<p class="text-body-secondary mb-0">The setup wizard at <code>/setup</code> offers to restore a backup before the first user exists - up to <?= escapeHtml(backupFormatBytes(backupMaxUploadBytes())) ?> here, the most PHP takes in one upload.</p>
|
||||
<p class="text-body-secondary">Restoring replaces the database - and, from a Full Backup, everything uploaded - with what's in the backup. Pick <strong class="text-body">Restore</strong> in a backup's menu, or upload one - from another ITFlow, say. ITFlow is backed up as it is first, as <strong class="text-body">Before restore</strong>, so a restore can be undone.</p>
|
||||
<form action="post.php" method="post" enctype="multipart/form-data" autocomplete="off" data-backup-upload data-max="<?= BACKUP_RESTORE_MAX_BYTES ?>">
|
||||
<input type="hidden" name="csrf_token" value="<?= $csrf ?>">
|
||||
<label class="form-label" for="backupUploadFile">Upload a backup to restore</label>
|
||||
<div class="input-group mb-2">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-file-archive"></i></span>
|
||||
<input type="file" class="form-control" name="backup_file" id="backupUploadFile" accept=".zip,application/zip" required>
|
||||
</div>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-key"></i></span>
|
||||
<input type="password" class="form-control" data-toggle="password" name="restore_key" placeholder="Its key, if from another ITFlow" autocomplete="off" aria-label="Its encryption key">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-eye"></i></span>
|
||||
</div>
|
||||
<div class="form-text">Up to <?= escapeHtml(backupFormatBytes(BACKUP_RESTORE_MAX_BYTES)) ?>. It joins the list as Uploaded, and its Restore window opens. A restore needs free space of about three times the backup's size.</div>
|
||||
<button type="submit" name="backup_upload" class="btn btn-sm btn-light border mt-2" data-backup-upload-button><i class="fas fa-fw fa-upload me-1"></i>Upload</button>
|
||||
<div class="mt-2" data-backup-upload-progress hidden>
|
||||
<div class="progress" role="progressbar" aria-label="Upload"><div class="progress-bar progress-bar-striped progress-bar-animated" style="width: 0%"></div></div>
|
||||
<div class="text-body-secondary mt-1" data-backup-upload-text></div>
|
||||
</div>
|
||||
</form>
|
||||
<details class="mt-3">
|
||||
<summary class="text-body-secondary">From the command line</summary>
|
||||
<div class="position-relative mt-2">
|
||||
<pre class="itflow-command bg-body-tertiary border rounded p-2 pe-5 mb-2"><code><?= escapeHtml($cli_command) ?></code></pre>
|
||||
<button type="button" class="btn btn-sm btn-link position-absolute top-0 end-0 clipboardjs" title="Copy" data-clipboard-text="<?= escapeHtml($cli_command) ?>"><i class="far fa-copy"></i></button>
|
||||
</div>
|
||||
<p class="text-body-secondary mb-0">For a backup of any size, or when cron isn't running. Add <code>--inspect</code> to see what's in one without changing anything, and <code>--key=</code> for a backup from another server, which needs that server's key. A backup's menu copies its path. On a new server, the setup wizard at <code>/setup</code> offers to restore a backup before the first user exists.</p>
|
||||
</details>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -622,7 +686,12 @@ $config_dev_mail_to = '<?= escapeHtml($dev_mail_to !== '' ? $dev_mail_to : 'dev@
|
||||
|
||||
</div>
|
||||
|
||||
<?php if ($restore_open) { ?>
|
||||
<button type="button" class="d-none ajax-modal" data-modal-url="modals/backup/backup_restore.php?backup_id=<?= $restore_open ?>" data-backup-restore-autoopen></button>
|
||||
<?php } ?>
|
||||
|
||||
<script src="<?= staticFileUrl('/js/auto_reload.js') ?>"></script>
|
||||
<script src="<?= staticFileUrl('/js/backup_restore.js') ?>"></script>
|
||||
|
||||
<?php
|
||||
require_once "../includes/footer.php";
|
||||
|
||||
101
admin/modals/backup/backup_restore.php
Normal file
101
admin/modals/backup/backup_restore.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Maintenance > Backup - restore a backup in the list (functions/backup_restore.php). What's in
|
||||
* it first, then the admin's password and RESTORE typed to confirm; admin/post/backup.php,
|
||||
* restore_backup, queues it for cron.
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
[$restore_row, $restore_path] = backupRestoreStored($mysqli, intval($_GET['backup_id'] ?? 0));
|
||||
$restore_meta = null;
|
||||
$restore_problem = '';
|
||||
$restore_needs_key = false;
|
||||
if ($restore_row) {
|
||||
[$restore_meta, $restore_problem] = backupRestoreCheck($mysqli, $restore_path, backupRestoreKey(''));
|
||||
// Made with another install's key - it opens once that key is typed
|
||||
$restore_needs_key = !$restore_meta;
|
||||
}
|
||||
$restore_can = $restore_row && ($restore_problem === '' || $restore_needs_key) && !backupRestoreInProgress() && backupEncryptionKey() !== '';
|
||||
$restore_with_uploads = !$restore_meta || in_array('uploads.zip', $restore_meta['entries'], true);
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-undo me-2"></i>Restore a backup</h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
<form action="post.php" method="post" autocomplete="off" data-backup-restore>
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="backup_id" value="<?= intval($restore_row['backup_id'] ?? 0) ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
<?php if (!$restore_row) { ?>
|
||||
<div class="alert alert-danger mb-0"><?= escapeHtml($restore_path) ?></div>
|
||||
<?php } else { ?>
|
||||
<dl class="row small mb-3" data-backup-restore-details>
|
||||
<dt class="col-sm-4 text-body-secondary fw-normal">Backup</dt>
|
||||
<dd class="col-sm-8"><?= escapeHtml(backupTypeLabel((string) $restore_row['backup_type'])) ?>, <?= escapeHtml(backupFormatBytes($restore_row['backup_size'])) ?></dd>
|
||||
<dt class="col-sm-4 text-body-secondary fw-normal">In the list since</dt>
|
||||
<dd class="col-sm-8"><?= escapeHtml(substr((string) $restore_row['backup_created_at'], 0, 16)) ?> - <?= escapeHtml(backupMadeBy($restore_row)) ?></dd>
|
||||
<?php if ($restore_meta) { ?>
|
||||
<dt class="col-sm-4 text-body-secondary fw-normal">Taken</dt>
|
||||
<dd class="col-sm-8"><?= escapeHtml($restore_meta['generated']) ?></dd>
|
||||
<dt class="col-sm-4 text-body-secondary fw-normal">Database version</dt>
|
||||
<dd class="col-sm-8"><?= escapeHtml($restore_meta['database_version']) ?> <span class="text-body-secondary">- this ITFlow is at <?= escapeHtml(backupCurrentDatabaseVersion($mysqli)) ?></span></dd>
|
||||
<dt class="col-sm-4 text-body-secondary fw-normal">Holds</dt>
|
||||
<dd class="col-sm-8"><?= $restore_with_uploads ? 'The database and everything uploaded' : 'The database only' ?></dd>
|
||||
<?php } ?>
|
||||
</dl>
|
||||
|
||||
<?php if (backupEncryptionKey() === '') { ?>
|
||||
<div class="alert alert-danger mb-0">There's no encryption key, so ITFlow can't back itself up before a restore. See the top of this page.</div>
|
||||
<?php } elseif (backupRestoreInProgress()) { ?>
|
||||
<div class="alert alert-info mb-0">A restore is running - wait for it to finish.</div>
|
||||
<?php } elseif ($restore_problem !== '' && !$restore_needs_key) { ?>
|
||||
<div class="alert alert-danger mb-0" data-backup-restore-problem><?= escapeHtml($restore_problem) ?></div>
|
||||
<?php } else { ?>
|
||||
<div class="alert alert-warning small">
|
||||
<strong>This replaces everything</strong> - the database<?= $restore_with_uploads ? ' and every file uploaded to ITFlow' : '' ?> - with what's in the backup.
|
||||
ITFlow is backed up as it is first and kept as <strong>Before restore</strong>, so this can be undone.
|
||||
Cron does it within a minute, and it takes a minute or more - anyone working then may see errors.
|
||||
Afterwards, sign in with an account from the backup if you're asked to.
|
||||
</div>
|
||||
|
||||
<?php if ($restore_needs_key) { ?>
|
||||
<label class="form-label" for="restoreKey">Its encryption key <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-key"></i></span>
|
||||
<input type="password" class="form-control" data-toggle="password" name="restore_key" id="restoreKey" placeholder="From Backup on the ITFlow that made it" autocomplete="off" required>
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-eye"></i></span>
|
||||
</div>
|
||||
<div class="form-text mb-3">This ITFlow's key doesn't open it - it was made on another ITFlow, whose Maintenance > Backup has its key. <span class="text-body-secondary">(<?= escapeHtml($restore_problem) ?>)</span></div>
|
||||
<?php } ?>
|
||||
|
||||
<label class="form-label" for="restorePassword">Your password <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group mb-3">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-lock"></i></span>
|
||||
<input type="password" class="form-control" data-toggle="password" name="password" id="restorePassword" placeholder="To confirm it's you" autocomplete="current-password" required>
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-eye"></i></span>
|
||||
</div>
|
||||
|
||||
<label class="form-label" for="restoreConfirm">Type <strong>RESTORE</strong> to confirm <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-keyboard"></i></span>
|
||||
<input type="text" class="form-control" name="confirm" id="restoreConfirm" pattern="RESTORE" placeholder="RESTORE" autocomplete="off" required>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<?php if ($restore_can) { ?>
|
||||
<button type="submit" name="restore_backup" class="btn btn-danger text-bold"><i class="fas fa-fw fa-undo me-2"></i>Restore</button>
|
||||
<?php } ?>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php
|
||||
require_once "../../../includes/modal_footer.php";
|
||||
@@ -167,3 +167,199 @@ if (isset($_POST['edit_dev_mode'])) {
|
||||
|
||||
redirect("backup.php#dev-mode");
|
||||
}
|
||||
|
||||
/*
|
||||
* Restoring from this page (functions/backup_restore.php). The admin's password and typing
|
||||
* RESTORE confirm it, then it's queued for cron/backup.php - nothing is replaced in this request.
|
||||
*/
|
||||
if (isset($_POST['restore_backup'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$backup_id = intval($_POST['backup_id'] ?? 0);
|
||||
|
||||
$user_row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT user_password FROM users WHERE user_id = " . intval($session_user_id)));
|
||||
|
||||
if (!$user_row || !password_verify((string) ($_POST['password'] ?? ''), (string) $user_row['user_password'])) {
|
||||
logAudit("Backup", "Restore", "$session_name tried to restore a backup with the wrong password");
|
||||
flashAlert("That isn't your password - nothing was restored.", 'error');
|
||||
redirect("backup.php");
|
||||
}
|
||||
|
||||
if (trim((string) ($_POST['confirm'] ?? '')) !== 'RESTORE') {
|
||||
flashAlert("Type RESTORE to confirm - nothing was restored.", 'error');
|
||||
redirect("backup.php");
|
||||
}
|
||||
|
||||
[$restore_ok, $restore_message] = backupRestoreQueue($mysqli, $backup_id, (string) ($_POST['restore_key'] ?? ''), stripslashes((string) $session_name), intval($session_user_id));
|
||||
|
||||
if (!$restore_ok) {
|
||||
flashAlert(escapeHtml($restore_message), 'error');
|
||||
redirect("backup.php");
|
||||
}
|
||||
|
||||
logAudit("Backup", "Restore", "$session_name asked to restore the " . $restore_message);
|
||||
|
||||
$cron_row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT config_enable_cron FROM settings WHERE company_id = 1"));
|
||||
|
||||
if (intval($cron_row['config_enable_cron']) === 0) {
|
||||
flashAlert("Restore queued, but cron is switched off in Maintenance > Cron - it's only done if cron runs within " . BACKUP_RESTORE_WAIT_MINUTES . " minutes.", 'warning');
|
||||
} else {
|
||||
flashAlert("Restore queued - cron starts it within a minute. ITFlow is backed up as it is first, then the backup is restored.");
|
||||
}
|
||||
|
||||
redirect("backup.php");
|
||||
}
|
||||
|
||||
if (isset($_GET['cancel_restore'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
if (backupRestoreCancel(stripslashes((string) $session_name))) {
|
||||
logAudit("Backup", "Restore", "$session_name cancelled a restore before it started");
|
||||
flashAlert("Restore cancelled - nothing was changed.");
|
||||
} else {
|
||||
flashAlert("There's no restore waiting to start - it may have started already.", 'error');
|
||||
}
|
||||
|
||||
redirect("backup.php");
|
||||
}
|
||||
|
||||
// Upload a backup to restore, in one request - the form without the script (js/backup_restore.js)
|
||||
if (isset($_POST['backup_upload'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$file = $_FILES['backup_file'] ?? [];
|
||||
|
||||
if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || !is_uploaded_file($file['tmp_name'] ?? '')) {
|
||||
$too_big = in_array($file['error'] ?? 0, [UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE], true);
|
||||
flashAlert($too_big ? "It's bigger than this server takes in one go (" . escapeHtml(backupFormatBytes(backupMaxUploadBytes())) . ") - upload it with JavaScript on, which sends it in pieces, or restore it from the command line." : "Pick a backup .zip to upload.", 'error');
|
||||
redirect("backup.php");
|
||||
}
|
||||
|
||||
[$uploaded_id, $upload_message] = backupRestoreAdopt($mysqli, $file['tmp_name'], (string) ($_POST['restore_key'] ?? ''), stripslashes((string) $session_name), true);
|
||||
|
||||
if (!$uploaded_id) {
|
||||
flashAlert(escapeHtml($upload_message), 'error');
|
||||
redirect("backup.php");
|
||||
}
|
||||
|
||||
logAudit("Backup", "Upload", "$session_name uploaded a backup to restore");
|
||||
flashAlert(escapeHtml($upload_message));
|
||||
redirect("backup.php?restore=$uploaded_id");
|
||||
}
|
||||
|
||||
/*
|
||||
* Upload a backup in pieces (js/backup_restore.js), so PHP's and a proxy's request limits don't
|
||||
* matter. Answers JSON. The upload going on is kept in the admin's session; the pieces are put
|
||||
* together beside the backups (backupRestorePartPath()).
|
||||
*/
|
||||
if (isset($_POST['backup_upload_start']) || isset($_POST['backup_upload_chunk']) || isset($_POST['backup_upload_finish'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
try {
|
||||
|
||||
if (isset($_POST['backup_upload_start'])) {
|
||||
|
||||
$size = intval($_POST['size'] ?? 0);
|
||||
|
||||
if (!preg_match('/\.zip$/i', (string) ($_POST['name'] ?? ''))) {
|
||||
throw new RuntimeException('Pick a backup .zip to upload.');
|
||||
}
|
||||
if ($size <= 0 || $size > BACKUP_RESTORE_MAX_BYTES) {
|
||||
throw new RuntimeException('A backup up to ' . backupFormatBytes(BACKUP_RESTORE_MAX_BYTES) . ' can be uploaded here - restore a bigger one from the command line.');
|
||||
}
|
||||
|
||||
backupRestoreTidyParts();
|
||||
if (!empty($_SESSION['backup_upload']['id'])) {
|
||||
@unlink(backupRestorePartPath($_SESSION['backup_upload']['id']));
|
||||
}
|
||||
|
||||
$problem = backupRestoreDiskProblem($size + 512 * 1024 * 1024, 'for the upload');
|
||||
if ($problem !== '') {
|
||||
throw new RuntimeException($problem);
|
||||
}
|
||||
|
||||
$upload_id = bin2hex(random_bytes(16));
|
||||
if (@file_put_contents(backupRestorePartPath($upload_id), '') === false) {
|
||||
throw new RuntimeException("The backups folder can't be written to.");
|
||||
}
|
||||
@chmod(backupRestorePartPath($upload_id), 0600);
|
||||
|
||||
$_SESSION['backup_upload'] = ['id' => $upload_id, 'size' => $size, 'received' => 0];
|
||||
|
||||
echo json_encode(['id' => $upload_id, 'chunk' => backupRestoreChunkBytes()]);
|
||||
|
||||
} elseif (isset($_POST['backup_upload_chunk'])) {
|
||||
|
||||
$upload = $_SESSION['backup_upload'] ?? null;
|
||||
$piece = $_FILES['chunk'] ?? [];
|
||||
|
||||
if (!$upload || !hash_equals((string) $upload['id'], (string) ($_POST['id'] ?? ''))) {
|
||||
throw new RuntimeException('This upload is no longer going on - start it again.');
|
||||
}
|
||||
if (($piece['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || !is_uploaded_file($piece['tmp_name'] ?? '')) {
|
||||
throw new RuntimeException('A piece of the file didn\'t arrive (error ' . intval($piece['error'] ?? 0) . ').');
|
||||
}
|
||||
if (intval($_POST['offset'] ?? -1) !== intval($upload['received'])) {
|
||||
throw new RuntimeException('A piece arrived out of order - start the upload again.');
|
||||
}
|
||||
|
||||
$length = intval(filesize($piece['tmp_name']));
|
||||
if ($length <= 0 || $length > backupRestoreChunkBytes() || $upload['received'] + $length > $upload['size']) {
|
||||
throw new RuntimeException('A piece of the file wasn\'t the size expected - start the upload again.');
|
||||
}
|
||||
|
||||
$in = fopen($piece['tmp_name'], 'rb');
|
||||
$out = fopen(backupRestorePartPath($upload['id']), 'ab');
|
||||
$copied = ($in && $out) ? stream_copy_to_stream($in, $out) : false;
|
||||
$in && fclose($in);
|
||||
$out && fclose($out);
|
||||
|
||||
if ($copied !== $length) {
|
||||
throw new RuntimeException("A piece couldn't be written to the backups folder - is the disk full?");
|
||||
}
|
||||
|
||||
$_SESSION['backup_upload']['received'] += $length;
|
||||
|
||||
echo json_encode(['received' => $_SESSION['backup_upload']['received']]);
|
||||
|
||||
} else {
|
||||
|
||||
$upload = $_SESSION['backup_upload'] ?? null;
|
||||
unset($_SESSION['backup_upload']);
|
||||
|
||||
if (!$upload || !hash_equals((string) $upload['id'], (string) ($_POST['id'] ?? ''))) {
|
||||
throw new RuntimeException('This upload is no longer going on - start it again.');
|
||||
}
|
||||
|
||||
$part = backupRestorePartPath($upload['id']);
|
||||
|
||||
if (intval($upload['received']) !== intval($upload['size']) || !is_file($part) || filesize($part) !== intval($upload['size'])) {
|
||||
@unlink($part);
|
||||
throw new RuntimeException("The upload didn't finish - start it again.");
|
||||
}
|
||||
|
||||
[$uploaded_id, $upload_message] = backupRestoreAdopt($mysqli, $part, (string) ($_POST['restore_key'] ?? ''), stripslashes((string) $session_name), false);
|
||||
|
||||
if (!$uploaded_id) {
|
||||
throw new RuntimeException($upload_message);
|
||||
}
|
||||
|
||||
logAudit("Backup", "Upload", "$session_name uploaded a backup to restore");
|
||||
flashAlert(escapeHtml($upload_message));
|
||||
|
||||
echo json_encode(['redirect' => "backup.php?restore=$uploaded_id"]);
|
||||
}
|
||||
|
||||
} catch (Throwable $e) {
|
||||
http_response_code(422);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
}
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user