Skip to content

Instantly share code, notes, and snippets.

@ultimatecoder
Created March 30, 2017 13:03
Show Gist options
  • Save ultimatecoder/6318012f85a552eeff0c4a2e8b42a4ca to your computer and use it in GitHub Desktop.
Save ultimatecoder/6318012f85a552eeff0c4a2e8b42a4ca to your computer and use it in GitHub Desktop.
A automated backup script written just to learn Python better. This script is not developed for solving any type of production purpose. I developed this while I was learning Python for the first time.
#!/usr/bin/python3
'''Title : Automated Backup script.
Auther : Jaysinh Shukla
Date : 21-01-2014
Version : 0.4
OS : POSIX
Description : The script will make backup of all the files in zip formate. User is required to add the list of file names with their path in SourcePath List. User can also give additional paths of the files as command line arguments. It is to be noted that the argumetns provided will not create any entry in the parmenet list of the script. The program will create dicrectory named with current date and backup file named current time. If backup of day is created first time then the program will create new dirctory.
References :
Python Modules
time.strftime(str format) : http://docs.python.org/2/library/time.html#time.strftime
os.path.exists(str path) : http://docs.python.org/2/library/os.path.html#os.path.exists
os.name : http://docs.python.org/2/library/os.html#os.name
zipfile.Zipfile(str file, str[1] mod) : http://docs.python.org/2/library/zipfile#zipfile.ZipFile
zipfile.write(str filename) : http://docs.python.org/2/library/zipfile#zipfile.ZipFile.write
zipfile.close() : http://docs.python.org/2/library/zipfile#zipfile.ZipFile.close
sys.arv[:] : http://docs.python.org/2/library/sys.html#sys.argv
'''
import time
import os
import zipfile
import sys
def CreateBackupInZip(SourcePathsAsList,DestinationPathAsString):
'''The function will accept the paths of all the files as List and the single string as destination path. The function will create the backfile in '.zip' formate with the use of Zip pakage of linux operating system. Name of backup file is current date and time always.'''
# Getting the Current Date and Time
currentDate = time.strftime("%d_%m_%y")
currentTime = time.strftime("%H_%M_%S")
# Appending Current Date to Destination path
DestinationPathAsString += currentDate
# Checking for the Directory
if not os.path.exists(DestinationPathAsString):
os.mkdir(DestinationPathAsString)
DestinationPathAsString += "{0}{1}{2}".format('/',currentTime,'.zip')
# Creating Zip.
BackupFile = zipfile.ZipFile(DestinationPathAsString,"w")
for path in SourcePathsAsList:
BackupFile.write(path)
BackupFile.close()
if __name__ == '__main__':
SourcePath = ['/home/bigj/Documents/Programes/Python/dictionary.py','/home/bigj/Documents/Programes/Python/testing.py']
DestinationPath = '/home/bigj/Documents/'
if os.name != 'posix':
print("Sorry. This program is only written for POSIX type operating system.")
else:
for arg in sys.argv[1:]:
SourcePath.append(arg)
CreateBackupInZip(SourcePath,DestinationPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment