Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. """ Backup files.
  2. """
  3.  
  4. import os.path
  5. import shutil
  6. import datetime
  7.  
  8. ROOT_DIR = os.path.abspath(os.path.join(os.sep, 'backup-from-folder'))
  9. ERROR_LOG_FILE = os.path.join(os.sep, ROOT_DIR, 'logs', 'errorlog.txt')
  10.  
  11. CURRENT_DATE = str(datetime.datetime.now().date())
  12.  
  13. FILES_TO_BACKUP = [
  14. 'file-1.txt',
  15. 'file-2.txt'
  16. ]
  17.  
  18. def backup_data_entry_files():
  19. """ Backup files.
  20. """
  21.  
  22.  
  23. for filename in FILES_TO_BACKUP:
  24.  
  25. if os.path.isfile(filename):
  26. # 1. Extract Directory name.
  27. dirname = os.path.dirname(filename)
  28.  
  29. # 2. Extract filename and extension.
  30. fn, fe = os.path.splitext(filename)
  31.  
  32. # 3. Append date to new filename.
  33. new_filename = os.path.basename(fn + '-' + CURRENT_DATE + fe)
  34.  
  35. # 4. Construct the new destination.
  36. destination = os.path.join(dirname, 'backup-folder-name', new_filename)
  37.  
  38. # 5. Attempt to copy.
  39. if not os.path.exists(destination):
  40. try:
  41. shutil.copy(os.path.normpath(filename), destination)
  42. except IOError, e:
  43. with open(ERROR_LOG_FILE, 'a') as f:
  44. f.write('%s \nUnable to copy file. %s' % (CURRENT_DATE, e))
  45.  
  46. else:
  47. with open(ERROR_LOG_FILE, 'a') as f:
  48. f.write('%s\nFile "%s" does not exist\n\n' % (CURRENT_DATE, filename))
  49.  
  50. if __name__ == '__main__':
  51. backup_data_entry_files()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement