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