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.
47 lines
1.3 KiB
47 lines
1.3 KiB
#!/usr/bin/python
|
|
from backup import hashify
|
|
import argparse
|
|
import os
|
|
import shutil
|
|
import sqlite3
|
|
import time
|
|
import time
|
|
|
|
def create_temp():
|
|
name= 'fsb{}'.format(hex(time.time_ns())[2:])
|
|
os.mkdir(name)
|
|
return name
|
|
|
|
def lookup(cur,imohash,realhash=None):
|
|
if realhash is not None:
|
|
cur.execute('SELECT path FROM paths where blake=?',[realhash])
|
|
else:
|
|
cur.execute('SELECT path FROM paths where imohash=?',[imohash])
|
|
res = cur.fetchall()
|
|
return res
|
|
|
|
def restore(database,source,destination):
|
|
con=sqlite3.connect(database)
|
|
cur=con.cursor()
|
|
temp = create_temp()
|
|
hashes = hashify(source)
|
|
for path,imohash,realhash in hashes:
|
|
ppath = os.path.join(source,path)
|
|
qpath=lookup(cur,imohash,realhash)
|
|
qpath=os.path.join(temp,qpath)
|
|
parent = os.path.dirname(qpath)
|
|
if not os.path.exists(parent):
|
|
os.makedirs(parent)
|
|
print(ppath,qpath,sep = '->')
|
|
os.rename(ppath,qpath)
|
|
os.rename(temp,destination)
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('database')
|
|
parser.add_argument('src')
|
|
parser.add_argument('dst',default = None,nargs='?')
|
|
args = parser.parse_args()
|
|
if args.dst is None:
|
|
args.dst=args.src
|
|
restore(args.database,args.src,args.dst)
|