Merry Christmas & Happy 2027! Less downtime. More good times. Celebrating 13 years of sorlavit.com
Skip to content
Sorlavit KimIT Support, Network & Infrastructure Services Let’s talk

Optional Thai beginner guide ↓

Articles are written in English. The Thai translation button opens Google Translate; code and commands stay in their original form.

System Security / Foundations

A backup needs a recovery test

Perform a real file backup and isolated restore, then detect both a missing file and corrupted content with a SHA-256 manifest.

Start hands-on practice ↓

Compiled for Sorlavit Kim · Published · Updated · 4 min read

Prepare the exact environment

Save the program as restore_check.py and run python3 restore_check.py. It creates three folders inside a new temporary workspace and never accepts a user-supplied deletion path. The source data is synthetic.

Define the recovery boundary and file inventory

The backup boundary contains three small UTF-8 files. Record filenames and SHA-256 digests before copying. A file written after this manifest belongs to a later recovery point and must not silently enter the expected result.

Expected files: customers.txt, settings.txt, readme.txt
Expected recovery point: the source state immediately before copytree

Create, restore and deliberately corrupt the copy

Run the complete script. It verifies a successful restore, changes one restored file, removes another and confirms that each failure is detected. The assertions exercise real filesystem operations in a disposable directory.

from pathlib import Path
from tempfile import TemporaryDirectory
from hashlib import sha256
from time import perf_counter
import shutil

def manifest(root):
    records = {}
    for path in sorted(root.rglob("*")):
        if path.is_file():
            digest = sha256()
            with path.open("rb") as handle:
                for chunk in iter(lambda: handle.read(1024 * 1024), b""):
                    digest.update(chunk)
            records[str(path.relative_to(root))] = digest.hexdigest()
    return records

with TemporaryDirectory(prefix="sorlavit-restore-") as directory:
    workspace = Path(directory)
    source = workspace / "source"
    source.mkdir()
    for name, value in {"customers.txt": "customer-001\n", "settings.txt": "region=lab\n", "readme.txt": "synthetic backup\n"}.items():
        (source / name).write_text(value, encoding="utf-8")
    expected = manifest(source)
    backup = workspace / "backup"
    restored = workspace / "restored"
    shutil.copytree(source, backup)
    (source / "settings.txt").write_text("region=changed-after-backup\n", encoding="utf-8")
    started = perf_counter()
    shutil.copytree(backup, restored)
    assert manifest(restored) == expected
    duration = perf_counter() - started
    (restored / "settings.txt").write_text("corrupt\n", encoding="utf-8")
    assert manifest(restored) != expected
    shutil.copy2(backup / "settings.txt", restored / "settings.txt")
    (restored / "readme.txt").unlink()
    assert manifest(restored) != expected
    shutil.copy2(backup / "readme.txt", restored / "readme.txt")
    assert manifest(restored) == expected
    print("PASS: 3 files restored; corruption and missing-file checks detected")
    print("Observed restore seconds:", round(duration, 6))

Run the same verification against an approved backup copy

For a real drill, choose an existing recovery point and restore it to an isolated destination through the supported backup product. Compare the restored file manifest with the inventory recorded at that recovery point, open representative documents and check permissions.

Record restore start, ready-for-use time, missing files and the person accepting the result. Measure application recovery separately from copying bytes.

Recovery point | expected files | restored files | hash mismatches | permission result | usable-at time

Acceptance check

All three restored files match the pre-copy manifest, and the intentionally corrupted and missing-file cases are detected. The real drill additionally needs permission and application-use checks appropriate to its backup type.

Recovery and cleanup

The script removes only its own TemporaryDirectory. For a real drill, obtain acceptance before removing the isolated restore; keep the original backup untouched and record any changes to recovery permissions.

Use this at work: checks and limits

Use this manifest approach to supplement, not replace, a supported backup product. Add encryption-key recovery, identity dependencies and measured application recovery time to the production runbook.

Practical guide

Download worksheet

Detailed practical guide

Prepare your workspace and inputs

