Add files via upload
This commit is contained in:
BIN
backup_db/__pycache__/config.cpython-39.pyc
Normal file
BIN
backup_db/__pycache__/config.cpython-39.pyc
Normal file
Binary file not shown.
11
backup_db/config.py
Normal file
11
backup_db/config.py
Normal file
@@ -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
|
||||||
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}")
|
||||||
22
backup_db/main.py
Normal file
22
backup_db/main.py
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user