-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzbackup
261 lines (236 loc) · 9.62 KB
/
zbackup
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/local/bin/python3
import sys, argparse
import os
def main():
target_data = ''
ID = ''
rotation_count = ''
parser = argparse.ArgumentParser(usage="zbackup [[--list | --delete | --export] target-dataset [ID] | [-import] target-dataset filename | target dataset [rotation count]]")
parser.add_argument('--list', nargs='*')
parser.add_argument('--delete', nargs='+')
parser.add_argument('--export', nargs='+')
parser.add_argument('--import', nargs=2, dest="_import")
parser.add_argument('target_dataset', nargs='*')
args = parser.parse_args()
if args.list != None:
if len(args.list) == 2:
target_data = args.list[0]
ID = args.list[1]
if len(args.list) == 1:
target_data = args.list[0]
if len(args.list) > 2:
parser.print_help()
zfs_list = os.popen('sudo zfs list -t snapshot').readlines()
ids = ["ID"]
datasets = ["dataset"]
time = ["time"]
one_counter = 0
two_counter = 0
for i in range(len(zfs_list)-1):
data = zfs_list[i+1].split(" ")[0]
data = data.split("@")
dt = data[1].split("_")
year = str(dt[5])
day = str(dt[2])
month = ""
if dt[1] == "Jan":
month = str(1)
elif dt[1] == "Feb":
month = str(2)
elif dt[1] == "Mar":
month = str(3)
elif dt[1] == "Apr":
month = str(4)
elif dt[1] == "May":
month = str(5)
elif dt[1] == "Jun":
month = str(6)
elif dt[1] == "Jul":
month = str(7)
elif dt[1] == "Aug":
month = str(8)
elif dt[1] == "Sep":
month = str(9)
elif dt[1] == "Oct":
month = str(10)
elif dt[1] == "Nov":
month = str(11)
elif dt[1] == "Dec":
month = str(12)
datetime = year+"-"+month+"-"+day+" "+dt[3]
if len(args.list) == 0:
ids.append(i+1)
datasets.append(data[0])
time.append(datetime)
if len(args.list) == 1:
if target_data == str(data[0]):
ids.append(one_counter+1)
one_counter += 1
datasets.append(data[0])
time.append(datetime)
if len(args.list) == 2:
if target_data == str(data[0]):
two_counter += 1
if target_data == str(data[0]) and two_counter == int(ID):
ids.append(two_counter)
datasets.append(data[0])
time.append(datetime)
print(str(ids[0])+" "+str(datasets[0])+" "+str(time[0]))
for i in range(len(ids)):
if i != 0:
print(str(ids[i])+" "+str(datasets[i])+" "+str(time[i]))
#print("List func: ", target_data, "and ID: ", ID)
elif args.delete != None:
if len(args.delete) > 2:
parser.print_help()
zfs_list = os.popen('sudo zfs list -t snapshot').readlines()
delete_counter = 0
if len(args.delete) == 1:
target_data = args.delete[0]
for i in range(len(zfs_list)-1):
this_data = zfs_list[i+1].split(" ")[0]
data = this_data.split("@")
if target_data == str(data[0]):
os.system('sudo zfs destroy '+this_data)
if len(args.delete) == 2:
ID = args.delete[1]
target_data = args.delete[0]
for i in range(len(zfs_list)-1):
this_data = zfs_list[i+1].split(" ")[0]
data = this_data.split("@")
if target_data == str(data[0]):
delete_counter += 1
if int(ID) == delete_counter:
os.system('sudo zfs destroy '+this_data)
#print("Delete func: ", target_data, "and ID: ", ID)
elif args.export != None:
target_data = args.export[0]
if len(args.export) == 2:
ID = args.export[1]
if len(args.export) == 1:
ID = 1
zfs_list = os.popen('sudo zfs list -t snapshot').readlines()
counter = 0
export_name = ""
export_dataset = ""
dt = str(os.popen('date').read())
dt = dt.replace("\n","")
dt = dt.split(" ")
year = str(dt[5])
day = str(dt[2])
month = ""
if dt[1] == "Jan":
month = str(1)
elif dt[1] == "Feb":
month = str(2)
elif dt[1] == "Mar":
month = str(3)
elif dt[1] == "Apr":
month = str(4)
elif dt[1] == "May":
month = str(5)
elif dt[1] == "Jun":
month = str(6)
elif dt[1] == "Jul":
month = str(7)
elif dt[1] == "Aug":
month = str(8)
elif dt[1] == "Sep":
month = str(9)
elif dt[1] == "Oct":
month = str(10)
elif dt[1] == "Nov":
month = str(11)
elif dt[1] == "Dec":
month = str(12)
for i in range(len(zfs_list)-1):
all_data = zfs_list[i+1].split(" ")[0]
data = all_data.split("@")
if target_data == data[0]:
counter += 1
if counter == int(ID) and target_data == data[0]:
export_name = all_data
export_dataset = data[0]
export_dataset = export_dataset.replace("/","-")
output_data_name = export_dataset+"@"+year+"-"+month+"-"+day
os.system('sudo zfs send '+export_name+' > '+output_data_name)
os.system('xz '+output_data_name)
os.system('openssl enc -aes-256-cbc -in '+output_data_name+'.xz -out '+output_data_name+'.xz.enc')
os.system('rm '+output_data_name+'.xz')
#print("Export func: ", target_data, "and ID: ", ID)
elif args._import != None:
if len(args._import) < 2:
parser.print_help()
else:
target_data = args._import[0]
filename = args._import[1]
xz_filename = filename.replace(".enc","")
zfs_filename = xz_filename.replace(".xz","")
zfs_list = os.popen('sudo zfs list -t snapshot').readlines()
for i in range(len(zfs_list)-1):
this_data = zfs_list[i+1].split(" ")[0]
data = this_data.split("@")[0]
if data == target_data:
os.system('sudo zfs destroy '+this_data)
os.system('openssl enc -aes-256-cbc -d -in '+filename+' > '+xz_filename)
os.system('xz -d '+xz_filename)
os.system('sudo zfs receive -F '+target_data+' < '+zfs_filename)
#os.system('rm '+filename)
os.system('rm '+zfs_filename)
#print("Import func: ", target_data)
else:
if(len(args.target_dataset) == 0):
parser.print_help()
else:
target_data = args.target_dataset[0]
if len(args.target_dataset) == 2:
rotation_count = args.target_dataset[1]
else:
rotation_count = 20
dt = str(os.popen('date').read())
dt = dt.replace("\n","")
dt = dt.replace(" ","_")
# get zfs list of snapshot
zfs_list = os.popen('sudo zfs list -t snapshot').readlines()
target_counter = 0
for i in range(len(zfs_list)-1):
data = zfs_list[i+1].split(" ")[0]
data = data.split("@")[0]
if target_data == data:
target_counter += 1
# length of zfs list larger then rotation count
if target_counter > int(rotation_count):
rotate_data = []
rotate_counter = 0
for i in range(len(zfs_list)-1):
all_data = zfs_list[i+1].split(" ")[0]
data = all_data.split("@")[0]
if target_data == data and rotate_counter < target_counter-int(rotation_count)+1:
rotate_counter += 1
rotate_data.append(all_data)
for i in range(len(rotate_data)):
os.system('sudo zfs destroy '+rotate_data[i])
print("Rotate "+rotate_data[i])
os.system('sudo zfs snapshot '+target_data+'@'+dt)
print("Snap "+target_data+"@"+dt)
# length of zfs list equal rotation count
elif target_counter == int(rotation_count):
rotate_data = []
find_old = 0
for i in range(len(zfs_list)-1):
all_data = zfs_list[i+1].split(" ")[0]
data = all_data.split("@")[0]
if data == target_data and not find_old:
rotate_data = all_data
find_all = 1
os.system('sudo zfs destroy '+rotate_data)
print("Rotate "+rotate_data)
os.system('sudo zfs snapshot '+target_data+'@'+dt)
print("Snap "+target_data+"@"+dt)
# length of zfs list smaller then rotation count
elif target_counter < int(rotation_count):
os.system('sudo zfs snapshot '+target_data+'@'+dt)
print("Snap "+target_data+"@"+dt)
#print("Default func: ", target_data, " and rotation: ", rotation_count)
if __name__ == "__main__":
main()