In this article, we are going to see how to automate backup with a Python script.
File backups are essential for preserving your data in local storage. We will use theshutil,os, andsysmodules. In this case, the shutil module is used to copy data from one file to another, while the os and sys modules are used to obtain the directory path and so on.
Stepwise Implementation:
Step 1: Import the following modules
Python3importshutilfromdatetimeimportdateimportosimportsys
Step 2:Let us now get today's date using the datetime module.
Python3today=date.today()date_format=today.strftime("%d_%b_%Y_")
Step 3:If the user specifies the path to the source file, use the line below to concatenate the path to the source file with the name of the source file.
Python3src_dir=src_dir+src_file_name
If not, and your file is stored in the same directory as your current Python script, use the os module to determine the current path of the file and create the source directory by combining the path provided by the os module with the source file name.
Python3
Step 4:If the user does not specify the source file name, we must return a file does not exist error.
Python3ifnotsrc_file_name:print("Please give atleast the Source File Name")
Step 5:Now, use the following cases to test the necessary conditions.
If the user provides all of the inputs, such as thesource file name,source file path,destination file name, anddestination file path.
Python3ifsrc_file_nameanddst_file_nameandsrc_diranddst_dir:src_dir=src_dir+src_file_namedst_dir=dst_dir+dst_file_name
If the destination file name is None, which indicates that the user did not specify a destination file name, then use the conditions listed below.
Python3elifdst_file_nameisNoneornotdst_file_name:dst_file_name=src_file_namedst_dir=dst_dir+date_format+dst_file_name
If a user enters an empty string with one or more spaces.
Python3elifdst_file_name.isspace():dst_file_name=src_file_namedst_dir=dst_dir+date_format+dst_file_name
If the user inputs the backup copy's name, just concatenate thedestination directory,date, anddestination file nameto create the output file name.
Python3else:dst_dir=dst_dir+date_format+dst_file_name
Step 6:Finally, we simply need to use theshutil function to copy the file to the destination.
Python3shutil.copy2(src_dir,dst_dir)
Note:If we want to back up theentire folder rather than individual files, we must use the code below.
shutil.copytree(src_file_name, dst_dir)
Below is the full implementation:
Python3# Import the following modulesimportshutilfromdatetimeimportdateimportosimportsys# When there is need, just change the directoryos.chdir(sys.path[0])# Function for performing the# backup of the files and foldersdeftake_backup(src_file_name,dst_file_name=None,src_dir='',dst_dir=''):try:# Extract the today's datetoday=date.today()date_format=today.strftime("%d_%b_%Y_")# Make the source directory,# where we wanna backup our filessrc_dir=src_dir+src_file_name# If user not enter any source file,# then just give the error message...ifnotsrc_file_name:print("Please give atleast the Source File Name")exit()try:# If user provides all the inputsifsrc_file_nameanddst_file_nameandsrc_diranddst_dir:src_dir=src_dir+src_file_namedst_dir=dst_dir+dst_file_name# When User Enter Either# 'None' or empty String ('')elifdst_file_nameisNoneornotdst_file_name:dst_file_name=src_file_namedst_dir=dst_dir+date_format+dst_file_name# When user Enter an empty# string with one or more spaces (' ')elifdst_file_name.isspace():dst_file_name=src_file_namedst_dir=dst_dir+date_format+dst_file_name# When user Enter an a# name for the backup copyelse:dst_dir=dst_dir+date_format+dst_file_name# Now, just copy the files# from source to destinationshutil.copy2(src_dir,dst_dir)print("Backup Successful!")exceptFileNotFoundError:print("File does not exists!,\ please give the complete path")# When we need to backup the folders only...exceptPermissionError:dst_dir=dst_dir+date_format+dst_file_name# Copy the whole folder# from source to destinationshutil.copytree(src_file_name,dst_dir)# Call the functiontake_backup("GFG.txt")
Output:

Folders BackUp:

Automate backup with Python Script