83 lines
2.2 KiB
Python
83 lines
2.2 KiB
Python
import os
|
|
import datetime
|
|
import subprocess
|
|
import zipfile
|
|
|
|
# Configuration
|
|
DB_HOST = '147.93.98.152'
|
|
DB_USER = 'devuser'
|
|
DB_PASSWORD = 'AppUser@123$'
|
|
DB_NAME_PROD = 'MarcoBMSProd'
|
|
DB_NAME_STAGE = 'MarcoBMSStage'
|
|
DB_NAME_GITA = 'gitea'
|
|
DB_NAME_MEDIAWIKI = 'mediawiki'
|
|
DB_NAME_REDMINE = 'redmine'
|
|
BACKUP_DIR = "C:/gita/database/backup"
|
|
MYSQLDUMP_PATH = r'C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump.exe'
|
|
LOG_FILE = r'C:\gita\backup_log.txt'
|
|
print(BACKUP_DIR)
|
|
# Generate backup filename with timestamp
|
|
|
|
def build_path(database_name):
|
|
timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M')
|
|
backup_filename = f"{timestamp} - {database_name} database backup.sql"
|
|
backup_path = os.path.join(BACKUP_DIR, backup_filename)
|
|
return backup_path
|
|
|
|
# Perform backup using mysqldump
|
|
def build_command(name):
|
|
command = [
|
|
MYSQLDUMP_PATH,
|
|
f"-h{DB_HOST}",
|
|
f"-u{DB_USER}",
|
|
f"-p{DB_PASSWORD}",
|
|
]
|
|
|
|
command.append(name)
|
|
return command
|
|
|
|
def start_backup(database):
|
|
|
|
with open(build_path(database), "w") as out_file:
|
|
subprocess.run(build_command(database), stdout=out_file, check=True)
|
|
|
|
def upload_to_git():
|
|
try:
|
|
# Move to backup directory
|
|
os.chdir(BACKUP_DIR)
|
|
|
|
# Git commands
|
|
subprocess.run(["git", "add", "."], check=True)
|
|
commit_message = f"Backup commit on {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
|
subprocess.run(["git", "commit", "-m", commit_message], check=True)
|
|
subprocess.run(["git", "push"], check=True)
|
|
|
|
print("Backup files pushed to Git repository.")
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"Git upload failed: {e}")
|
|
|
|
# Run backup and log
|
|
try:
|
|
print("Backup started.")
|
|
|
|
# Run MySQL backup for production
|
|
start_backup(DB_NAME_PROD)
|
|
|
|
# Run MySQL backup for staging
|
|
start_backup(DB_NAME_STAGE)
|
|
|
|
# Run MySQL backup for Gita
|
|
start_backup(DB_NAME_GITA)
|
|
|
|
# Run MySQL backup for Redmine
|
|
start_backup(DB_NAME_REDMINE)
|
|
|
|
# Run MySQL backup for Wiki
|
|
start_backup(DB_NAME_MEDIAWIKI)
|
|
|
|
upload_to_git()
|
|
print("Backup process completed successfully.")
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"Backup failed: {e}")
|
|
|
|
exit(0) |