first
This commit is contained in:
BIN
backup_db/jobs/__pycache__/backup_db.cpython-39.pyc
Normal file
BIN
backup_db/jobs/__pycache__/backup_db.cpython-39.pyc
Normal file
Binary file not shown.
BIN
backup_db/jobs/__pycache__/delete_files.cpython-39.pyc
Normal file
BIN
backup_db/jobs/__pycache__/delete_files.cpython-39.pyc
Normal file
Binary file not shown.
31
backup_db/jobs/backup_db.py
Normal file
31
backup_db/jobs/backup_db.py
Normal file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright Hersel Giannella
|
||||
|
||||
import os
|
||||
import time
|
||||
import subprocess
|
||||
import pymysql
|
||||
from config import Config
|
||||
|
||||
# set backup directory and filename
|
||||
backup_file = os.path.join(Config.backup_dir, '{}backup.sql'.format(time.strftime('%d%m%Y-%H%M%S')))
|
||||
|
||||
def backup(dbname):
|
||||
try:
|
||||
conn = pymysql.connect(host=Config.db_host, user=Config.db_user, password=Config.db_password,
|
||||
database=dbname)
|
||||
except pymysql.Error as err:
|
||||
print(f"Error connecting to database: {err}")
|
||||
exit()
|
||||
|
||||
# create backup command
|
||||
backup_cmd = f"mysqldump -u {Config.db_user} -p'{Config.db_password}' {dbname} > {backup_file}"
|
||||
print(backup_cmd)
|
||||
|
||||
# execute backup command
|
||||
subprocess.call(backup_cmd, shell=True)
|
||||
|
||||
# close database connection
|
||||
conn.close()
|
||||
28
backup_db/jobs/delete_files.py
Normal file
28
backup_db/jobs/delete_files.py
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright Hersel Giannella
|
||||
|
||||
import os
|
||||
from config import Config
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
def delete_sql_files():
|
||||
try:
|
||||
# Verifica se la cartella esiste
|
||||
if not os.path.exists(Config.backup_dir):
|
||||
print(f"La cartella {Config.backup_dir} non esiste.")
|
||||
return
|
||||
|
||||
# Ottiene la data di 7 giorni fa
|
||||
seven_days_ago = datetime.now() - timedelta(days=Config.retention_days)
|
||||
|
||||
# Itera su tutti i file nella cartella
|
||||
for filename in os.listdir(Config.backup_dir):
|
||||
file_path = os.path.join(Config.backup_dir, filename)
|
||||
# Verifica se il file ha estensione .sql e se è stato creato 7 giorni fa, quindi lo elimina
|
||||
if filename.endswith(".sql") and datetime.fromtimestamp(os.path.getctime(file_path)).date() == seven_days_ago.date():
|
||||
os.remove(file_path)
|
||||
print(f"File eliminato: {file_path}")
|
||||
except Exception as e:
|
||||
print(f"Si è verificato un errore durante l'eliminazione dei file: {e}")
|
||||
Reference in New Issue
Block a user