Skip to content

Instantly share code, notes, and snippets.

@vahnilah vahnilah/BackupFiles.py
Last active Mar 16, 2019

Embed
What would you like to do?
Python script for Windows 10 to backup all files & replace with a newer copy, use git to make a history, and then shutdown computer
# Backup files through git and local
# Setup git to push to a remote or local network to secure backups outside of local machine if hardware failure occurs
# Setup backup folder to a seperate drive
# Original: Jan 5 2015
# Update1: Feb 13 2018
# Update2: Mar 16 2019
""" Example of txt file
---- Git Folder ----
D:\Projects\...\SomeProject
---- Copy From -----
C:\Projects\...\SomeProject
---- Copy To -------
D:\Projects\...\SomeProject
"""
import errno
import shutil
import os
import sys
import git
import datetime
ignore_pattern = ['.git','tmp']
def copytree(src, dst, symlinks=False, ignore=shutil.ignore_patterns(ignore_pattern)):
# print("Dst : " + str(dst))
if (ignore_pattern[0] == str(dst[-4:])):
pass
else:
if not os.path.exists(dst):
os.makedirs(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
copytree(s, d, symlinks, ignore)
else:
if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1:
shutil.copy2(s, d)
print("Copying: " + str(item))
def opentext(name, parameter):
content2 = []
file = open(name, parameter)
content = file.readlines()
file.close()
for file in content:
file = file.split("\n")
content2.append(file[0])
return content2
def gitcommands(file_location):
repo = git.Repo(file_location[1])
repo.git.add(".")
t = datetime.datetime.today()
commitMsg = str(datetime.date.today()) + " @ " + str(t.hour) + ":" + str(t.minute)
repo.git.commit('-m', commitMsg)
print("Successfully commited files to backup drive")
def backup_files_to_directory(file_location):
isSourceLocationExist = False
isBackupLcationExist = False
originalSource = file_location[3]
backupLocation1 = file_location[5]
if os.path.isdir(originalSource):
isSourceLocationExist = True
if os.path.isdir(backupLocation1):
isBackupLcationExist = True
if (isSourceLocationExist == True and isBackupLcationExist == True):
copytree(originalSource, backupLocation1)
print("\nSuccessfuly backed up to " + backupLocation1)
return True
else:
reason = ""
if (isSourceLocationExist == False):
reason += "\n Source folder does not exist"
if (isBackupLcationExist == False):
reason += "\n Backup folder does not exist"
print("\nNot able to backup files: " + reason)
return False
def shutdown():
t = datetime.datetime.today()
# Shutdown between the hours of 10pm to 12am
if (t.hour >= 22 and t.hour <= 24):
os.system('shutdown -s -t 10')
# Shutdown between the hours of 12am to 4am
if (t.hour >= 0 and t.hour < 4):
os.system('shutdown -s -t 10')
def main():
didFileBackupSuccessfuly = False
location = opentext("directoryToBackup.txt", "r")
print("Starting backup...")
didFileBackupSuccessfuly = backup_files_to_directory(location)
if (didFileBackupSuccessfuly == True):
try:
gitcommands(location)
except Exception as e:
print("Could not backup with git\n" + str(e))
shutdown()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.