fix proceed multi files, format query
This commit is contained in:
parent
17b42a14b7
commit
1827794ec5
50
Backup.py
50
Backup.py
@ -23,14 +23,12 @@ class Backup:
|
||||
def recurse(self, path):
|
||||
tarball_size = parse_size(self.tarball_size)
|
||||
files = []
|
||||
print("Start", path, ":", files)
|
||||
for f in os.listdir(path):
|
||||
uri = os.path.join(path, f)
|
||||
if os.path.isfile(uri):
|
||||
size = os.path.getsize(uri)
|
||||
m_date = datetime.fromtimestamp(os.path.getmtime(uri))
|
||||
c_date = datetime.fromtimestamp(os.path.getctime(uri))
|
||||
print(f + " : ", human_size(size))
|
||||
m_date = datetime.fromtimestamp(os.path.getmtime(uri)).strftime("%Y-%m-%d %H:%M:%S.%f")
|
||||
c_date = datetime.fromtimestamp(os.path.getctime(uri)).strftime("%Y-%m-%d %H:%M:%S.%f")
|
||||
if size > tarball_size:
|
||||
crypt_name = self.bdd.add([{'name': f,
|
||||
'path': path,
|
||||
@ -39,7 +37,7 @@ class Backup:
|
||||
'c_date': c_date}],
|
||||
compress_mode="gz")
|
||||
if crypt_name is not None:
|
||||
print("proceed")
|
||||
print("Proceed", uri)
|
||||
enc = crypt(compress(uri), self.key)
|
||||
save(enc, os.path.join(self.save_location, crypt_name))
|
||||
else:
|
||||
@ -51,10 +49,9 @@ class Backup:
|
||||
elif os.path.isdir(uri):
|
||||
self.recurse(uri)
|
||||
if len(files) > 0:
|
||||
print("End", path, ":", [file['name'] for file in files])
|
||||
crypt_name = self.bdd.add(files, compress_mode="tar.gz")
|
||||
if crypt_name is not None:
|
||||
print("proceed")
|
||||
print("Proceed", path, ":", [file['name'] for file in files])
|
||||
tarball = tar(files)
|
||||
enc = crypt(compress(tarball), self.key)
|
||||
save(enc, os.path.join(self.save_location, crypt_name))
|
||||
@ -148,7 +145,7 @@ class DataBase:
|
||||
id INTEGER PRIMARY KEY UNIQUE NOT NULL,
|
||||
name TEXT,
|
||||
path TEXT,
|
||||
size TEXT,
|
||||
size INTEGER,
|
||||
m_date DATE,
|
||||
c_date DATE,
|
||||
crypt_id INTEGER,
|
||||
@ -193,13 +190,13 @@ class DataBase:
|
||||
cursor.execute("""SELECT name, path, size, m_date, c_date FROM files WHERE name=? AND path=?""",
|
||||
(file['name'], file['path']))
|
||||
bdd_file = cursor.fetchone()
|
||||
for key in ['m_date', 'c_date']:
|
||||
bdd_file[key] = datetime.strptime(bdd_file[key], "%Y-%m-%d %H:%M:%S.%f")
|
||||
bdd_file['size'] = int(bdd_file['size'])
|
||||
if file != bdd_file:
|
||||
return True
|
||||
else:
|
||||
# for key in ['m_date', 'c_date']:
|
||||
# bdd_file[key] = datetime.strptime(bdd_file[key], "%Y-%m-%d %H:%M:%S.%f")
|
||||
# bdd_file['size'] = int(bdd_file['size'])
|
||||
if file == bdd_file:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def add(self, list_file, compress_mode=None):
|
||||
cursor = self.conn.cursor()
|
||||
@ -207,21 +204,26 @@ class DataBase:
|
||||
if crypt_id is None:
|
||||
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as crypt_id FROM crypt""")
|
||||
crypt_id = cursor.fetchone()['crypt_id']
|
||||
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as files_id FROM files""")
|
||||
file_id = cursor.fetchone()['files_id']
|
||||
proceed = False
|
||||
for file in list_file:
|
||||
if self.exist(file):
|
||||
if self.modified(file):
|
||||
cursor.execute("""UPDATE files SET size=?, m_date=?, c_date=?, crypt_id=?
|
||||
WHERE name=? AND path=?""",
|
||||
cursor.execute("""UPDATE files SET size=?, m_date=?, c_date=?, crypt_id=? WHERE name=? AND path=?""",
|
||||
(file['size'], file['m_date'], file['c_date'], crypt_id, file['name'], file['path']))
|
||||
else:
|
||||
return None
|
||||
proceed = True
|
||||
else:
|
||||
cursor.execute("""INSERT INTO files (name, path, size, m_date, c_date, crypt_id)
|
||||
VALUES(?, ?, ?, ?, ?, ?)""", (file['name'], file['path'], file['size'], file['m_date'], file['c_date'], crypt_id))
|
||||
|
||||
cursor.execute("""INSERT OR IGNORE INTO crypt VALUES(?, ?)""", (crypt_id, compress_mode))
|
||||
self.conn.commit()
|
||||
return str(crypt_id)
|
||||
cursor.execute("""INSERT INTO files VALUES({id}, '{name}', '{path}', '{size}', '{m_date}', '{c_date}', {crypt_id})""".format(
|
||||
id=file_id, crypt_id=crypt_id, **file))
|
||||
file_id += 1
|
||||
proceed = True
|
||||
if proceed:
|
||||
cursor.execute("""INSERT OR IGNORE INTO crypt VALUES(?, ?)""", (crypt_id, compress_mode))
|
||||
self.conn.commit()
|
||||
return str(crypt_id).zfill(5)
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def human_size(size, decimal_places=0):
|
||||
|
Loading…
x
Reference in New Issue
Block a user