diff --git a/backup_db/__pycache__/config.cpython-39.pyc b/backup_db/__pycache__/config.cpython-39.pyc new file mode 100644 index 0000000..8b6873e Binary files /dev/null and b/backup_db/__pycache__/config.cpython-39.pyc differ diff --git a/backup_db/config.py b/backup_db/config.py new file mode 100644 index 0000000..478ccec --- /dev/null +++ b/backup_db/config.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Copyright Hersel Giannella + +class Config(object): + db_host = 'localhost' + db_user = 'root' + db_password = '' + backup_dir = 'backupdb' + retention_days = 7 \ No newline at end of file diff --git a/backup_db/jobs/__pycache__/backup_db.cpython-39.pyc b/backup_db/jobs/__pycache__/backup_db.cpython-39.pyc new file mode 100644 index 0000000..b7a2185 Binary files /dev/null and b/backup_db/jobs/__pycache__/backup_db.cpython-39.pyc differ diff --git a/backup_db/jobs/__pycache__/delete_files.cpython-39.pyc b/backup_db/jobs/__pycache__/delete_files.cpython-39.pyc new file mode 100644 index 0000000..0ddc39b Binary files /dev/null and b/backup_db/jobs/__pycache__/delete_files.cpython-39.pyc differ diff --git a/backup_db/jobs/backup_db.py b/backup_db/jobs/backup_db.py new file mode 100644 index 0000000..8e22495 --- /dev/null +++ b/backup_db/jobs/backup_db.py @@ -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() \ No newline at end of file diff --git a/backup_db/jobs/delete_files.py b/backup_db/jobs/delete_files.py new file mode 100644 index 0000000..cbcd46b --- /dev/null +++ b/backup_db/jobs/delete_files.py @@ -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}") diff --git a/backup_db/main.py b/backup_db/main.py new file mode 100644 index 0000000..232538a --- /dev/null +++ b/backup_db/main.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Copyright Hersel Giannella + +import time +import schedule +from jobs.delete_files import delete_sql_files +from jobs.backup_db import backup + +def get_time() -> str: + return time.strftime('%X (%d/%m/%y)') + +# Schedulazione del backup di tutti i database ogni giorno alle 4:00 AM +schedule.every().day.at("04:00").do(backup, dbname='dbname') +schedule.every().day.at("05:00").do(delete_sql_files) + +# Loop infinito per eseguire lo scheduler +while True: + schedule.run_pending() + print("eseguo job",get_time()) + time.sleep(1) \ No newline at end of file