蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

备份与恢复:真实本地字节校验

PYTHON本地Python3.12/SQLite;PHP8.4.27 -n;回环HTTPbackup_restore.py
backup_restore.py15 行
from pathlib import Path
import tempfile,zipfile,hashlib,json
ROOT=Path(__file__).resolve().parent
names=['index.html','site.css','logo.svg'];source={n:(ROOT/'public'/n).read_bytes() for n in names}
with tempfile.TemporaryDirectory(prefix='mbsky-owned-restore-') as folder:
 base=Path(folder).resolve();archive=base/'example.zip';destination=base/'restored';destination.mkdir()
 with zipfile.ZipFile(archive,'w',zipfile.ZIP_DEFLATED) as z:
  for name,data in source.items():z.writestr(name,data)
 with zipfile.ZipFile(archive) as z:
  assert z.testzip() is None
  for info in z.infolist():
   target=(destination/info.filename).resolve();assert target.is_relative_to(destination.resolve()) and not target.exists();target.write_bytes(z.read(info))
 assert all((destination/n).read_bytes()==b for n,b in source.items())
 print(json.dumps({'restored':[{'file':n,'sha256':hashlib.sha256(b).hexdigest()} for n,b in source.items()],'scope':'owned three-file local restore, not vendor daily backup SLA'},ensure_ascii=False))
本例需在 本地Python3.12/SQLite;PHP8.4.27 -n;回环HTTP 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地Python3.12/SQLite;PHP8.4.27 -n;回环HTTP

入口文件:projects/hosting-lab/backup_restore.py

预期结果与验证条件观察提示
  • ZIP CRC正常,三份恢复文件与原字节相同
  • 不代表第三方每日备份SLA

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:网站主机教程

课程关联工程包(登记关联11章) · 含当前章节或实例 · mbskycom202611a516.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习