Last active
May 13, 2016 15:30
-
-
Save thurask/f54b06e594b320347add5fa59e67a780 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
import time | |
import win32api | |
import pythoncom | |
from win32com.shell import shell,shellcon | |
def win_copy_files(source,dest): | |
if not os.path.exists(dest): | |
os.makedirs(dest) | |
pfo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation,None,pythoncom.CLSCTX_ALL,shell.IID_IFileOperation) | |
pfo.SetOperationFlags(shellcon.FOF_NOCONFIRMATION) | |
dst = shell.SHCreateItemFromParsingName(dest,None,shell.IID_IShellItem) | |
for f in source: | |
src = shell.SHCreateItemFromParsingName(f,None,shell.IID_IShellItem) | |
print("Copying {0}...".format(os.path.basename(f))) | |
pfo.CopyItem(src,dst) | |
success = pfo.PerformOperations() | |
aborted = pfo.GetAnyOperationsAborted() | |
return success and not aborted | |
def get_drive(): | |
drives = win32api.GetLogicalDriveStrings() | |
dx = [x for x in drives.split("\000") if x] | |
for drive in dx: | |
try: | |
inf = win32api.GetVolumeInformation(drive) | |
except: | |
pass | |
else: | |
if inf[0] == "My Passport": | |
return drive | |
else: | |
print("Please plug in the backup drive!") | |
raise SystemExit | |
DRIVELETTER = get_drive() | |
def prepare_warning(): | |
notice = os.path.join(backup_folder(), "!!!NOTICE!!!.txt") | |
if not os.path.exists(notice): | |
try: | |
os.makedirs(os.path.dirname(notice)) | |
except FileExistsError: | |
pass | |
with open(notice, "w") as afile: | |
afile.write("Don't touch this folder, everything is automated.") | |
else: | |
pass | |
def todays_backup(): | |
return os.path.join("Backups", time.strftime("%Y%m%d")) | |
def backup_folder(): | |
return os.path.join(DRIVELETTER, os.sep, "Backups") | |
def copy_files(): | |
dest = os.path.join(DRIVELETTER, os.sep, todays_backup()) | |
src = [] | |
src.append(os.path.join("C:", os.sep, "ParadigmOffice")) | |
src.append(os.path.join("C:", os.sep, "CADIData")) | |
src.append(os.path.join("C:", os.sep, "CADI")) | |
src.append(os.path.join("C:", os.sep, "Users", "mothership")) | |
win_copy_files(src, dest) | |
def clean_backups(maximum=5): | |
bfo = backup_folder() | |
backups = [os.path.join(bfo, folder) for folder in os.listdir(bfo) if os.path.isdir(os.path.join(bfo, folder))] | |
if len(backups) <= maximum: | |
pass | |
else: | |
print("Cleaning oldest backups...") | |
for folder in backups[:-maximum]: | |
shutil.rmtree(folder, ignore_errors=True) | |
if __name__ == "__main__": | |
print("AUTOMATIC BACKUP PROGRAM") | |
print("Backup for {0}".format(time.strftime("%Y%m%d"))) | |
prepare_warning() | |
copy_files() | |
clean_backups() | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment