import os import tarfile from datetime import datetime import sys # Import sys for command line arguments from BunnyCDN.Storage import Storage def is_hidden(path): """ Check if the given path is a hidden folder or file. """ return path.startswith('.') or '/.' in path def should_exclude(path, excluded_items): """ Check if the given path should be excluded. """ if is_hidden(path): return True for item in excluded_items: if path.startswith(item): return True return False def backup(folder_path, excluded_folders=[], excluded_files=[]): """ Create a compressed backup of the specified folder, excluding specified items and hidden folders. """ timestamp = int(datetime.timestamp(datetime.now())) backup_file = os.path.join(folder_path, f'backup-{timestamp}.tar.gz') with tarfile.open(backup_file, "w:gz") as tar: for root, dirs, file_names in os.walk(folder_path): if should_exclude(root, excluded_folders): continue for file_name in file_names: file_path = os.path.join(root, file_name) if should_exclude(file_path, excluded_files): continue print("Adding %s" % file_path) tar.add(file_path, arcname=os.path.relpath(file_path, start=folder_path)) return backup_file if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: python script.py ") sys.exit(1) folder_path = sys.argv[1] if not os.path.isdir(folder_path): print(f"Error: The folder '{folder_path}' does not exist.") sys.exit(1) backup_file = backup(folder_path) obj_storage = Storage('99f4c72b-2674-4e6a-a1825c269cc0-b959-48a1', 'ab-backups') obj_storage.PutFile(backup_file, f'backups/{os.path.basename(backup_file)}') print("Backup and upload successful.")