Jas*_*nTS 5 python linux hardlink duplicates nas
我是摄影师,做了很多备份。多年来,我发现自己拥有许多硬盘驱动器。现在,我购买了NAS,并使用rsync将所有图片复制到一个3TB的RAID 1上。根据我的脚本,其中大约1TB的文件是重复的。这是因为在删除笔记本电脑上的文件之前进行了多次备份,而且非常混乱。我的确在旧硬盘上备份了所有这些文件,但是如果我的脚本搞砸了,那将很痛苦。您能否看一下我重复的查找程序脚本,并告诉我您是否认为我可以运行它?我在测试文件夹上进行了尝试,看起来还可以,但是我不想在NAS上弄乱东西。
该脚本在三个文件中包含三个步骤。在第一部分中,我将找到所有图像和元数据文件,并将它们作为大小的文件放入货架数据库(datenbank)中。
import os
import shelve
datenbank = shelve.open(os.path.join(os.path.dirname(__file__),"shelve_step1"), flag='c', protocol=None, writeback=False)
#path_to_search = os.path.join(os.path.dirname(__file__),"test")
path_to_search = "/volume1/backup_2tb_wd/"
file_exts = ["xmp", "jpg", "JPG", "XMP", "cr2", "CR2", "PNG", "png", "tiff", "TIFF"]
walker = os.walk(path_to_search)
counter = 0
for dirpath, dirnames, filenames in walker:
if filenames:
for filename in filenames:
counter += 1
print str(counter)
for file_ext in file_exts:
if file_ext in filename:
filepath = os.path.join(dirpath, filename)
filesize = str(os.path.getsize(filepath))
if not filesize in datenbank:
datenbank[filesize] = []
tmp = datenbank[filesize]
if filepath not in tmp:
tmp.append(filepath)
datenbank[filesize] = tmp
datenbank.sync()
print "done"
datenbank.close()
Run Code Online (Sandbox Code Playgroud)
第二部分。现在,我删除所有列表中只有一个文件的文件大小,并创建另一个以md5哈希为键,文件列表为值的搁置数据库。
import os
import shelve
import hashlib
datenbank = shelve.open(os.path.join(os.path.dirname(__file__),"shelve_step1"), flag='c', protocol=None, writeback=False)
datenbank_step2 = shelve.open(os.path.join(os.path.dirname(__file__),"shelve_step2"), flag='c', protocol=None, writeback=False)
counter = 0
space = 0
def md5Checksum(filePath):
with open(filePath, 'rb') as fh:
m = hashlib.md5()
while True:
data = fh.read(8192)
if not data:
break
m.update(data)
return m.hexdigest()
for filesize in datenbank:
filepaths = datenbank[filesize]
filepath_count = len(filepaths)
if filepath_count > 1:
counter += filepath_count -1
space += (filepath_count -1) * int(filesize)
for filepath in filepaths:
print counter
checksum = md5Checksum(filepath)
if checksum not in datenbank_step2:
datenbank_step2[checksum] = []
temp = datenbank_step2[checksum]
if filepath not in temp:
temp.append(filepath)
datenbank_step2[checksum] = temp
print counter
print str(space)
datenbank_step2.sync()
datenbank_step2.close()
print "done"
Run Code Online (Sandbox Code Playgroud)
最后是最危险的部分。对于evrey md5键,我检索了文件列表并执行了另外的sha1。如果匹配,则删除列表中除第一个文件外的每个文件,并创建一个硬链接来替换已删除的文件。
import os
import shelve
import hashlib
datenbank = shelve.open(os.path.join(os.path.dirname(__file__),"shelve_step2"), flag='c', protocol=None, writeback=False)
def sha1Checksum(filePath):
with open(filePath, 'rb') as fh:
m = hashlib.sha1()
while True:
data = fh.read(8192)
if not data:
break
m.update(data)
return m.hexdigest()
for hashvalue in datenbank:
switch = True
for path in datenbank[hashvalue]:
if switch:
original = path
original_checksum = sha1Checksum(path)
switch = False
else:
if sha1Checksum(path) == original_checksum:
os.unlink(path)
os.link(original, path)
print "delete: ", path
print "done"
Run Code Online (Sandbox Code Playgroud)
你怎么看?非常感谢你。
*如果这很重要:它是Synology 713+,具有ext3或ext4文件系统。
为什么不逐字节比较文件而不是第二个校验和?十亿分之一的两个校验和可能会意外匹配,但直接比较应该不会失败。它不应该更慢,甚至可能更快。当有两个以上的文件并且您必须互相读取原始文件时,可能会更慢。如果您确实想要,可以通过一次比较所有文件的块来解决这个问题。
编辑:
我认为这不需要更多代码,只是不同。循环体是这样的:
data1 = fh1.read(8192)
data2 = fh2.read(8192)
if data1 != data2: return False
Run Code Online (Sandbox Code Playgroud)