BKP PROJECT
import shutil
import os
import datetime
import random
import win32com.client as win32
#importing the libs
def backup(file, folder): #creating the backup method
 try: #exception tratament if something unexpected happen
   if not os.path.exists(file): #Verifying the file existence
     print(f"Arquivo {file} não encontrado")
     return None
   #creating folder if doesn't exist
   os.makedirs(folder, exist_ok=True)
   final_path = os.path.join(folder, generate_src_code()) #generating the new file
name
   shutil.copy2(file, final_path)
   print(f"Backup concluido: {final_path}")
   return final_path
 except Exception as ex: #error catch
   print(f"Ocorreu o seguinte erro: {ex}") #exception message
def generate_src_code(): #method to generate de code name to the backup file
 timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
 random_number = random.randint(1000, 9999)
 new_file_name = f"BKP{timestamp}_{random_number}.xlsm"
 return new_file_name
def send_email(): # send the backup confirmation to my email
 outlook = win32.Dispatch('outlook.application') #outlook instance
 mail = outlook.CreateItem(0) #creating the email object
 mail.To = “email” #my email
 if saved_file != None:
   mail.Subject = 'Backup concluido com sucesso' #email subject
    mail.Body = f'Backup concluido com sucesso! date:
{datetime.datetime.now()}' #email body in success case
   mail.Attachments.Add(saved_file) #attaching the backup file
   print("Email enviado com sucesso") #success message
 else:
   mail.Subject = 'Erro ao realizar o backup'
   mail.Body = f'Erro ao realizar o backup! date: {datetime.datetime.now()}'
#email body in error case
   print("Email enviado com sucesso") #error message
 #send mail method source code
 mail.Send() #sending the email
 return
org_file = "CaminhoArquivoOriginal"
bkp_path = "Caminho onde salvar"
saved_file = backup(org_file, bkp_path)
send_email()