-
Notifications
You must be signed in to change notification settings - Fork 0
/
intuitbackuputil.py
135 lines (111 loc) · 5.04 KB
/
intuitbackuputil.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, sys, time
import toml
from dataclasses import dataclass
@dataclass
class Config:
dest_dir: str
record_dir: str
info_dir: str
password: str
class IntuitBackup():
def __init__(self):
self.__username = os.getenv('SUDO_USER') or os.getenv('USER')
if not self.__username:
sys.stderr.write("Failed to get username\n")
sys.exit(1)
elif self.__username == "root":
self.__home_dir = "/root"
else:
self.__home_dir = os.path.join("/home", self.__username)
config_path = os.path.join(self.__home_dir, ".config", "intuitbackup", "config.toml")
with open(config_path, 'r') as file:
config_dict = toml.load(file)
self.__config = Config(**config_dict)
self.__config.dest_dir = self.__expanduser(self.__config.dest_dir)
self.__config.record_dir = self.__expanduser(self.__config.record_dir)
self.__config.info_dir = self.__expanduser(self.__config.info_dir)
if os.geteuid() != 0:
os.makedirs(self.__config.dest_dir, exist_ok=True)
os.makedirs(self.__config.record_dir, exist_ok=True)
os.makedirs(self.__config.info_dir, exist_ok=True)
return
def backup(self, record_name):
archive_name = record_name + '-' + time.strftime('%Y%m%d_%H%M%S', time.localtime()) + ".tar.gz"
archive_path = os.path.join(self.__config.dest_dir, archive_name)
record_path = os.path.join(self.__config.record_dir, record_name)
files = self.__get_files(record_path)
self.backup_helper(archive_path, files)
print(f'Backup successfully: "{archive_path}"')
def backup_helper(self, archive_path, files):
files_with_quote = []
for f in files:
files_with_quote.append(f'"{f}"')
#cmd = f'tar -acvP -f "{archive_path}" {" ".join(files_with_quote)}'
cmd = f"tar -acvP -f - {' '.join(files_with_quote)} | gpg --batch --yes --symmetric --cipher-algo AES256 --passphrase '{self.__config.password}' --output '{archive_path}.gpg'"
os.system(cmd)
def check_record(self, record_name):
record_path = os.path.join(self.__config.record_dir, record_name)
files = self.__get_files(record_path)
for f in files:
if not os.path.exists(f):
print(f'"{f}" is not exist.')
def cat_record(self, record_name):
record_path = os.path.join(self.__config.record_dir, record_name)
files = self.__get_files(record_path)
print("\n".join(files))
def extract(self, archive_name):
oldwd = os.getcwd()
os.chdir(self.__config.dest_dir)
file_name_no_suffix = archive_name.split(".")[0]
os.mkdir(file_name_no_suffix)
archive_path = os.path.join(self.__config.dest_dir, archive_name)
#cmd = f'tar -xv -p --same-owner -f "{archive_path}" -C "{file_name_no_suffix}"'
cmd = f"gpg --batch --yes --passphrase '{self.__config.password}' -d {archive_path} | tar -xv -p --same-owner -f - -C '{file_name_no_suffix}'"
os.system(cmd)
os.chdir(oldwd)
def restore(self, archive_name):
archive_path = os.path.join(self.__config.dest_dir, archive_name)
#cmd = f'tar -xvP -p --same-owner -f {archive_path}'
cmd = f"gpg --batch --yes --passphrase '{self.__config.password}' -d {archive_path} | tar -xvP -p --same-owner -f -"
os.system(cmd)
def list_files(self, archive_name):
archive_path = os.path.join(self.__config.dest_dir, archive_name)
#cmd = f'tar -tv -f {archive_path}'
cmd = f"gpg --batch --yes --passphrase '{self.__config.password}' -d {archive_path} | tar -tv -f -"
os.system(cmd)
def backup_info(self):
oldwd = os.getcwd()
os.chdir(self.__config.info_dir)
cmd = f'cd "{self.__config.info_dir}"; ./info_cmd'
print(cmd)
os.system(cmd)
os.chdir(oldwd)
record_name = "cmd_info"
archive_name = record_name + '-' + time.strftime('%Y%m%d_%H%M%S', time.localtime()) + ".tar.gz"
archive_path = os.path.join(self.__config.dest_dir, archive_name)
self.backup_helper(archive_path, [self.__config.info_dir])
print(f'Backup successfully: "{archive_path}"')
def get_dest_dir(self):
return self.__config.dest_dir
def get_record_dir(self):
return self.__config.record_dir
def get_username(self):
return self.__username
def get_home_dir(self):
return self.__home_dir
def __get_files(self, record_path):
# get files from the record
files = []
with open(record_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
files.append(self.__expanduser(line))
return files
def __expanduser(self, path):
if path.startswith("~"):
return self.__home_dir + path[1:]
return path