-
Notifications
You must be signed in to change notification settings - Fork 2
/
detect_inode_moves.py
462 lines (417 loc) · 19.7 KB
/
detect_inode_moves.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'Miikka Veijonen, Pavel Kr'
__version__ = '2.0'
__copyright__ = 'Copyright: (C) 2021 Miikka Veijonen, Pavel Krc'
__license__ = 'GPLv2+'
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import hashlib
import re
import sys
import signal
import threading
import time
import datetime
import shutil
import random
import string
import argparse
inode_move_script_begin = '''#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import shutil
import random
import string
import re
import sys
fmoves = {}
finalfmoves = {}
finalfmovesrev = {}
failedmoves = {}
def id_generator(size=8, chars=string.ascii_letters + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def prepare_move(a, b):
global fmoves
global finalfmoves
global finalfmovesrev
global failedmoves
srcdir = os.path.dirname(a)
dstdir = os.path.dirname(b)
dsttmpdir = dstdir
fname = os.path.basename(a)
while True:
if dsttmpdir == '/':
break
if re.match('^' + dsttmpdir + '/', srcdir + '/'):
break
dsttmpdir = os.path.dirname(dsttmpdir)
while True:
ftmp = dsttmpdir + '/.' + fname + '_' + id_generator()
if not os.path.isdir(ftmp) and not os.path.isfile(ftmp) and not os.path.islink(ftmp):
break
print('Temporarily moving file: ' + a + ' => ' + ftmp)
try:
os.rename(a, ftmp)
except:
print('Could not move temporarily file: ' + a + ' => ' + ftmp)
failedmoves[a] = b
else:
finalfmoves[a] = ftmp
finalfmovesrev[ftmp] = a
fmoves[a] = b
def finish_moves():
global fmoves
global finalfmoves
global finalfmovesrev
global failedmoves
for f in finalfmoves:
skipthis = False
for ff in failedmoves:
if re.match('^' + ff, fmoves[f]):
print('Previously failed (and reverted) file move (' + ff + ') is part of this file\\'s final move destination: ' + finalfmoves[f] + ' => ' + fmoves[f] + ', skipping this one (and reverting the temp move: ' + finalfmoves[f] + ' => ' + f +')')
try:
os.rename(finalfmoves[f], f)
except:
print('Could not revert the temp move: ' + finalfmoves[f] + ' => ' + f)
skipthis = True
break
if skipthis:
continue
if os.path.isdir(fmoves[f]):
try:
shutil.rmtree(fmoves[f])
except Exception as e:
print('Could not remove destination before the final file move, the file move will fail: ' + finalfmoves[f] + ' => ' + fmoves[f])
fdestdir = os.path.dirname(fmoves[f])
if not os.path.isdir(fdestdir):
try:
print('Creating directory (recursively): ' + fdestdir)
os.makedirs(fdestdir)
except:
print('Could not create the destination directory (' + fdestdir + '), the file move will fail')
try:
print('Moving temporarily moved file to its final location: ' + finalfmoves[f] + ' => ' + fmoves[f])
os.rename(finalfmoves[f], fmoves[f])
except:
print('Could not perform final move (reverting the temp move): ' + finalfmoves[f] + ' => ' + f)
try:
os.rename(finalfmoves[f], f)
except:
print('Could not revert the temp move: ' + finalfmoves[f] + ' => ' + f)
else:
failedmoves[f] = finalfmovesrev[finalfmoves[f]]
'''
def find_mount_point(path):
path = os.path.abspath(path)
orig_dev = os.stat(path).st_dev
while path != '/':
dir = os.path.dirname(path)
if os.stat(dir).st_dev != orig_dev:
# we crossed the device border
break
path = dir
return path
def find_files_with_exceptions(rootdir, d, edirsenabled, cexcludeddirsregex, rsyncefile, rsyncefilehandle):
global filecount
global dircount
global lutime
dmountpoint = find_mount_point(d)
dmountpointhash = hashlib.sha256(dmountpoint.encode('utf-8')).hexdigest()
stlog('VERBOSE', 'Scanning directory: ' + d)
filehashes = {}
try:
fds = os.scandir(d)
except Exception as e:
stlog('WARNING', 'Could not go in to directory: ' + d + ' (' + str(e) + ')')
return False
for f in fds:
try:
ttt = f.path.encode('UTF-8').decode('UTF-8')
except Exception as e:
stlog('WARNING', 'Could not read filename/path properly (most probably some charset issue): ' + f.path.encode('UTF-8','ignore').decode('UTF-8') + ' (' + str(e).encode('UTF-8','ignore').decode('UTF-8') + ')')
continue
if (lutime + 10) < int(time.time()):
stlog('INFO', 'Scanning... (scanned: ' + str(dircount) + ' directories and ' + str(filecount) + ' files, currently scanning directory: ' + d + ')')
lutime = int(time.time())
if f.is_file() and not f.is_symlink():
filecount +=1
finode = os.lstat(f.path).st_ino
filehashes[f.path] = [ dmountpointhash, finode ]
elif f.is_dir():
if f.is_symlink():
stlog('VERBOSE', 'Directory is a symlink, not following: ' + f.path)
continue
if edirsenabled and re.match(cexcludeddirsregex, f.path):
stlog('INFO', 'Directory excluded, not scanning: ' + f.path)
if rsyncefile:
rsyncline = re.sub("^" + rootdir , '', f.path.encode('UTF-8','ignore').decode('UTF-8'))
rsyncefilehandle.write(rsyncline + "/\n")
continue
dircount +=1
try:
fsubdirhashes = find_files_with_exceptions(rootdir, f.path, edirsenabled, cexcludeddirsregex, rsyncefile, rsyncefilehandle)
except:
continue
if type(filehashes) is dict:
filehashes.update(fsubdirhashes)
else:
continue
return filehashes
def id_generator(size=8, chars=string.ascii_letters + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def find_for_moved_files(origfilehashes, filehashes, rootdirold, rootdirnew):
global verbose_mode
global lutime
filestobemoved = {}
movedfdc = 0
mtc = len(origfilehashes)
for of in list(origfilehashes):
movedfdc +=1
if (lutime + 10) < int(time.time()):
stlog('INFO', 'Detecting moved files... (scanned: ' + str(movedfdc) + '/' + str(mtc) + ' files)')
lutime = int(time.time())
ffound=False
ofnpath = re.sub('^' + rootdirnew + '/', rootdirold + '/', of)
if not of in origfilehashes:
# Already removed file/key
continue
if os.path.isfile(ofnpath) and not os.path.islink(ofnpath):
try:
filehashes[ofnpath][0]
except:
pass
else:
if origfilehashes[of][0] == filehashes[ofnpath][0] and origfilehashes[of][1] == filehashes[ofnpath][1]:
# Found an existing file which is still in the same location as well
origfilehashes.pop(of)
filehashes.pop(ofnpath)
continue
for f in list(filehashes):
if not f in filehashes:
break
if origfilehashes[of][0] == filehashes[f][0] and origfilehashes[of][1] == filehashes[f][1]:
oldfilewithorigrootdir = re.sub('^' + rootdirold + '/', rootdirnew + '/', of)
newfilewithorigrootdir = re.sub('^' + rootdirold + '/', rootdirnew + '/', f)
stlog('VERBOSE', 'Found moved file: ' + oldfilewithorigrootdir + ' => ' + newfilewithorigrootdir)
filestobemoved[newfilewithorigrootdir] = oldfilewithorigrootdir
origfilehashes.pop(of)
filehashes.pop(f)
ffound=True
break
if ffound == False:
stlog('VERBOSE', 'File ' + of + ' has been removed')
stlog('INFO', 'File move detection finished (scanned: ' + str(movedfdc) + '/' + str(mtc) + ' files)')
return filestobemoved
def write_move_file(filestobemoved, outputsourcereffilehandle, rootdir):
global lutime
delaymovedfiles = {}
tempmovedfiles = {}
sortedfilestobemoved = sorted(filestobemoved, key=lambda x: (-x.count('/')))
tam = len(filestobemoved)
outputsourcereffilehandle.write('%s\n' % inode_move_script_begin)
cline = 'if not os.path.isdir(\'' + rootdir + '\'):\n print(\'ERROR: Root directory ' + rootdir + ' is not a directory or it does not exist, aborting.\')\n sys.exit(1)\n\n'
outputsourcereffilehandle.write(cline)
tpc = 0
for f in sortedfilestobemoved:
tpc +=1
if (lutime + 10) < int(time.time()):
stlog('INFO', 'Writing the output file... (written ' + str(tpc) + '/' + str(tam) + ' file moves already)')
lutime = int(time.time())
renl = 'prepare_move(\'' + filestobemoved[f].encode('UTF-8','ignore').decode('UTF-8').replace("'", "\\'") + '\', \'' + f.encode('UTF-8','ignore').decode('UTF-8').replace("'", "\\'") + '\')\n'
outputsourcereffilehandle.write(renl)
outputsourcereffilehandle.write('finish_moves()\n')
stlog('INFO', 'Output file writing finished (wrote: ' + str(tpc) + '/' + str(tpc) + ' file moves)')
def main_thread(rootdir, edirsenabled, cexcludeddirsregex, inputsourcereffile, outputsourcereffile, rsyncefile):
global verbose_mode
global filecount
global dircount
global lutime
filehashesnew = {}
filecount = 0
dircount = 0
lutime = int(time.time())
if outputsourcereffile != None and inputsourcereffile == None:
if not os.path.isdir(rootdir):
stlog('ERROR', 'Root directory does not exists: ' + rootdir)
return False
rsyncefilehandle=False
stlog('INFO', 'Starting to scan the files from the root dir: ' + rootdir)
# Reading the filepaths, inodes and mount hashes
try:
filehashesnew = find_files_with_exceptions(rootdir, rootdir, edirsenabled, cexcludeddirsregex, rsyncefile, rsyncefilehandle)
except Exception as e:
stlog('ERROR', 'Could not scan ' + rootdir + ' for directories and files (' + str(e) + ')')
return False
else:
if filehashesnew == False:
stlog('ERROR', 'Could not scan ' + rootdir + ' for directories and files')
return False
stlog('INFO', 'Root dir scan finished (scanned ' + str(dircount) + ' directories which contained in total ' + str(filecount) + ' files)')
stlog('INFO', 'Writing the scan result to inode dump file: ' + outputsourcereffile)
# Saving the data to the inode dump file
try:
filelistfile = open(outputsourcereffile, "w")
except Exception as e:
stlog('ERROR', 'Could not open output inode dump writing: ' + outputsourcereffile + ' (' + str(e) + ')')
return False
filelistfile.write(rootdir + '\n')
if edirsenabled:
filelistfile.write('%s\n' % cexcludeddirsregex)
else:
filelistfile.write('%s\n' % ['__NO-EXCLUDED-DIRS__'])
for f in filehashesnew:
try:
fenc = f.encode('UTF-8').decode('UTF-8')
except Exception as e:
stlog('ERROR', 'Could not store filename/path properly (most probably some charset issue): ' + f.encode('UTF-8','ignore').decode('UTF-8') + ' (' + str(e) + ')')
continue
filelistfile.write(filehashesnew[f][0] + ' ' + str(filehashesnew[f][1]) + ' ' + f + '\n')
filelistfile.close()
return
elif inputsourcereffile != None and outputsourcereffile != None:
# Reading the old filepath, inode and mount hash information from the inode dump file
filehashesold = {}
rsyncefilehandle=False
if rsyncefile:
stlog('INFO', 'Writing the Rsync dir exclusion file to: ' + rsyncefile)
try:
rsyncefilehandle = open(rsyncefile, "w")
except Exception as e:
stlog('ERROR', 'Could not open Rsync dir exclusion file for the writing: ' + rsyncefile + ' (' + str(e) + ')')
return False
stlog('INFO', 'Reading the inode dump file: ' + inputsourcereffile)
try:
f = open(inputsourcereffile, "r")
except Exception as e:
stlog('ERROR', 'Could not open inode dump file for the reading: ' + inputsourcereffile + ' (' + str(e) + ')')
return False
fr = 0
for l in f:
if fr == 0:
rootdirold = l.rstrip('\n')
fr = 1
continue
elif fr == 1:
cexcludeddirsregex = l.rstrip('\n')
if (cexcludeddirsregex == '[\'__NO-EXCLUDED-DIRS__\']'):
edirsenabled=False
else:
edirsenabled=True
fr = -1
continue
la = l.split(' ', 2)
filehashesold[la[2].rstrip('\n')] = [ la[0], int(la[1]) ]
f.close()
if not rootdir:
rootdir = rootdirold
stlog('INFO', 'Starting to re-scan the files from the original root directory: ' + rootdirold)
# Reading the current filepaths, inodes and mount hashes
try:
filehashesnew = find_files_with_exceptions(rootdirold, rootdirold, edirsenabled, cexcludeddirsregex, rsyncefile, rsyncefilehandle)
except Exception as e:
stlog('ERROR', 'File scan failed (' + str(e) + ')')
return False
else:
if filehashesnew == False:
stlog('ERROR', 'Could not scan ' + rootdirold + ' for directories and files')
return False
stlog('INFO', 'Originial root directory scan finished (scanned ' + str(dircount) + ' directories which contained in total ' + str(filecount) + ' files)')
try:
outputsourcereffilehandle = open(outputsourcereffile, "w")
except Exception as e:
stlog('ERROR', 'Could not open output rename script for writing: ' + outputsourcereffile + ' (' + str(e) + ')')
return False
stlog('INFO', 'Detecting the moved files and preparing the moves if found...')
filestobemoved = {}
filestobemoved = find_for_moved_files(filehashesold, filehashesnew, rootdirold, rootdir)
if len(filestobemoved) > 0:
stlog('INFO', 'Writing the file moving script...')
write_move_file(filestobemoved, outputsourcereffilehandle, rootdir)
else:
stlog('INFO', 'No moved files found, nothing to do')
outputsourcereffilehandle.close()
return
def quit_func(signum, frame):
stlog('WARNING', 'Script interrupted, aborting.\n')
sys.exit(0)
def stlog(level, msg):
global verbose_mode
global begin_time
if level == 'VERBOSE':
if verbose_mode:
print(str(datetime.datetime.now() - begin_time) + ' ' + level + ': ' + msg, end="\n", flush=True)
else:
print(str(datetime.datetime.now() - begin_time) + ' ' + level + ': ' + msg, end="\n", flush=True)
verbose_mode = False
begin_time = datetime.datetime.now()
def main():
global verbose_mode
global __version__
global __copyright__
global __license__
# Parsing arguments
my_parser = argparse.ArgumentParser()
my_parser = argparse.ArgumentParser(description='File move(s) detection and execution script (designed to be executed before the actual Rsync is executed), Version: ' + __version__ + ', ' + __copyright__ + ', License: '+ __license__)
my_parser.add_argument('-d', '--root-dir', action='store', type=str, help='Specify the root dir (when reading existing inode dump file, this can be set to the target system\'s root dir so the correct rootdir will be set to the output rename script file).')
my_parser.add_argument('-e','--excluded-dirs',action='append',type=str, help='Set the directories that should be excluded from the source directory scan, in regular expression (regexp) format (example: -e ".*/\.btrfs$" -e "^/home/user/tmp$"). NOTE: can be set multiple times for multiple exclusions if needed and this setting has effect only when "--source-dir" is used. NOTE2: the exclusions are saved to the "output source dir reference file" if it is used.')
my_parser.add_argument('-o', '--output-file', action='store', type=str, required=True, help='Set the output inode dump file location, or if used with -i / --input-file argument, this will define the output rename script file\'s location')
my_parser.add_argument('-i', '--input-file', action='store', type=str, help='Set the input inode dump file location. This is used in combination with -o / --output-file argument.')
my_parser.add_argument('-r', '--dir-exclusion-file-for-rsync', action='store', type=str, required=False, help='Set the output file for excluded dir list file for Rsync. NOTE: This should be used only during file move detection phase (=both -i / --input-file and -o / --output-file arguments are set) and when -e / --excluded-dirs argument(s) are used.')
my_parser.add_argument('--verbose', action='store_true', required=False, help='Run the script in verbose mode')
args = my_parser.parse_args()
rootdir = args.root_dir
outputsourcereffile = args.output_file
inputsourcereffile = args.input_file
# Checking arguments and required / prohibited combinations
if args.dir_exclusion_file_for_rsync:
rsyncefile=args.dir_exclusion_file_for_rsync
else:
rsyncefile=False
if inputsourcereffile == None and rsyncefile != False:
print('-r / --dir-exclusion-file-for-rsync can be used only during inode dump')
quit(1)
elif inputsourcereffile == None and rootdir == None:
print('-d / --root-dir must be set for inode dump (no -i / --input-file is set)')
quit(1)
if rootdir != None:
rootdir = rootdir.rstrip('/')
else:
rootdir = False
if args.verbose:
verbose_mode = True
else:
verbose_mode = False
if args.excluded_dirs:
edirsenabled=True
excluded_dirs_regex = args.excluded_dirs
cexcludeddirsregex = "(" + ")|(".join(excluded_dirs_regex) + ")"
else:
cexcludeddirsregex = ""
edirsenabled=False
stlog('INFO', 'Script execution started')
# Performing the scanning and move detection in a separate thread
# because it's very I/O instensive operation and the interruption
# of the script by using CTRL+C is not working very well.
work_thread = threading.Thread(target=main_thread, args=(rootdir, edirsenabled, cexcludeddirsregex, inputsourcereffile, outputsourcereffile, rsyncefile))
work_thread.daemon = True
work_thread.start()
signal.signal(signal.SIGINT, quit_func)
while True:
time.sleep(1)
if not work_thread.is_alive():
stlog('INFO', 'Script exectuion finished')
sys.exit(0)
if __name__ == "__main__":
main()