Save the program as restore_check.py and run python3 restore_check.py. It creates three folders inside a new temporary workspace and never accepts a user-supplied deletion path. The source data is synthetic.

Platform context: Python 3.10+; local temporary directories and standard library. This is a file-level recovery test, not a database-consistent backup.

Scenario to reproduce or investigate

A backup job reports success but nobody has checked whether the restored files are complete and unchanged. The test must validate a restore, not just the existence of a backup directory.

Evidence to record in your worksheet
  • Actual target and platform release
  • Exact inputs or substituted values
  • Baseline result
  • Per-step expected and observed output
  • Failure encountered and corrective action
  • Final acceptance and recovery state
  1. Define the recovery boundary and file inventory

    The backup boundary contains three small UTF-8 files. Record filenames and SHA-256 digests before copying. A file written after this manifest belongs to a later recovery point and must not silently enter the expected result.

    Command, configuration or input

    Expected files: customers.txt, settings.txt, readme.txt
    Expected recovery point: the source state immediately before copytree
    Expected result

    The recovery checklist names exact files and their hashes rather than relying on the folder size.

    If it fails

    For a database or live application, replace a plain file copy with that product’s supported consistent backup method; this exercise does not freeze live writes.

  2. Create, restore and deliberately corrupt the copy

    Run the complete script. It verifies a successful restore, changes one restored file, removes another and confirms that each failure is detected. The assertions exercise real filesystem operations in a disposable directory.

    Save and run this example

    from pathlib import Path
    from tempfile import TemporaryDirectory
    from hashlib import sha256
    from time import perf_counter
    import shutil
    
    def manifest(root):
        records = {}
        for path in sorted(root.rglob("*")):
            if path.is_file():
                digest = sha256()
                with path.open("rb") as handle:
                    for chunk in iter(lambda: handle.read(1024 * 1024), b""):
                        digest.update(chunk)
                records[str(path.relative_to(root))] = digest.hexdigest()
        return records
    
    with TemporaryDirectory(prefix="sorlavit-restore-") as directory:
        workspace = Path(directory)
        source = workspace / "source"
        source.mkdir()
        for name, value in {"customers.txt": "customer-001\n", "settings.txt": "region=lab\n", "readme.txt": "synthetic backup\n"}.items():
            (source / name).write_text(value, encoding="utf-8")
        expected = manifest(source)
        backup = workspace / "backup"
        restored = workspace / "restored"
        shutil.copytree(source, backup)
        (source / "settings.txt").write_text("region=changed-after-backup\n", encoding="utf-8")
        started = perf_counter()
        shutil.copytree(backup, restored)
        assert manifest(restored) == expected
        duration = perf_counter() - started
        (restored / "settings.txt").write_text("corrupt\n", encoding="utf-8")
        assert manifest(restored) != expected
        shutil.copy2(backup / "settings.txt", restored / "settings.txt")
        (restored / "readme.txt").unlink()
        assert manifest(restored) != expected
        shutil.copy2(backup / "readme.txt", restored / "readme.txt")
        assert manifest(restored) == expected
        print("PASS: 3 files restored; corruption and missing-file checks detected")
        print("Observed restore seconds:", round(duration, 6))
    
    Expected result

    The script prints PASS: 3 files restored; corruption and missing-file checks detected, followed by the measured restore duration.

    If it fails

    A permission or space error means the restore did not pass. Keep the backup and record the failing operation before retrying on a destination with sufficient capacity.

  3. Run the same verification against an approved backup copy

    For a real drill, choose an existing recovery point and restore it to an isolated destination through the supported backup product. Compare the restored file manifest with the inventory recorded at that recovery point, open representative documents and check permissions.

    Record restore start, ready-for-use time, missing files and the person accepting the result. Measure application recovery separately from copying bytes.

    Command, configuration or input

    Recovery point | expected files | restored files | hash mismatches | permission result | usable-at time
    Expected result

    A recovery record identifies the actual restore point and has evidence that a user can use the restored data.

    If it fails

    If no historical manifest exists, record that limitation and verify known application records or document samples. Do not call the current source folder the historical truth.

