summaryrefslogtreecommitdiff
path: root/scripts/backup.py
blob: 753988f4bcbb923a01f314ba6a2a9e8a0fdb5e6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import sys
import json
import subprocess

folders_created = 0
folders_existed = 0
files_uploaded = 0
files_existed = 0
files_replaced = 0

def run_cmd(cmd):
    print(f"Running: {cmd}")
    p = subprocess.run(cmd, shell=True, capture_output=True, text=True)
    if p.stderr:
        print(f"Error running command: {p.stderr}")
        raise Exception(f"Command failed: {cmd} - {p.stderr}")
    try:
        return json.loads(p.stdout)
    except json.JSONDecodeError:
        print(f"Non-JSON output: {p.stdout}")
        raise Exception(f"Invalid JSON from: {cmd}")

def get_listing(remote_id):
    cmd = f'internxt list --json --non-interactive --id="{remote_id}"'
    out = run_cmd(cmd)
    if out.get('success', False):
        return out['list']
    else:
        print(f"List failed: {out.get('message', 'Unknown error')}")
        raise Exception(f"Failed to list {remote_id}")

def find_folder_uuid(parent_id, name):
    listing = get_listing(parent_id)
    for folder in listing['folders']:
        if folder['plainName'] == name:
            print(f"Folder '{name}' exists in '{parent_id}', uuid: {folder['uuid']}")
            return folder['uuid']
    return None

def create_folder(parent_id, name):
    cmd = f'internxt create-folder --json --non-interactive --name="{name}" --id="{parent_id}"'
    out = run_cmd(cmd)
    if out.get('success', False):
        uuid = out['folder']['uuid']
        print(f"Created folder '{name}' in '{parent_id}', uuid: {uuid}")
        return uuid
    else:
        print(f"Failed to create folder '{name}': {out.get('message', 'Unknown error')}")
        raise Exception(f"Failed to create folder {name}")

def find_or_create_folder(parent_id, name):
    global folders_created, folders_existed
    uuid = find_folder_uuid(parent_id, name)
    if uuid:
        folders_existed += 1
        return uuid
    else:
        folders_created += 1
        return create_folder(parent_id, name)

def find_file_uuid(parent_uuid, full_name):
    stem, ext = os.path.splitext(full_name)
    ext = ext.lstrip('.')
    listing = get_listing(parent_uuid)
    for file in listing['files']:
        if file['plainName'] == stem and file.get('type', '') == ext:
            print(f"File '{full_name}' exists in '{parent_uuid}', uuid: {file['uuid']}")
            return file['uuid']
    return None

def upload_file(local_path, dest_uuid):
    cmd = f'internxt upload-file --json --non-interactive --file="{local_path}" --destination="{dest_uuid}"'
    out = run_cmd(cmd)
    if out.get('success', False):
        print(f"Uploaded '{local_path}' to '{dest_uuid}'")
        return True
    else:
        print(f"Failed to upload '{local_path}': {out.get('message', 'Unknown error')}")
        return False

def trash_file(file_uuid):
    cmd = f'internxt trash-file --json --non-interactive --id="{file_uuid}"'
    out = run_cmd(cmd)
    if out.get('success', False):
        print(f"Trashed file '{file_uuid}'")
        return True
    else:
        print(f"Failed to trash '{file_uuid}': {out.get('message', 'Unknown error')}")
        return False

def handle_file(local_path, dest_uuid, mode):
    global files_uploaded, files_existed, files_replaced
    full_name = os.path.basename(local_path)
    print(f"Handling file '{local_path}' in '{dest_uuid}' (mode: {mode})")
    existing_uuid = find_file_uuid(dest_uuid, full_name)
    if not existing_uuid:
        if upload_file(local_path, dest_uuid):
            files_uploaded += 1
    else:
        if mode == 'append':
            print(f"File '{full_name}' exists, skipping")
            files_existed += 1
        elif mode == 'replace':
            if trash_file(existing_uuid):
                if upload_file(local_path, dest_uuid):
                    files_replaced += 1
                else:
                    print(f"Failed to replace '{local_path}' after trashing")
            else:
                print(f"Failed to trash '{full_name}', skipping replacement")

def backup_dir(local_dir, remote_uuid, mode):
    print(f"Backing up directory '{local_dir}' to '{remote_uuid}'")
    for entry in os.listdir(local_dir):
        local_path = os.path.join(local_dir, entry)
        if os.path.isdir(local_path):
            sub_uuid = find_or_create_folder(remote_uuid, entry)
            backup_dir(local_path, sub_uuid, mode)
        elif os.path.isfile(local_path):
            handle_file(local_path, remote_uuid, mode)
        else:
            print(f"Skipping non-file/non-dir '{local_path}'")

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python backup.py <local_dir> <mode: append|replace>")
        sys.exit(1)
    local_root = sys.argv[1]
    mode = sys.argv[2].lower()
    if mode not in ['append', 'replace']:
        print("Invalid mode: must be 'append' or 'replace'")
        sys.exit(1)
    print(f"Local root directory: {local_root}")
    base_name = os.path.basename(os.path.abspath(local_root))
    root_parent = ""
    remote_root_uuid = find_or_create_folder(root_parent, base_name)
    backup_dir(local_root, remote_root_uuid, mode)
    print("\nBackup Summary:")
    print(f"Folders created: {folders_created}")
    print(f"Folders existed: {folders_existed}")
    print(f"Files uploaded (new): {files_uploaded}")
    print(f"Files existed (skipped): {files_existed}")
    if mode == 'replace':
        print(f"Files replaced (trashed + uploaded): {files_replaced}")