Mercurial > hg > syntilista
diff slbackup.php @ 142:36c9cb759326
Implement simple SQLite database backup at program exit using Qt HTTP/HTTPS
and a PHP script on the remote server. Needs more work, testing and better
error handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 24 Aug 2017 15:44:33 +0300 |
parents | |
children | 3b904b49ce57 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/slbackup.php Thu Aug 24 15:44:33 2017 +0300 @@ -0,0 +1,142 @@ +<?php +// +// Syntilista - debt list/management database +// SQLite3 database backup backend PHP blurb +// Programmed and designed by Matti Hämäläinen <ccr@tnsp.org> +// (C) Copyright 2017 Tecnic Software productions (TNSP) +// +// Distributed under 3-clause BSD style license, refer to +// included file "COPYING" for exact terms. +// + +// +// Settings, etc. +// +$dataName = "backup"; +$dataSuffix = ".sqlite3"; +$dataMaxSize = 1024 * 1024; +$dataBackups = 24; +$configFile = "slbackup.cfg"; + +if (file_exists($configFile)) + require_once $configFile; + + +// +// Helper functions +// +function stError($msg) +{ + global $errorSet, $errorMsg; + $errorSet = TRUE; + $errorMsg = $msg; +} + + +function stGetBackupFilenameFilename($base, $suffix, $num) +{ + return sprintf("%s%02d%s", $base, $num, $suffix); +} + + +function stRotateBackups($base, $suffix, $num) +{ + for ($i = $num; $i > 0; $i--) + { + $srcFilename = stGetBackupFilenameFilename($base, $suffix, $i - 1); + if (file_exists($srcFilename)) + { + $dstFilename = stGetBackupFilenameFilename($base, $suffix, $i); + if (file_exists($dstFilename)) + unlink($dstFilename); + + if (@rename($srcFilename, $dstFilename) === FALSE) + return FALSE; + } + } + return TRUE; +} + + +// +// Actual main code begins here +// +$errorMsg = ""; +$errorSet = FALSE; +$index = "file"; + +if (!isset($dataSecret) || !isset($dataPath) || + $dataSecret == "" || $dataPath == "") +{ + error_log("SyntilistaBackup: Invalid configuration."); + exit; +} + +// Basic check for credentials .. +if (isset($_REQUEST["secret"]) && isset($_FILES[$index]) && + ($secret = $_REQUEST["secret"]) == $dataSecret) +{ + $fileName = $_FILES[$index]["tmp_name"]; + $fileSize = $_FILES[$index]["size"]; + $fileError = $_FILES[$index]["error"]; + + switch ($fileError) + { + case UPLOAD_ERR_INI_SIZE: + stError("File size exceeds PHP's max upload size."); + break; + + case UPLOAD_ERR_PARTIAL: + stError("File only partially uploaded."); + break; + + case UPLOAD_ERR_NO_FILE: + stError("No file data received!"); + break; + + case UPLOAD_ERR_NO_TMP_DIR: + stError("Internal error: Temporary file directory not available!"); + break; + + case UPLOAD_ERR_CANT_WRITE: + stError("Internal error: PHP could not write the file to disk."); + break; + + case UPLOAD_ERR_OK: + break; + + default: + stError("Unknown PHP file error occured."); + break; + } + + if (!$errorSet && $fileSize > $dataMaxSize) + { + stError("File is too large (".$fileSize." > ".$dataMaxSize." bytes)."); + } + + if (!$errorSet) + { + $path = $dataPath."/".$dataName; + stRotateBackups($path, $dataSuffix, $dataBackups); + + $dstFilename = stGetBackupFilenameFilename($path, $dataSuffix, 1); + if (@move_uploaded_file($fileName, $dstFilename) === false) + { + stError("Could not move the uploaded file to proper directory."); + } + } + + if ($errorSet) + { + header("Status: 500 Internal error"); + header("Content-Type: text/plain"); + echo $errorMsg; + } +} +else +{ + header("Status: 403 Forbidden"); + header("Content-Type: text/plain"); +} +?> \ No newline at end of file