Check the result before marking this passed

All three restored files match the pre-copy manifest, and the intentionally corrupted and missing-file cases are detected. The real drill additionally needs permission and application-use checks appropriate to its backup type.

Record the observed result and evidence location in the downloaded worksheet. Mark unresolved or untested conditions explicitly.

Recovery and cleanup

The script removes only its own TemporaryDirectory. For a real drill, obtain acceptance before removing the isolated restore; keep the original backup untouched and record any changes to recovery permissions.

Apply to actual work and known limits

Use this manifest approach to supplement, not replace, a supported backup product. Add encryption-key recovery, identity dependencies and measured application recovery time to the production runbook.

The included self-contained Python fixture was executed successfully in this Linux environment. External products and production infrastructure were not exercised.

Observed local test output

Create, restore and deliberately corrupt the copy

PASS: 3 files restored; corruption and missing-file checks detected
Observed restore seconds: 0.000788
Optional Thai beginner guide · ฉบับภาษาไทยสำหรับผู้เริ่มต้น

เริ่มต้น · ไม่ต้องมีพื้นฐาน

สำรองไฟล์อย่างไรให้กู้กลับมาใช้ได้จริง

แยกการซิงก์ออกจากการสำรอง เริ่มด้วยไฟล์ทดลอง แล้วตรวจว่ากู้คืน เปิดอ่าน และใช้ข้อมูลได้ก่อนวางใจว่ามีแบ็กอัป

เวลาอ่านฉบับภาษาไทยประมาณ 5 นาที

แบ็กอัปคืออะไร

Backup หรือแบ็กอัป คือสำเนาข้อมูลที่เตรียมไว้เพื่อกู้กลับเมื่อข้อมูลหลักเสียหาย สูญหาย หรือถูกเปลี่ยนโดยไม่ตั้งใจ เช่น มีสำเนารายงานที่ยังเปิดได้แม้เครื่องทำงานเสีย

การมีข้อความว่า “สำรองสำเร็จ” ช่วยบอกว่าระบบทำงานบางส่วนแล้ว แต่ยังไม่พิสูจน์ว่าคุณเลือกสำรองครบ มีสิทธิ์กู้ หรือเปิดไฟล์ที่กู้มาได้ การทดลองกู้คืนจึงเป็นส่วนหนึ่งของการสำรอง

การซิงก์ไม่เหมือนการมีสำเนาย้อนหลังเสมอไป

Sync หรือการซิงก์ คือการทำให้ข้อมูลหลายที่ตรงกัน หากคุณลบไฟล์ในที่หนึ่ง การลบอาจส่งต่อไปอีกที่ด้วย บริการบางแห่งมีถังขยะหรือประวัติรุ่น แต่ระยะเวลาเก็บและสิทธิ์กู้ขึ้นกับบริการและการตั้งค่า

ดังนั้นอย่าเดาว่ามีโฟลเดอร์บนคลาวด์แล้วจะย้อนกลับได้ทุกกรณี ให้ตรวจว่ามีประวัติรุ่นใดอยู่ กู้ได้ถึงเมื่อไร และเข้าถึงได้อย่างไรเมื่อบัญชีหรืออุปกรณ์หลักมีปัญหา

ตัวอย่าง: ไฟล์รายรับของร้านถูกเขียนทับ

สมมติว่าคุณแก้ไฟล์รายรับผิดแล้วบันทึกทับ สำเนาที่ดีควรช่วยให้เลือกข้อมูลก่อนแก้ผิดกลับมาได้ หากเก็บไว้เพียงสำเนาล่าสุดที่ถูกเขียนทับตามกัน ก็อาจไม่มีรุ่นที่ต้องการเหลือ

เป้าหมายของการฝึกนี้คือกู้ไฟล์ทดลองหนึ่งไฟล์ไปยังที่ใหม่ โดยไม่ลบหรือเขียนทับงานจริง คุณยังไม่ต้องทดลองให้เครื่องเสียหรือลบข้อมูลสำคัญ

