Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #AFS Backup
- #2013, #astropheed
- #Usage:
- # /s - Silent
- # /l - No Logging
- # /d - Detailed Logging (If logging is enabled)
- # /r - No Retries
- # /a - Archive prior to backup
- # /ao - Only run the Archive Operation and skip the Backup
- import datetime
- import os
- import sys
- import shutil
- import time
- class Backup:
- def __init__(self):
- #Get Location List
- if len(sys.argv) == 1:
- print("\nKCE AFS Backup Script")
- print("Copyright 2013, Ricky Hopfe\n")
- print("Usage:")
- print("Run from Command with appropriate switches")
- print("Argument 1: Source Location")
- print("Argument 2: Destination Location")
- print("Switches:")
- print("\t/a - Enable Archive before backup")
- print("\t/d - Enable Details in log (only if /l is not used)")
- print("\t/l - Disable Logging")
- print("\t/s - Enable Silent Operation")
- print("\t/r - Disable Retry on bad files")
- print("\t/ao - Only run the Archive Operation and skip the Backup")
- print("\n\nExample: 'python backup.py C:/ D:/ /s /d /a'")
- print("This would first archive contents of D:/ and then copy C:/")
- print("to the D:/ with no on-screen information. The log file")
- print("will contain a detailed report of everything that happened")
- sys.exit("\n\nSystem Exit: No Arguments")
- self.reporting = True
- self.detail_report = False
- self.silence = False
- self.retries = True
- self.cancel_backup = False
- self.files_updated = 0
- self.files_created = 0
- self.files_ignored = 0
- self.start_time = time.strftime("%H:%M:%S on %d/%m/%Y")
- self.cstart_time = time.time()
- self.sls = [[sys.argv[1], sys.argv[2]]]
- dow = datetime.date.today().weekday()
- #Run backup methods
- self.switches()
- if not self.cancel_backup:
- self.backup()
- def switches(self):
- if len(sys.argv) >= 3:
- if "/s" in sys.argv[3:]: self.silence = True
- if "/l" in sys.argv[3:]: self.reporting = False
- if "/d" in sys.argv[3:]: self.detail_report = True
- if "/r" in sys.argv[3:]: self.retries = False
- if "/ao" in sys.argv[3:]: self.cancel_backup = True
- if "/a" or "/ao" in sys.argv[3:]:
- self.archive_loc = sys.argv[2][:3]+"/ARCHIVE/"
- self.archive()
- def wlog(self, text, detail=False):
- if detail:
- if not self.detail_report:
- return 0
- if self.reporting:
- backup_location = "C:/backup_logs/"
- if not os.path.isdir(backup_location):
- os.makedirs(backup_location)
- log_date = time.strftime("%Y_%m_%d")
- log_time = time.strftime("%H:%M:%S")
- self.log = open("%slog_%s.txt"%(backup_location, log_date),"a")
- self.log.write("[%s]\t%s\n" % (log_time, text))
- self.log.close()
- else:
- pass
- def wprint(self, *arg, **kw):
- if not self.silence:
- print(*arg, **kw)
- def calculate_time(self):
- #Calculate a time between two times in certain formats
- total_time_temp = self.cend_time - self.cstart_time
- #86400 seconds in a Day
- if total_time_temp >= 86400:
- self.total_time = '{0:.3g}'.format(total_time_temp / 86400)
- self.total_time_format = "Days"
- #1440 Seconds in an hour
- elif total_time_temp >= 1440:
- self.total_time = '{0:.3g}'.format(total_time_temp / 1440)
- self.total_time_format = "Hours"
- #60 Seconds in a minute
- elif total_time_temp >= 60:
- self.total_time = '{0:.3g}'.format(total_time_temp / 60)
- self.total_time_format = "Minutes"
- #Must be less than a minute, Seconds it is
- else:
- self.total_time = '{0:.3g}'.format(total_time_temp)
- self.total_time_format = "Seconds"
- def archive(self):
- stamp = "Save_Set_%s" % time.strftime("%Y_%m_%d")
- save_set = "%s%s" % (self.archive_loc, stamp)
- if os.path.isdir(save_set):
- self.wlog("Error: Archive already run today, skipped")
- return 0
- self.wlog("Archive Operation Started")
- last_error = ""
- file_count = 0
- if not os.path.isdir(save_set):
- os.makedirs(save_set)
- for (path, _, __) in os.walk(self.archive_loc[:3]):
- if path[:len(self.archive_loc)-1] not in self.archive_loc:
- if path not in self.archive_loc[:3]+'$RECYCLE.BIN':
- self.wprint(path, self.archive_loc)
- for i in __: file_count += 1
- try:
- shutil.move(path, save_set)
- file_count += 1
- os.system('cls')
- self.wprint("Currently running Archive Operation:")
- self.wprint("Moved: %s things" % file_count)
- except Exception as e:
- last_error = "Last Error: %s" % e
- self.wlog(last_error)
- pass
- if not self.silence: os.system('cls')
- self.wprint("Archive Operation completed successfully.")
- self.wlog("Archive Operation completed")
- self.wprint("Moved: %s files" % file_count)
- self.wlog("Moved: %s files" % file_count, True)
- self.wprint(last_error)
- def compare(self, p_src, p_dest, file):
- f_src = p_src+file
- f_dest = p_dest+file
- try:
- f_src_time = os.path.getmtime(f_src)
- except:
- self.wlog("Source File Error: %s%s" % (p_src, file))
- return 0
- else:
- try:
- f_dest_time = os.path.getmtime(f_dest)
- except:
- #Destination doesn't exist
- self.files_created += 1
- return 1
- else:
- if f_src_time == f_dest_time:
- self.files_ignored += 1
- self.wlog("File Ignored: %s%s" % (p_src, file), True)
- return 0
- elif f_src_time > f_dest_time:
- self.files_updated += 1
- return 1
- else:
- #DEST is newer
- self.files_ignored += 1
- self.wlog("File Ignored: %s%s" % (p_src, file), True)
- return 0
- def retry_backup(self, n, first = False):
- bad_files = 0
- good_files = 0
- if not first:
- if not self.silence: os.system('cls')
- self.wprint("Currently running Backup Operation (Retry):")
- self.wprint("Waiting 60 Seconds until next Retry:")
- self.wprint("")
- self.wprint("")
- self.wprint("")
- self.wprint("Remaining issues: %s:" % self.file_errors)
- time.sleep(60)
- if n > 0:
- wlog("Attempting to Retry(%s) bad files" % n)
- for src, dest in self.retry:
- if not os.path.isfile(dest):
- if not self.silence: os.system('cls')
- self.wprint("Retrying Bad Files (%s tries remain):" % n)
- self.wprint("Processed: %s files" % file_count)
- self.wprint("Current file: %s%s" % (src))
- self.wprint("Destination: %s%s" % (dest))
- self.wprint("")
- self.wprint("Remaining issues: %s:" % self.file_errors)
- self.wprint("Successes: %s" % good_files)
- self.wprint("Failures: %s" % bad_files)
- try:
- shutil.copy(src, dest)
- self.wlog("%s *finally* copied to %s"%(src,dest), True)
- good_files += 1
- self.new_files += 1
- self.file_errors -= 1
- except:
- self.wlog("Unfortunately %s would not copy!" % src)
- bad_files += 1
- if bad_files > 0:
- self.retry_backup(n-1)
- def backup(self, limit=0):
- self.wlog("Backup Operation started at %s" % self.start_time)
- self.retry = []
- file_count = 0
- dobreak = False
- self.new_files = 0
- self.file_errors = 0
- for loc in self.sls:
- if dobreak: break
- loc[0] = loc[0] if loc[0][-1] is "/" else loc[0]+"/"
- for (path, dirs, files) in os.walk(loc[0]):
- if dobreak: break
- ext = path[len(loc[0]):] + "/"
- for dr in dirs:
- try:
- dest = loc[1] if loc[1][-1] is "/" else loc[1]+"/"
- dxt = ext if len(ext) > 1 else ""
- if not os.path.isdir(dest+dxt+dr):
- os.makedirs(dest+dxt+dr)
- self.wlog("Directory created: %s"%dest+dxt+dr,True)
- except:
- self.wlog("Failed to create directory: %s" % dr)
- for file in files:
- if file_count > limit and limit > 0: dobreak = True
- if dobreak: break
- file_count += 1
- p_src = path if path[-1] is "/" else path+"/"
- p_dest = loc[1] if loc[1][-1] is "/" else loc[1]+"/"
- if not self.silence: os.system('cls')
- self.wprint("Currently running Backup Operation:")
- self.wprint("Processed: %s files" % file_count)
- self.wprint("Current file: %s%s" % (p_src,file))
- self.wprint("Destination: %s%s" % (p_dest+ext,file))
- if self.compare(p_src, p_dest+ext, file):
- try:
- if not os.path.isdir(p_dest+ext):
- os.makedirs(p_dest+ext)
- self.wlog("Directory created: %s"%p_dest+ext,
- True)
- except:
- pass
- try:
- shutil.copy(p_src+file, p_dest+ext+file)
- self.wlog("%s copied to %s" % (p_src+file,
- p_dest+ext+file),
- True)
- self.new_files += 1
- except:
- self.file_errors += 1
- self.retry.append([p_src+file, p_dest+ext+file])
- self.wlog("File Error: %s" % p_src+file)
- self.wlog("Destination Error: %s"%p_dest+ext+file)
- if self.retries:
- if self.file_errors > 0:
- self.retry_backup(3, True)
- if not self.silence: os.system('cls')
- end_time = time.strftime("%H:%M:%S on %d/%m/%Y")
- self.cend_time = time.time()
- self.calculate_time()
- self.wprint("Backup Operation started at %s" % self.start_time)
- self.wprint("Backup Operation completed successfully at %s" % end_time)
- self.wprint("Time required: %s %s" % (self.total_time,
- self.total_time_format))
- self.wprint("")
- self.wprint("Processed: %s files" % file_count)
- self.wprint("")
- self.wprint("Files created: %s" % self.files_created)
- self.wprint("Files updated: %s" % self.files_updated)
- self.wprint("Files ignored: %s" % self.files_ignored)
- self.wprint("Files with errors: %s" % self.file_errors)
- self.wlog("Backup Operation completed successfully at %s" % end_time)
- self.wlog("Time required: %s %s" % (self.total_time,
- self.total_time_format))
- self.wlog("Processed: %s files" % file_count)
- self.wlog("Files created: %s" % self.files_created)
- self.wlog("Files updated: %s" % self.files_updated)
- self.wlog("Files ignored: %s" % self.files_ignored)
- self.wlog("Files with errors: %s" % self.file_errors)
- Backup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement