Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

287 строки
8.5KB

  1. <%
  2. ' Backup, verification and restore of the SQLite database (spec NFR-005, AC-17).
  3. ' Run through scripts\backup.cmd, which sets BACKUP_ACTION and BACKUP_FILE:
  4. '
  5. ' backup [file] consistent online copy with VACUUM INTO (default db/backups/app-YYYYMMDD-HHMMSS.sqlite),
  6. ' then verifies the copy and compares its row counts with the live database
  7. ' verify <file> PRAGMA integrity_check, foreign_key_check, latest migration and row counts per table
  8. ' restore <file> verifies <file>, keeps the current database as db/backups/pre-restore-<ts>.sqlite, then copies
  9. ' <file> over the live database. Only with the HTTP server and embed worker STOPPED
  10. ' (scripts\restore.cmd does that); afterwards run scripts\embed.cmd reindex.
  11. '
  12. ' File names are relative to the framework folder (for example db/backups/app-20261205-120000.sqlite) or absolute
  13. ' under /opt/axonasp/site. The framework folder must be the web root: -e SERVER_WEB_ROOT=/opt/axonasp/site
  14. ' Prints "RESULT: ok" or "RESULT: failed" as the last line (the CLI exit code is not reliable, see AGENTS.md).
  15. %>
  16. <!--#include file="../core/autoload_core.asp" -->
  17. <%
  18. Const BK_DIR = "/db/backups"
  19. Const BK_LIVE = "/db/app.sqlite"
  20. Const BK_SITE = "/opt/axonasp/site"
  21. Dim sh : Set sh = CreateObject("WScript.Shell")
  22. Dim fso : Set fso = Server.CreateObject("Scripting.FileSystemObject")
  23. Function Arg(name)
  24. Dim v
  25. v = sh.ExpandEnvironmentStrings("%" & name & "%")
  26. If v = "%" & name & "%" Then v = ""
  27. Arg = Trim(v)
  28. End Function
  29. Sub Say(msg)
  30. Response.Write msg & vbCrLf
  31. End Sub
  32. Function Stamp()
  33. Dim n
  34. n = Now()
  35. Stamp = Year(n) & Right("0" & Month(n), 2) & Right("0" & Day(n), 2) & "-" & _
  36. Right("0" & Hour(n), 2) & Right("0" & Minute(n), 2) & Right("0" & Second(n), 2)
  37. End Function
  38. ' Physical path for a file name given relative to the framework folder (or absolute under it). "" if invalid.
  39. Function PhysPath(name)
  40. Dim p
  41. PhysPath = ""
  42. p = Replace(name, "\", "/")
  43. If InStr(p, "..") > 0 Then Exit Function
  44. If Left(p, Len(BK_SITE) + 1) = BK_SITE & "/" Then
  45. p = Mid(p, Len(BK_SITE) + 1)
  46. End If
  47. If Left(p, 1) <> "/" Then
  48. p = "/" & p
  49. End If
  50. PhysPath = Server.MapPath(p)
  51. End Function
  52. Function SqlText(s)
  53. SqlText = "'" & Replace(s, "'", "''") & "'"
  54. End Function
  55. ' Opens a SQLite file with G3DB; Nothing if it cannot be opened.
  56. Function OpenDb(path)
  57. Dim db
  58. Set OpenDb = Nothing
  59. Set db = Server.CreateObject("G3DB")
  60. If Not db.Open("sqlite", path) Then
  61. Say "ERROR: cannot open " & path & ": " & db.LastError
  62. Exit Function
  63. End If
  64. Set OpenDb = db
  65. End Function
  66. ' Checks a database file and prints its report; returns True when it is sound. counts receives "table=n;..." text.
  67. Function CheckDb(path, ByRef counts)
  68. Dim db, rs, names, i, n, v, ok, fk, latest
  69. CheckDb = False
  70. counts = ""
  71. If Not fso.FileExists(path) Then
  72. Say "ERROR: no such file: " & path
  73. Exit Function
  74. End If
  75. Set db = OpenDb(path)
  76. If db Is Nothing Then
  77. Exit Function
  78. End If
  79. ok = True
  80. Set rs = db.Query("PRAGMA integrity_check")
  81. If IsEmpty(rs) Then
  82. Say "ERROR: integrity_check failed: " & db.LastError
  83. db.Close
  84. Exit Function
  85. End If
  86. v = ""
  87. Do While Not rs.EOF
  88. v = v & rs(0) & " "
  89. rs.MoveNext
  90. Loop
  91. rs.Close
  92. v = Trim(v)
  93. Say "integrity_check: " & v
  94. If v <> "ok" Then
  95. ok = False
  96. End If
  97. Set rs = db.Query("PRAGMA foreign_key_check")
  98. fk = 0
  99. If Not IsEmpty(rs) Then
  100. Do While Not rs.EOF
  101. fk = fk + 1
  102. rs.MoveNext
  103. Loop
  104. rs.Close
  105. End If
  106. Say "foreign_key_violations: " & fk
  107. If fk > 0 Then
  108. ok = False
  109. End If
  110. Set rs = db.Query("SELECT MAX(version) FROM schema_migrations")
  111. latest = ""
  112. If Not IsEmpty(rs) Then
  113. If Not rs.EOF Then
  114. latest = "" & rs(0)
  115. End If
  116. rs.Close
  117. End If
  118. Say "latest_migration: " & latest
  119. If latest = "" Then
  120. ok = False
  121. End If
  122. ' Table names first (close the result set before counting).
  123. names = ""
  124. Set rs = db.Query("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name")
  125. If Not IsEmpty(rs) Then
  126. Do While Not rs.EOF
  127. names = names & rs(0) & vbLf
  128. rs.MoveNext
  129. Loop
  130. rs.Close
  131. End If
  132. names = Split(names, vbLf)
  133. For i = 0 To UBound(names) - 1
  134. Set rs = db.Query("SELECT COUNT(*) FROM """ & Replace(names(i), """", """""") & """")
  135. n = "?"
  136. If Not IsEmpty(rs) Then
  137. If Not rs.EOF Then
  138. n = "" & rs(0)
  139. End If
  140. rs.Close
  141. End If
  142. counts = counts & names(i) & "=" & n & ";"
  143. Next
  144. db.Close
  145. Say "tables: " & (UBound(names))
  146. CheckDb = ok
  147. End Function
  148. Function DoBackup()
  149. Dim target, db, res, counts, liveCounts, sz, t0
  150. DoBackup = False
  151. If Arg("BACKUP_FILE") = "" Then
  152. If Not fso.FolderExists(Server.MapPath(BK_DIR)) Then
  153. fso.CreateFolder Server.MapPath(BK_DIR)
  154. End If
  155. target = Server.MapPath(BK_DIR & "/app-" & Stamp() & ".sqlite")
  156. Else
  157. target = PhysPath(Arg("BACKUP_FILE"))
  158. End If
  159. If target = "" Then
  160. Say "ERROR: invalid file name"
  161. Exit Function
  162. End If
  163. If fso.FileExists(target) Then
  164. Say "ERROR: file exists: " & target
  165. Exit Function
  166. End If
  167. Set db = OpenDb(Server.MapPath(BK_LIVE))
  168. If db Is Nothing Then
  169. Exit Function
  170. End If
  171. t0 = Timer
  172. ' VACUUM INTO writes a consistent, compacted copy while the server keeps running (readers and writers wait).
  173. Set res = db.Exec("VACUUM INTO " & SqlText(target))
  174. If IsEmpty(res) Then
  175. Say "ERROR: VACUUM INTO failed: " & db.LastError
  176. db.Close
  177. Exit Function
  178. End If
  179. db.Close
  180. sz = fso.GetFile(target).Size
  181. Say "backup: " & target
  182. Say "bytes: " & sz
  183. Say "seconds: " & FormatNumber(Timer - t0, 2, -1, 0, 0)
  184. If Not CheckDb(target, counts) Then
  185. Exit Function
  186. End If
  187. Say "-- live database"
  188. If Not CheckDb(Server.MapPath(BK_LIVE), liveCounts) Then
  189. Exit Function
  190. End If
  191. If counts = liveCounts Then
  192. Say "row_counts: match"
  193. Else
  194. ' A write between the copy and the comparison also gives a difference; rerun verify if the server is busy.
  195. Say "row_counts: differ (backup " & counts & " live " & liveCounts & ")"
  196. End If
  197. DoBackup = True
  198. End Function
  199. Function DoVerify()
  200. Dim path, counts
  201. DoVerify = False
  202. path = PhysPath(Arg("BACKUP_FILE"))
  203. If path = "" Then
  204. Say "ERROR: give the backup file (BACKUP_FILE)"
  205. Exit Function
  206. End If
  207. If Not CheckDb(path, counts) Then
  208. Exit Function
  209. End If
  210. Say "row_counts: " & counts
  211. DoVerify = True
  212. End Function
  213. Function DoRestore()
  214. Dim src, live, safety, counts, f
  215. DoRestore = False
  216. src = PhysPath(Arg("BACKUP_FILE"))
  217. live = Server.MapPath(BK_LIVE)
  218. If src = "" Then
  219. Say "ERROR: give the backup file (BACKUP_FILE)"
  220. Exit Function
  221. End If
  222. If LCase(src) = LCase(live) Then
  223. Say "ERROR: the backup is the live database"
  224. Exit Function
  225. End If
  226. If Not CheckDb(src, counts) Then
  227. Say "ERROR: the backup did not verify; nothing was changed"
  228. Exit Function
  229. End If
  230. If fso.FileExists(live) Then
  231. If Not fso.FolderExists(Server.MapPath(BK_DIR)) Then
  232. fso.CreateFolder Server.MapPath(BK_DIR)
  233. End If
  234. safety = Server.MapPath(BK_DIR & "/pre-restore-" & Stamp() & ".sqlite")
  235. fso.CopyFile live, safety, False
  236. Say "previous database kept as: " & safety
  237. End If
  238. For Each f In Array(live & "-wal", live & "-shm", live & "-journal")
  239. If fso.FileExists(f) Then
  240. fso.DeleteFile f, True
  241. End If
  242. Next
  243. fso.CopyFile src, live, True
  244. Say "restored: " & src & " -> " & live
  245. Say "-- restored database"
  246. If Not CheckDb(live, counts) Then
  247. Exit Function
  248. End If
  249. Say "row_counts: " & counts
  250. DoRestore = True
  251. End Function
  252. Dim action, ok
  253. action = LCase(Arg("BACKUP_ACTION"))
  254. ok = False
  255. Select Case action
  256. Case "backup"
  257. ok = DoBackup()
  258. Case "verify"
  259. ok = DoVerify()
  260. Case "restore"
  261. ok = DoRestore()
  262. Case Else
  263. Say "Usage: BACKUP_ACTION=backup|verify|restore [BACKUP_FILE=db/backups/<file>.sqlite]"
  264. End Select
  265. If ok Then
  266. Say "RESULT: ok"
  267. Else
  268. Say "RESULT: failed"
  269. End If
  270. %>

Powered by TurnKey Linux.