Update to allow zip file creation
This commit is contained in:
47
backup.py
47
backup.py
@@ -2,26 +2,52 @@
|
||||
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 local_backup(output_dir: Path, backup_name: str, inputs: list, ignore_patterns: list):
|
||||
def should_filter_file(filters, file_path):
|
||||
return any([filter_name in str(file_path) for filter_name in filters])
|
||||
|
||||
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()
|
||||
archive_path = output_dir / f"{backup_name}-{current_time}.tar.gz"
|
||||
filter_function = partial(tar_filter, ignore_patterns)
|
||||
print(archive_path)
|
||||
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)
|
||||
|
||||
if archive_type == "zip":
|
||||
archive_path = output_dir / f"{backup_name}-{current_time}.zip"
|
||||
print(archive_path)
|
||||
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))
|
||||
else:
|
||||
archive_path = output_dir / f"{backup_name}-{current_time}.tar.gz"
|
||||
filter_function = partial(tar_filter, ignore_patterns)
|
||||
print(archive_path)
|
||||
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)
|
||||
|
||||
print(f"Total time: {(time.time() - start) / 60}mins")
|
||||
|
||||
@@ -38,6 +64,7 @@ def backup_all():
|
||||
inputs = config["inputs"]
|
||||
ignore_patterns = config["ignorePatterns"]
|
||||
backup_name = file.stem
|
||||
local_backup(output_dir, backup_name, inputs, ignore_patterns)
|
||||
archive_type = config.get("archiveType", "tar")
|
||||
local_backup(output_dir, backup_name, inputs, ignore_patterns, archive_type)
|
||||
if __name__ == "__main__":
|
||||
backup_all()
|
||||
@@ -8,5 +8,6 @@
|
||||
],
|
||||
"ignorePatterns": [
|
||||
".git/"
|
||||
]
|
||||
],
|
||||
"archiveType": "zip"
|
||||
}
|
||||
Reference in New Issue
Block a user