Advertisement
DrAungWinHtut

db_backup.py

Dec 23rd, 2023
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. import subprocess
  2. import time
  3.  
  4. def backup_database():
  5.     # Replace with your MySQL connection details
  6.     mysql_username = 'root'
  7.     mysql_password = ''
  8.     database_name = 'schooldb'
  9.    
  10.     # Replace with the desired file path for the backup
  11.     backup_file_path = "e:\\cs2024\\schooldb.sql"
  12.  
  13.     # Build the mysqldump command
  14.     mysqldump_cmd = f"mysqldump -u {mysql_username} -p{mysql_password} {database_name} > {backup_file_path}"
  15.  
  16.     # Execute the mysqldump command
  17.     subprocess.run(mysqldump_cmd, shell=True)
  18.    
  19.  
  20. def restore_database():
  21.     # Replace with your MySQL connection details
  22.     mysql_username = 'root'
  23.     mysql_password = ''
  24.     database_name = 'test'
  25.    
  26.     # Replace with the file path of the backup to restore
  27.     backup_file_path = "e:\\cs2024\\schooldb.sql"
  28.  
  29.     # Build the mysql command for restoring
  30.     mysql_cmd = f"mysql -u {mysql_username} -p{mysql_password} {database_name} < {backup_file_path}"
  31.  
  32.     # Execute the mysql command for restoring
  33.     subprocess.run(mysql_cmd, shell=True)
  34.  
  35. # Run the backup and restore process every 20 minutes
  36.  
  37. try:
  38.     restore_database()
  39. except Exception as e:
  40.     print(f"An error occurred: {e}")
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement