76 lines
3.1 KiB
Python
Executable File
76 lines
3.1 KiB
Python
Executable File
#!/usr/bin/python3
|
|
from pathlib import Path
|
|
import json
|
|
import tarfile
|
|
import zipfile
|
|
import datetime
|
|
from functools import partial
|
|
import time
|
|
import os
|
|
def tar_filter(filters, tarinfo):
|
|
if any([filter_name in tarinfo.name for filter_name in filters]):
|
|
print(f"\tFiltering: {tarinfo.name}")
|
|
return None
|
|
return tarinfo
|
|
|
|
def should_filter_file(filters, file_path):
|
|
return any([filter_name in str(file_path) for filter_name in filters])
|
|
|
|
def create_zip_archive(archive_path: Path, inputs: list, ignore_patterns: list):
|
|
with zipfile.ZipFile(archive_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
|
for file_or_dir in inputs:
|
|
file_or_dir = Path(file_or_dir).expanduser().resolve()
|
|
print(f"Compressing: {file_or_dir}")
|
|
|
|
if file_or_dir.is_file():
|
|
if not should_filter_file(ignore_patterns, file_or_dir):
|
|
zipf.write(file_or_dir, arcname=file_or_dir.name)
|
|
else:
|
|
for root, dirs, files in os.walk(file_or_dir):
|
|
root_path = Path(root)
|
|
for file in files:
|
|
file_path = root_path / file
|
|
if not should_filter_file(ignore_patterns, file_path):
|
|
relative_path = file_path.relative_to(file_or_dir.parent)
|
|
zipf.write(file_path, arcname=str(relative_path))
|
|
|
|
def create_tar_archive(archive_path: Path, inputs: list, ignore_patterns: list):
|
|
filter_function = partial(tar_filter, ignore_patterns)
|
|
with tarfile.open(archive_path, "w:xz") as f:
|
|
for file_or_dir in inputs:
|
|
file_or_dir = Path(file_or_dir).expanduser().resolve()
|
|
print(f"Compressing: {file_or_dir}")
|
|
f.add(file_or_dir, filter=filter_function)
|
|
|
|
def local_backup(output_dir: Path, backup_name: str, inputs: list, ignore_patterns: list, archive_type="tar"):
|
|
current_time = datetime.datetime.now().isoformat()
|
|
start = time.time()
|
|
|
|
if archive_type == "zip":
|
|
archive_path = output_dir / f"{backup_name}-{current_time}.zip"
|
|
print(archive_path)
|
|
create_zip_archive(archive_path, inputs, ignore_patterns)
|
|
else:
|
|
archive_path = output_dir / f"{backup_name}-{current_time}.tar.gz"
|
|
print(archive_path)
|
|
create_tar_archive(archive_path, inputs, ignore_patterns)
|
|
|
|
print(f"Total time: {(time.time() - start) / 60}mins")
|
|
|
|
|
|
def backup_all():
|
|
config_dir = Path(__file__).parent / "configs"
|
|
config_files = sorted(config_dir.glob("*"))
|
|
print(config_files)
|
|
for file in config_files:
|
|
text = file.read_text()
|
|
config = json.loads(text)
|
|
output_dir = Path(config["outputDir"]).expanduser().resolve()
|
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
inputs = config["inputs"]
|
|
ignore_patterns = config["ignorePatterns"]
|
|
backup_name = file.stem
|
|
archive_type = config.get("archiveType", "tar")
|
|
local_backup(output_dir, backup_name, inputs, ignore_patterns, archive_type)
|
|
if __name__ == "__main__":
|
|
backup_all() |