ขั้นที่ 1 เลือกข้อมูลและเขียนเกณฑ์ให้ชัด

สร้างไฟล์ข้อความชื่อ backup-demo.txt ใส่ข้อความสมมติว่า “ยอดขายตัวอย่าง 100 บาท” จดตำแหน่งไฟล์และวันเวลา จากนั้นเลือกวิธีสำรองที่คุณมีสิทธิ์ใช้ เช่น อุปกรณ์สำรองที่ถอดเก็บได้ หรือบริการที่มีประวัติรุ่น

ผลที่ควรได้คือรู้ว่าไฟล์ต้นฉบับอยู่ไหน สำเนาอยู่ไหน และต้องใช้อะไรเข้าถึงสำเนา สำหรับไฟล์งานจริง ให้เจ้าของงานระบุว่าข้อมูลใดสำคัญและยอมเสียข้อมูลย้อนหลังได้แค่ไหน

ขั้นที่ 2 สร้างสำเนา แล้วกู้ไปยังโฟลเดอร์ใหม่

สำรองไฟล์ทดลองตามคู่มือของเครื่องมือ จากนั้นสร้างโฟลเดอร์ชื่อ restore-test และใช้ขั้นตอนกู้คืนหรือคัดลอกจากสำเนามาที่นี่ โดยเก็บต้นฉบับไว้

เปิดไฟล์ที่กู้จาก restore-test แล้วดูว่าข้อความครบและตรงกับรุ่นที่ตั้งใจหรือไม่ ถ้าเปิดไม่ได้ ให้ตรวจสิทธิ์ รหัสถอดรหัส และรุ่นสำเนาที่เลือก อย่าลบต้นฉบับเพื่อบังคับให้การทดสอบดูเหมือนเหตุการณ์จริง

ขั้นที่ 3 จดผลและแยกสำเนาจากปัญหาเดียวกัน

จดเวลาที่เริ่มกู้ เวลาที่เปิดไฟล์ได้ ตำแหน่งสำเนา และผลตรวจข้อความ ถ้าใช้ไดรฟ์ภายนอก ให้ถอดอย่างถูกวิธีและเก็บปลอดภัยหลังงานสำรอง หากใช้บริการออนไลน์ ให้ป้องกันบัญชีสำรองและตรวจว่ามีมาตรการจำกัดการลบอย่างไร

สำเนาที่อยู่ในเครื่องหรือสิทธิ์ชุดเดียวกับข้อมูลหลักอาจเสียหายไปพร้อมกันได้ วิธีแยกสำเนาต้องเหมาะกับงาน เช่น สำเนาออฟไลน์หรือที่จัดเก็บที่จำกัดการแก้ไข ไม่จำเป็นต้องเริ่มจากการซื้อระบบใหญ่

เมื่อไรจึงบอกว่าทดลองสำเร็จ

คุณควรเปิดไฟล์ที่กู้จากสำเนาได้ เนื้อหาตรงรุ่น และอธิบายวิธีกู้ซ้ำได้ การทดสอบนี้พิสูจน์เฉพาะไฟล์ทดลอง ไม่ได้ยืนยันว่าฐานข้อมูลหรือทั้งระบบพร้อมกู้

ขั้นถัดไปคือเลือกไฟล์งานจริงหนึ่งชนิดกับเจ้าของข้อมูล ทดลองในพื้นที่แยก และกำหนดรอบทดสอบใหม่เมื่อเปลี่ยนเครื่องมือ สิทธิ์ หรือรูปแบบงาน อย่าทิ้งสำเนาที่ใช้งานได้ก่อนมีวิธีใหม่ที่ทดสอบผ่าน

อ่านต่อจากแหล่งต้นทาง

ปรับคำอธิบายวันที่ 2026-11-07 · เรียบเรียงคำอธิบายและตัวอย่างสำหรับผู้เริ่มต้น ยังไม่ได้ทดสอบกับบัญชีหรือระบบจริงของผู้อ่าน

Share this article

Back to all articles