Дедуплицированный снимок
Создаёт новый снимок папки, связывая неизменённые файлы с прошлым снимком жёсткими ссылками.
Что делает скрипт
Скрипт находит последний snapshot в каталоге назначения и сравнивает размер и время файлов. Одинаковые файлы получают жёсткую ссылку, изменённые копируются заново. По умолчанию показывается план, запись включается флагом --apply.
- Не редактируйте файлы внутри старых снимков: жёсткие ссылки общие.
Быстрый старт
python dedup_snapshot.py --source ~/data --dest /mnt/backup/snapshots --apply
Скачайте файл, откройте терминал в папке со скриптом и выполните команду.
В Linux и macOS интерпретатор может называться python3, в Windows - python или py.
Другие варианты
python dedup_snapshot.py --source ~/data --dest /mnt/backup/snapshotspython dedup_snapshot.py --source ~/data --dest /mnt/backup/snapshots --apply --verify
Что понадобится
winget install Python.Python.3.12
brew install python
sudo apt install python3
dedup_snapshot.py
#!/usr/bin/env python3
"""Снимок папки с дедупликацией неизменённых файлов через жёсткие ссылки."""
from __future__ import annotations
import argparse
import filecmp
import json
import os
import shutil
import sys
from datetime import datetime
from pathlib import Path
def is_inside(child: Path, parent: Path) -> bool:
try:
child.relative_to(parent)
return True
except ValueError:
return False
def find_previous(root: Path, explicit: Path | None) -> Path | None:
if explicit is not None:
candidate = explicit.expanduser().resolve()
if not candidate.is_dir():
raise ValueError(f"Предыдущий снимок не найден: {candidate}")
return candidate
candidates = [path for path in root.glob("snapshot-*") if path.is_dir()]
if not candidates:
return None
return max(candidates, key=lambda path: path.stat().st_mtime_ns)
def unchanged(source: Path, previous: Path, verify: bool) -> bool:
if not previous.is_file():
return False
try:
source_stat = source.stat()
previous_stat = previous.stat()
except OSError:
return False
if source_stat.st_size != previous_stat.st_size:
return False
if source_stat.st_mtime_ns != previous_stat.st_mtime_ns:
return False
if verify:
return filecmp.cmp(source, previous, shallow=False)
return True
def iter_files(source: Path):
for current, dirs, files in os.walk(source):
dirs[:] = sorted(name for name in dirs if not Path(current, name).is_symlink())
for name in sorted(files):
path = Path(current, name)
if path.is_symlink() or not path.is_file():
continue
yield path
def main() -> int:
parser = argparse.ArgumentParser(description="Снимок с дедупликацией")
parser.add_argument("--source", type=Path, required=True)
parser.add_argument("--dest", type=Path, required=True, help="Корень снимков")
parser.add_argument("--previous", type=Path, help="Конкретный предыдущий снимок")
parser.add_argument("--apply", action="store_true", help="Создать снимок")
parser.add_argument("--verify", action="store_true", help="Сравнивать содержимое файлов")
args = parser.parse_args()
source = args.source.expanduser().resolve()
dest = args.dest.expanduser().resolve()
if not source.is_dir():
parser.error(f"Источник не найден: {source}")
if source == dest:
parser.error("Источник и назначение совпадают")
if is_inside(dest, source) or is_inside(source, dest):
parser.error("Источник и назначение не должны вкладываться друг в друга")
try:
previous = find_previous(dest, args.previous)
except ValueError as error:
parser.error(str(error))
files = list(iter_files(source))
if not args.apply:
same = 0
for path in files:
if previous is not None:
candidate = previous / path.relative_to(source)
if unchanged(path, candidate, args.verify):
same += 1
print(f"Файлов: {len(files)}, можно связать с прошлым снимком: {same}")
if previous is not None:
print(f"Предыдущий снимок: {previous}")
print("Предпросмотр. Для создания снимка добавьте --apply.")
return 0
stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
target = dest / f"snapshot-{stamp}"
index = 1
while target.exists():
target = dest / f"snapshot-{stamp}-{index}"
index += 1
target.mkdir(parents=True)
linked = 0
copied = 0
linked_bytes = 0
entries = []
errors = 0
for path in files:
relative = path.relative_to(source)
target_path = target / relative
target_path.parent.mkdir(parents=True, exist_ok=True)
previous_path = previous / relative if previous is not None else None
entry = {"path": relative.as_posix(), "size": path.stat().st_size, "linked": False}
try:
if previous_path is not None and unchanged(path, previous_path, args.verify):
try:
os.link(previous_path, target_path)
linked += 1
linked_bytes += entry["size"]
entry["linked"] = True
entries.append(entry)
continue
except OSError:
pass
shutil.copy2(path, target_path)
copied += 1
entries.append(entry)
except OSError as error:
errors += 1
print(f"Ошибка {relative}: {error}", file=sys.stderr)
manifest = {
"created": stamp,
"source": str(source),
"previous": str(previous) if previous is not None else "",
"files": entries,
"linked": linked,
"copied": copied,
"linked_bytes": linked_bytes,
"errors": errors,
}
manifest_path = target / "manifest.json"
manifest_path.write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print(f"Снимок: {target}")
print(f"Связано: {linked}, скопировано: {copied}, ошибок: {errors}")
print(f"Сэкономлено байт: {linked_bytes}")
return 1 if errors else 0
if __name__ == "__main__":
sys.exit(main())
Скрипты работают с файлами и настройками системы. Перед запуском прочитайте код и проверьте пути. Особенно если скрипт удаляет, перезаписывает или перемещает данные.