61 lines
2.5 KiB
Python
61 lines
2.5 KiB
Python
|
#!/usr/bin/env python
|
|||
|
# -*- coding: utf-8 -*-
|
|||
|
|
|||
|
import os
|
|||
|
import hashlib
|
|||
|
import datetime
|
|||
|
import subprocess
|
|||
|
import pysftp
|
|||
|
import telebot
|
|||
|
from dotenv import load_dotenv
|
|||
|
|
|||
|
# Load environment variables from .env file
|
|||
|
load_dotenv()
|
|||
|
|
|||
|
# Set up variables from environment variables
|
|||
|
MARIADB_HOST = os.getenv("MARIADB_HOST", default="localhost")
|
|||
|
MARIADB_USER = os.getenv("MARIADB_USER", default="root")
|
|||
|
MARIADB_PASSWORD = os.getenv("MARIADB_PASSWORD", default="passwd")
|
|||
|
MARIADB_DATABASE = os.getenv("MARIADB_DATABASE", default="DBName")
|
|||
|
SQLITE_DATABASE = os.getenv("SQLITE_DATABASE", default="/Path/to/database")
|
|||
|
SFTP_HOST = os.getenv("SFTP_HOST", default="localhost")
|
|||
|
SFTP_USER = os.getenv("SFTP_USER", default="user")
|
|||
|
SFTP_PASSWORD = os.getenv("SFTP_PASSWORD", default="passwd")
|
|||
|
SFTP_PATH = os.getenv("SFTP_PATH", default="/Paht/to/sftp")
|
|||
|
SFTP_PORT = os.getenv("SFTP_PORT", default="22")
|
|||
|
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN", default="TOKEN")
|
|||
|
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID", default="ID_CHAT")
|
|||
|
|
|||
|
# Generate backups
|
|||
|
start_time = datetime.datetime.now()
|
|||
|
mariadb_backup_filename = f"mariadb_backup_{datetime.datetime.now().strftime('%Y%m%d')}.sql.gz"
|
|||
|
subprocess.run(f"mariadb-dump -h {MARIADB_HOST} -u {MARIADB_USER} -p{MARIADB_PASSWORD} {MARIADB_DATABASE} | gzip > {mariadb_backup_filename}", shell=True, check=True)
|
|||
|
sqlite_backup_filename = f"sqlite_backup_{datetime.datetime.now().strftime('%Y%m%d')}.sql.gz"
|
|||
|
subprocess.run(f"sqlite3 {SQLITE_DATABASE} .dump | gzip > {sqlite_backup_filename}", shell=True, check=True)
|
|||
|
|
|||
|
# Calculate SHA512 hash of backups
|
|||
|
mariadb_hash = hashlib.sha512(open(mariadb_backup_filename, "rb").read()).hexdigest()
|
|||
|
sqlite_hash = hashlib.sha512(open(sqlite_backup_filename, "rb").read()).hexdigest()
|
|||
|
|
|||
|
# Transfer backups to SFTP server
|
|||
|
with pysftp.Connection(host=SFTP_HOST, username=SFTP_USER, password=SFTP_PASSWORD, port=SFTP_PORT) as sftp:
|
|||
|
sftp.put(mariadb_backup_filename, f"{SFTP_PATH}/{mariadb_backup_filename}")
|
|||
|
sftp.put(sqlite_backup_filename, f"{SFTP_PATH}/{sqlite_backup_filename}")
|
|||
|
|
|||
|
# Delete local backup files
|
|||
|
os.remove(mariadb_backup_filename)
|
|||
|
os.remove(sqlite_backup_filename)
|
|||
|
|
|||
|
# Send message via Telegram
|
|||
|
bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN)
|
|||
|
message = f"""
|
|||
|
Respaldo completado!
|
|||
|
|
|||
|
* **MariaDB:** *{mariadb_backup_filename}* (hash: {mariadb_hash})
|
|||
|
* **SQLite:** ️ *{sqlite_backup_filename}* (hash: {sqlite_hash})
|
|||
|
|
|||
|
⏱️ Tiempo de transferencia: {datetime.datetime.now() - start_time}
|
|||
|
|
|||
|
Disponible en SFTP: {SFTP_PATH}
|
|||
|
"""
|
|||
|
bot.send_message(TELEGRAM_CHAT_ID, message)
|