0% found this document useful (0 votes)
12 views3 pages

BKP Project

The document outlines a Python script for creating backups of a specified file, including error handling and email notifications. It defines functions to copy the file to a backup location, generate a unique backup file name, and send an email confirmation upon success or failure. The script utilizes libraries such as shutil, os, datetime, random, and win32com for its operations.

Uploaded by

mamacolindinho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

BKP Project

The document outlines a Python script for creating backups of a specified file, including error handling and email notifications. It defines functions to copy the file to a backup location, generate a unique backup file name, and send an email confirmation upon success or failure. The script utilizes libraries such as shutil, os, datetime, random, and win32com for its operations.

Uploaded by

mamacolindinho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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()

You might also like