upload/download in class
This commit is contained in:
parent
1cc0e98de3
commit
f84e226770
28
backup.py
28
backup.py
@ -68,7 +68,7 @@ class Backup:
|
|||||||
enc = crypt(compress(uri), self.key)
|
enc = crypt(compress(uri), self.key)
|
||||||
self.bdd.set_crypt_attr(crypt_id, compress_mode="gz", sha1sum=get_hash(enc))
|
self.bdd.set_crypt_attr(crypt_id, compress_mode="gz", sha1sum=get_hash(enc))
|
||||||
print(" Size : ", get_size(enc))
|
print(" Size : ", get_size(enc))
|
||||||
upload_file(enc, self.save_mode, file_name=os.path.join(self.save_location, str(crypt_id).zfill(ZFILL)), bucket=self.buk)
|
self.upload_file(enc, file_name=str(crypt_id).zfill(ZFILL))
|
||||||
else:
|
else:
|
||||||
files.append({'name': f,
|
files.append({'name': f,
|
||||||
'path': pathlib.Path(uri).as_posix(),
|
'path': pathlib.Path(uri).as_posix(),
|
||||||
@ -85,13 +85,13 @@ class Backup:
|
|||||||
enc = crypt(compress(tarball), self.key)
|
enc = crypt(compress(tarball), self.key)
|
||||||
self.bdd.set_crypt_attr(crypt_id, compress_mode="tar.gz", sha1sum=get_hash(enc))
|
self.bdd.set_crypt_attr(crypt_id, compress_mode="tar.gz", sha1sum=get_hash(enc))
|
||||||
print(" Size : ", get_size(enc))
|
print(" Size : ", get_size(enc))
|
||||||
upload_file(enc, self.save_mode, file_name=os.path.join(self.save_location, str(crypt_id).zfill(ZFILL)), bucket=self.buk)
|
self.upload_file(enc, file_name=str(crypt_id).zfill(ZFILL))
|
||||||
|
|
||||||
def recover_file(self, paths, parents=False, save_path=os.getcwd()):
|
def recover_file(self, paths, parents=False, save_path=os.getcwd()):
|
||||||
files = self.bdd.get_crypt_name(paths)
|
files = self.bdd.get_crypt_name(paths)
|
||||||
for file in files:
|
for file in files:
|
||||||
if file['crypt_id'] is not None:
|
if file['crypt_id'] is not None:
|
||||||
encrypted_file = download_file(self.save_mode, os.path.join(self.save_location, str(file['crypt_id']).zfill(ZFILL)), bucket=self.buk)
|
encrypted_file = self.download_file(str(file['crypt_id']).zfill(ZFILL))
|
||||||
file_hash = get_hash(encrypted_file)
|
file_hash = get_hash(encrypted_file)
|
||||||
if file['sha1sum'] == file_hash:
|
if file['sha1sum'] == file_hash:
|
||||||
if parents:
|
if parents:
|
||||||
@ -103,20 +103,18 @@ class Backup:
|
|||||||
print("{} {}".format(file_hash, "File"))
|
print("{} {}".format(file_hash, "File"))
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
def upload_file(self, file, file_name):
|
||||||
|
if self.save_mode == BACKBLAZE:
|
||||||
|
self.buk.upload_bytes(file.read(), file_name)
|
||||||
|
elif self.save_mode == LOCAL:
|
||||||
|
save(file, os.path.join(self.save_location, file_name))
|
||||||
|
|
||||||
def upload_file(file, mode, file_name, bucket=None):
|
def download_file(self, file):
|
||||||
if mode == BACKBLAZE:
|
|
||||||
bucket.upload_bytes(file.read(), file_name)
|
|
||||||
elif mode == LOCAL:
|
|
||||||
save(file, file_name)
|
|
||||||
|
|
||||||
|
|
||||||
def download_file(mode, file, bucket=None):
|
|
||||||
dl = tempfile.SpooledTemporaryFile()
|
dl = tempfile.SpooledTemporaryFile()
|
||||||
if mode == BACKBLAZE:
|
if self.save_mode == BACKBLAZE:
|
||||||
bucket.download_file_by_name(file).save(dl)
|
self.buk.download_file_by_name(file).save(dl)
|
||||||
elif mode == LOCAL:
|
elif self.save_mode == LOCAL:
|
||||||
with open(file, 'rb') as infile:
|
with open(os.path.join(self.save_location, file), 'rb') as infile:
|
||||||
dl.write(infile.read())
|
dl.write(infile.read())
|
||||||
return dl
|
return dl
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user