15 lines
417 B
Python
15 lines
417 B
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import shutil
|
||
|
|
from datetime import datetime
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
|
||
|
|
def backup_file(source: Path, backup_dir: Path) -> None:
|
||
|
|
if not source.exists():
|
||
|
|
return
|
||
|
|
backup_dir.mkdir(parents=True, exist_ok=True)
|
||
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||
|
|
target = backup_dir / f"{source.stem}_{timestamp}{source.suffix}"
|
||
|
|
shutil.copy2(source, target)
|