From f03fe21cde88eaec529dfbc9fbdb252ab0453b5c Mon Sep 17 00:00:00 2001 From: wvzack <124844773+wvzack@users.noreply.github.com> Date: Sun, 4 May 2025 12:37:33 -0700 Subject: [PATCH 1/4] Create BackUp --- BackUp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 BackUp diff --git a/BackUp b/BackUp new file mode 100644 index 0000000..6d22ec7 --- /dev/null +++ b/BackUp @@ -0,0 +1,69 @@ +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.application import MIMEApplication +from email.mime.text import MIMEText +from email.encoders import encode_base64 +import os.path +import py7zr +import os +os.chdir(os.path.dirname(os.path.abspath(__file__))) + + +# Create a 7z archive named 'BlockBit.7z' and add the contents of 'source_dir' +with py7zr.SevenZipFile('BlockBit.7z', 'w') as archive: + archive.writeall('replace this with the file that everything is enclosed in', arcname='optional_name_in_archive') # change this line to your needs + + + +# Replace with the actual path to your .7z file +file_path = '/home/pi/Python_Projects/ByteBank/backups/ByteBank.7z' +def delete7z(): + try: + os.remove(file_path) + print(f"File '{file_path}' deleted successfully.") + except FileNotFoundError: + print(f"Error: File '{file_path}' not found.") + except PermissionError: + print(f"Error: No permission to delete file '{file_path}'.") + except Exception as e: + print(f"Error deleting file: {e}") + + +# Email settings +sender_email = 'example@gmail.com' #replace with your email +sender_password = '' # Use an app password, not your Gmail password +receiver_email = 'example@gmail.com' #replace with your email +subject = 'Email with 7z attachment' +body = 'Please find the attached 7z file.' +file_path = '' # Replace with where you want your backup to go while it is sending + + +msg = MIMEMultipart() +msg['From'] = sender_email +msg['To'] = receiver_email +msg['Subject'] = subject + +# Add the email body +msg.attach(MIMEText(body, 'plain')) + +# Attach the 7z file +with open(file_path, 'rb') as file: + part = MIMEApplication(file.read(), Name=os.path.basename(file_path)) + part.add_header('Content-Disposition', f'attachment; filename="{os.path.basename(file_path)}"') + msg.attach(part) + +# Encode the attachment +#encoders.encode_base64(part) + +# Send the email +try: + with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server: + server.login(sender_email, sender_password) + server.sendmail(sender_email, receiver_email, msg.as_string()) + print('Email sent successfully!') +except Exception as e: + print(f'Error sending email: {e}') + + +SendTo7z.delete7z() + From 14b8336349d9da05914c764c6c43e226fca0bfff Mon Sep 17 00:00:00 2001 From: wvzack <124844773+wvzack@users.noreply.github.com> Date: Sun, 4 May 2025 12:38:08 -0700 Subject: [PATCH 2/4] Update BackUp From 3897ba56751b58c04bf691b41ab547b4511b6f00 Mon Sep 17 00:00:00 2001 From: wvzack <124844773+wvzack@users.noreply.github.com> Date: Sun, 4 May 2025 12:43:03 -0700 Subject: [PATCH 3/4] Update BackUp --- BackUp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/BackUp b/BackUp index 6d22ec7..3d34652 100644 --- a/BackUp +++ b/BackUp @@ -61,9 +61,12 @@ try: server.login(sender_email, sender_password) server.sendmail(sender_email, receiver_email, msg.as_string()) print('Email sent successfully!') + delete7z() except Exception as e: print(f'Error sending email: {e}') + delete7z() + -SendTo7z.delete7z() + From 5da526e718e7851eeff6fb86509aca0801b2ffd4 Mon Sep 17 00:00:00 2001 From: wvzack <124844773+wvzack@users.noreply.github.com> Date: Sun, 4 May 2025 13:00:48 -0700 Subject: [PATCH 4/4] Update BackUp --- BackUp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BackUp b/BackUp index 3d34652..0a4d14c 100644 --- a/BackUp +++ b/BackUp @@ -16,7 +16,7 @@ with py7zr.SevenZipFile('BlockBit.7z', 'w') as archive: # Replace with the actual path to your .7z file -file_path = '/home/pi/Python_Projects/ByteBank/backups/ByteBank.7z' +file_path = '' # replace with where your .7z file will be after creating it def delete7z(): try: os.remove(file_path) @@ -35,7 +35,7 @@ sender_password = '' # Use an app password, not your Gmail password receiver_email = 'example@gmail.com' #replace with your email subject = 'Email with 7z attachment' body = 'Please find the attached 7z file.' -file_path = '' # Replace with where you want your backup to go while it is sending +file_path = '' # Replace with where you want the .7z file to go while it is sending msg = MIMEMultipart()