From 4906291dc212fd306725b97745fc88001e709fec Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Thu, 22 May 2025 20:03:49 +0530 Subject: [PATCH] mailling script and database backup script --- backup/database_backup-script.py | 83 +++++++++++++++++++ .../localhost/localhost_project_report.py | 48 +++++++++++ mailling/prod/prod_project_report.py | 48 +++++++++++ mailling/stage/stage_project_report.py | 48 +++++++++++ 4 files changed, 227 insertions(+) create mode 100644 backup/database_backup-script.py create mode 100644 mailling/localhost/localhost_project_report.py create mode 100644 mailling/prod/prod_project_report.py create mode 100644 mailling/stage/stage_project_report.py diff --git a/backup/database_backup-script.py b/backup/database_backup-script.py new file mode 100644 index 0000000..daa057f --- /dev/null +++ b/backup/database_backup-script.py @@ -0,0 +1,83 @@ +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) \ No newline at end of file diff --git a/mailling/localhost/localhost_project_report.py b/mailling/localhost/localhost_project_report.py new file mode 100644 index 0000000..e500fb4 --- /dev/null +++ b/mailling/localhost/localhost_project_report.py @@ -0,0 +1,48 @@ +import sys +import requests +from datetime import date, timedelta + + +if len(sys.argv) > 1: + date = sys.argv[1] +else: + date = date.today() - timedelta(days=1) + +project_id = "2618eb89-2823-11f0-9d9e-bc241163f504" +base_url = "http://localhost:5032/api" + +def login(): + payload = { + "username": "admin@marcoaiot.com", + "password": "User@123" + } + + headers = { + "Content-Type": "application/json" + } + + response = requests.post(f"{base_url}/auth/login",json=payload,headers=headers) + + data = response.json()['data'] + jwt = data["token"] + return jwt + +def project_proccess(jwt,project_id,date): + headers = { + "Authorization": f"Bearer {jwt}", + "Content-Type": "application/json" + } + print(date) + response = requests.get(f"{base_url}/report/project-statistics/{project_id}?date={date}", headers=headers) + return response.status_code + +try: + jwt = login() + code = project_proccess(jwt,project_id,date) + if code == 200: + print("Email sent") + else: + print(f"{code}") +except Exception as e: + print(f"An error occurred: {e}") + diff --git a/mailling/prod/prod_project_report.py b/mailling/prod/prod_project_report.py new file mode 100644 index 0000000..043c389 --- /dev/null +++ b/mailling/prod/prod_project_report.py @@ -0,0 +1,48 @@ +import sys +import requests +from datetime import date, timedelta + + +if len(sys.argv) > 1: + date = sys.argv[1] +else: + date = date.today() - timedelta(days=1) + +project_id = "2618eb89-2823-11f0-9d9e-bc241163f504" +base_url = "https://api.marcoaiot.com/api" + +def login(): + payload = { + "username": "admin@marcoaiot.com", + "password": "User@123" + } + + headers = { + "Content-Type": "application/json" + } + + response = requests.post(f"{base_url}/auth/login",json=payload,headers=headers) + + data = response.json()['data'] + jwt = data["token"] + return jwt + +def project_proccess(jwt,project_id,date): + headers = { + "Authorization": f"Bearer {jwt}", + "Content-Type": "application/json" + } + print(date) + response = requests.get(f"{base_url}/report/project-statistics/{project_id}?date={date}", headers=headers) + return response.status_code + +try: + jwt = login() + code = project_proccess(jwt,project_id,date) + if code == 200: + print("Email sent") + else: + print(f"{code}") +except Exception as e: + print(f"An error occurred: {e}") + diff --git a/mailling/stage/stage_project_report.py b/mailling/stage/stage_project_report.py new file mode 100644 index 0000000..e5f98fc --- /dev/null +++ b/mailling/stage/stage_project_report.py @@ -0,0 +1,48 @@ +import sys +import requests +from datetime import date, timedelta + + +if len(sys.argv) > 1: + date = sys.argv[1] +else: + date = date.today() - timedelta(days=1) + +project_id = "2618eb89-2823-11f0-9d9e-bc241163f504" +base_url = "https://stageapi.marcoaiot.com/api" + +def login(): + payload = { + "username": "admin@marcoaiot.com", + "password": "User@123" + } + + headers = { + "Content-Type": "application/json" + } + + response = requests.post(f"{base_url}/auth/login",json=payload,headers=headers) + + data = response.json()['data'] + jwt = data["token"] + return jwt + +def project_proccess(jwt,project_id,date): + headers = { + "Authorization": f"Bearer {jwt}", + "Content-Type": "application/json" + } + print(date) + response = requests.get(f"{base_url}/report/project-statistics/{project_id}?date={date}", headers=headers) + return response.status_code + +try: + jwt = login() + code = project_proccess(jwt,project_id,date) + if code == 200: + print("Email sent") + else: + print(f"{code}") +except Exception as e: + print(f"An error occurred: {e}") +