def create_copy_of_database(self):
conn = self.connect_to_database()
# Prompt the user to select a backup location
backup_folder = filedialog.askdirectory()
if backup_folder: # Ensure a folder was selected
# Generate backup file name with timestamp
backup_file_name = f"Database_Backup_{datetime.now().strftime('%Y-%m-%d_%H-%S')}.sql"
backup_location = os.path.join(backup_folder, backup_file_name)
with open(backup_location, "w") as backup_file:
# rest of the function remains the same
cursor.execute("SHOW TABLES")
tables = cursor.fetchall()
cursor.execute(f"SELECT * FROM {table_name}")
backup_file.write(f"\n-- Table structure for table {table_name}\n")
cursor.execute(f"SHOW CREATE TABLE {table_name}")
create_table = cursor.fetchone()[1]
backup_file.write(f"{create_table};\n")
backup_file.write(f"\n-- Data for table {table_name}\n")
if isinstance(value, str):
values.append(f"'{value}'")
elif isinstance(value, bytes):
values.append(f"x'{value.hex()}'")
elif isinstance(value, float):
values.append(repr(value))
values.append(str(value))
insert_query = f"INSERT INTO {table_name} VALUES ({', '.join(values)});\n"
backup_file.write(insert_query)
encryption_key = b'wWU9Yh3Q7JN5XXpbK43YuMYQtA9yUI3wdDzmADMUOzk=wq' # Encryption key
with open(backup_location, 'rb') as f:
fernet = Fernet(encryption_key)
encrypted_data = fernet.encrypt(data)
encrypted_backup_file_name = f"Encrypted_Database_Backup_{datetime.now().strftime('%Y-%m-%d_%H-%S')}.enc"
encrypted_backup_location = os.path.join(backup_folder, encrypted_backup_file_name)
with open(encrypted_backup_location, 'wb') as f:
os.unlink(backup_location) # Permanently delete the unencrypted .sql file
msg.showinfo("Success", "تمت العملية بنجاح.")
os.startfile(backup_folder)
msg.showerror("خطأ في إجراء العملية", f"{e}")
def create_copy_of_database(self):
try:
conn = self.connect_to_database()
cursor = conn.cursor()
# Prompt the user to select a backup location
backup_folder = filedialog.askdirectory()
if backup_folder: # Ensure a folder was selected
# Generate backup file name with timestamp
backup_file_name = f"Database_Backup_{datetime.now().strftime('%Y-%m-%d_%H-%S')}.sql"
backup_location = os.path.join(backup_folder, backup_file_name)
# Creating backup file
with open(backup_location, "w") as backup_file:
# rest of the function remains the same
cursor.execute("SHOW TABLES")
tables = cursor.fetchall()
for table in tables:
table_name = table[0]
cursor.execute(f"SELECT * FROM {table_name}")
rows = cursor.fetchall()
backup_file.write(f"\n-- Table structure for table {table_name}\n")
cursor.execute(f"SHOW CREATE TABLE {table_name}")
create_table = cursor.fetchone()[1]
backup_file.write(f"{create_table};\n")
backup_file.write(f"\n-- Data for table {table_name}\n")
for row in rows:
values = []
for value in row:
if isinstance(value, str):
values.append(f"'{value}'")
elif isinstance(value, bytes):
values.append(f"x'{value.hex()}'")
elif value is None:
values.append("NULL")
elif isinstance(value, float):
values.append(repr(value))
else:
values.append(str(value))
insert_query = f"INSERT INTO {table_name} VALUES ({', '.join(values)});\n"
backup_file.write(insert_query)
# Perform encryption
encryption_key = b'wWU9Yh3Q7JN5XXpbK43YuMYQtA9yUI3wdDzmADMUOzk=wq' # Encryption key
with open(backup_location, 'rb') as f:
data = f.read()
fernet = Fernet(encryption_key)
encrypted_data = fernet.encrypt(data)
encrypted_backup_file_name = f"Encrypted_Database_Backup_{datetime.now().strftime('%Y-%m-%d_%H-%S')}.enc"
encrypted_backup_location = os.path.join(backup_folder, encrypted_backup_file_name)
with open(encrypted_backup_location, 'wb') as f:
f.write(encrypted_data)
os.unlink(backup_location) # Permanently delete the unencrypted .sql file
cursor.close()
conn.close()
msg.showinfo("Success", "تمت العملية بنجاح.")
os.startfile(backup_folder)
else:
self.general_manager()
return
except Exception as e:
msg.showerror("خطأ في إجراء العملية", f"{e}")