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 the shutil, os, and sys modules. 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
Python3
import shutil
from datetime import date
import os
import sys
|
Step 2: Let us now get today’s date using the datetime module.
Python3
today = 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.
Python3
src_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.
Step 4: If the user does not specify the source file name, we must return a file does not exist error.
Python3
if not src_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 the source file name, source file path, destination file name, and destination file path.
Python3
if src_file_name and dst_file_name and src_dir and dst_dir:
src_dir = src_dir + src_file_name
dst_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.
Python3
elif dst_file_name is None or not dst_file_name:
dst_file_name = src_file_name
dst_dir = dst_dir + date_format + dst_file_name
|
If a user enters an empty string with one or more spaces.
Python3
elif dst_file_name.isspace():
dst_file_name = src_file_name
dst_dir = dst_dir + date_format + dst_file_name
|
If the user inputs the backup copy’s name, just concatenate the destination directory, date, and destination file name to create the output file name.
Python3
else :
dst_dir = dst_dir + date_format + dst_file_name
|
Step 6: Finally, we simply need to use the shutil function to copy the file to the destination.
Python3
shutil.copy2(src_dir, dst_dir)
|
Note: If we want to back up the entire 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 shutil
from datetime import date
import os
import sys
os.chdir(sys.path[ 0 ])
def take_backup(src_file_name,
dst_file_name = None ,
src_dir = '',
dst_dir = ''):
try :
today = date.today()
date_format = today.strftime( "%d_%b_%Y_" )
src_dir = src_dir + src_file_name
if not src_file_name:
print ( "Please give atleast the Source File Name" )
exit()
try :
if src_file_name and dst_file_name and src_dir and dst_dir:
src_dir = src_dir + src_file_name
dst_dir = dst_dir + dst_file_name
elif dst_file_name is None or not dst_file_name:
dst_file_name = src_file_name
dst_dir = dst_dir + date_format + dst_file_name
elif dst_file_name.isspace():
dst_file_name = src_file_name
dst_dir = dst_dir + date_format + dst_file_name
else :
dst_dir = dst_dir + date_format + dst_file_name
shutil.copy2(src_dir, dst_dir)
print ( "Backup Successful!" )
except FileNotFoundError:
print (" File does not exists!,\
please give the complete path")
except PermissionError:
dst_dir = dst_dir + date_format + dst_file_name
shutil.copytree(src_file_name, dst_dir)
take_backup( "GFG.txt" )
|
Output:

Folders BackUp:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
31 Aug, 2021
Like Article
Save Article