comparison 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
comparison
equal deleted inserted replaced
141:d0943d41f391 142:36c9cb759326
1 <?php
2 //
3 // Syntilista - debt list/management database
4 // SQLite3 database backup backend PHP blurb
5 // Programmed and designed by Matti Hämäläinen <ccr@tnsp.org>
6 // (C) Copyright 2017 Tecnic Software productions (TNSP)
7 //
8 // Distributed under 3-clause BSD style license, refer to
9 // included file "COPYING" for exact terms.
10 //
11
12 //
13 // Settings, etc.
14 //
15 $dataName = "backup";
16 $dataSuffix = ".sqlite3";
17 $dataMaxSize = 1024 * 1024;
18 $dataBackups = 24;
19 $configFile = "slbackup.cfg";
20
21 if (file_exists($configFile))
22 require_once $configFile;
23
24
25 //
26 // Helper functions
27 //
28 function stError($msg)
29 {
30 global $errorSet, $errorMsg;
31 $errorSet = TRUE;
32 $errorMsg = $msg;
33 }
34
35
36 function stGetBackupFilenameFilename($base, $suffix, $num)
37 {
38 return sprintf("%s%02d%s", $base, $num, $suffix);
39 }
40
41
42 function stRotateBackups($base, $suffix, $num)
43 {
44 for ($i = $num; $i > 0; $i--)
45 {
46 $srcFilename = stGetBackupFilenameFilename($base, $suffix, $i - 1);
47 if (file_exists($srcFilename))
48 {
49 $dstFilename = stGetBackupFilenameFilename($base, $suffix, $i);
50 if (file_exists($dstFilename))
51 unlink($dstFilename);
52
53 if (@rename($srcFilename, $dstFilename) === FALSE)
54 return FALSE;
55 }
56 }
57 return TRUE;
58 }
59
60
61 //
62 // Actual main code begins here
63 //
64 $errorMsg = "";
65 $errorSet = FALSE;
66 $index = "file";
67
68 if (!isset($dataSecret) || !isset($dataPath) ||
69 $dataSecret == "" || $dataPath == "")
70 {
71 error_log("SyntilistaBackup: Invalid configuration.");
72 exit;
73 }
74
75 // Basic check for credentials ..
76 if (isset($_REQUEST["secret"]) && isset($_FILES[$index]) &&
77 ($secret = $_REQUEST["secret"]) == $dataSecret)
78 {
79 $fileName = $_FILES[$index]["tmp_name"];
80 $fileSize = $_FILES[$index]["size"];
81 $fileError = $_FILES[$index]["error"];
82
83 switch ($fileError)
84 {
85 case UPLOAD_ERR_INI_SIZE:
86 stError("File size exceeds PHP's max upload size.");
87 break;
88
89 case UPLOAD_ERR_PARTIAL:
90 stError("File only partially uploaded.");
91 break;
92
93 case UPLOAD_ERR_NO_FILE:
94 stError("No file data received!");
95 break;
96
97 case UPLOAD_ERR_NO_TMP_DIR:
98 stError("Internal error: Temporary file directory not available!");
99 break;
100
101 case UPLOAD_ERR_CANT_WRITE:
102 stError("Internal error: PHP could not write the file to disk.");
103 break;
104
105 case UPLOAD_ERR_OK:
106 break;
107
108 default:
109 stError("Unknown PHP file error occured.");
110 break;
111 }
112
113 if (!$errorSet && $fileSize > $dataMaxSize)
114 {
115 stError("File is too large (".$fileSize." > ".$dataMaxSize." bytes).");
116 }
117
118 if (!$errorSet)
119 {
120 $path = $dataPath."/".$dataName;
121 stRotateBackups($path, $dataSuffix, $dataBackups);
122
123 $dstFilename = stGetBackupFilenameFilename($path, $dataSuffix, 1);
124 if (@move_uploaded_file($fileName, $dstFilename) === false)
125 {
126 stError("Could not move the uploaded file to proper directory.");
127 }
128 }
129
130 if ($errorSet)
131 {
132 header("Status: 500 Internal error");
133 header("Content-Type: text/plain");
134 echo $errorMsg;
135 }
136 }
137 else
138 {
139 header("Status: 403 Forbidden");
140 header("Content-Type: text/plain");
141 }
142 ?>