You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB

  1. #!/usr/bin/python
  2. from imohash.imohash import hashfile as _imohash
  3. import argparse
  4. import hashlib
  5. import os
  6. import psutil
  7. import sqlite3
  8. # https://stackoverflow.com/a/17782753
  9. def file_hash(path, block_size=4096*8):
  10. '''
  11. Block size directly depends on the block size of your filesystem
  12. to avoid performances issues
  13. Here I have blocks of 4096 octets (Default NTFS)
  14. '''
  15. _hash = hashlib.blake2b()
  16. if os.path.getsize(path) < psutil.virtual_memory().available:
  17. split = False
  18. else:
  19. split = True
  20. with open(path,'rb') as f:
  21. if split:
  22. for chunk in iter(lambda: f.read(block_size), b''):
  23. _hash.update(chunk)
  24. else:
  25. bytes = f.read()
  26. _hash.update(bytes)
  27. return _hash.digest()
  28. def backup(top,db_path):
  29. paths = hashify(top)
  30. con = __init_database__(db_path)
  31. cur = con.cursor()
  32. cur.executemany('INSERT OR IGNORE INTO `paths` VALUES (?,?,?);',paths)
  33. con.commit()
  34. if __name__ == "__main__":
  35. parser = argparse.ArgumentParser()
  36. parser.add_argument('dir')
  37. parser.add_argument('-d','--database',default='fs.db')
  38. args = parser.parse_args()
  39. __init_database__(args.database)
  40. backup(args.dir,args.database)