Advertisement
Guest User

Untitled

a guest
Apr 16th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. def EXTERNAL_BACKUP():
  2.     # Loaction of External backup, to check if the language folder exists
  3.     EXT_BACKUP_LANG_DIR = 'D:/2019/' + BACKUP_LANGUAGE
  4.  
  5.     if os.path.exists(EXT_BACKUP_LANG_DIR):
  6.         # Check if project has a backup saved already
  7.         if os.path.exists(EXTERNAL_LOCATION):
  8.             print('\nFolder has already been backed up, previously. Proceeding to zip previous folder before copying new backup.')
  9.             # Check for any old zip files of the project
  10.             os.chdir(EXT_BACKUP_LANG_DIR)
  11.             for file in glob.glob(PROJECT_NAME + '.zip'):
  12.                 # All zips with the name of the current project backup will be deleted
  13.                 os.remove(EXT_BACKUP_LANG_DIR, file)
  14.                 if os.path.exists(EXT_BACKUP_LANG_DIR + file):
  15.                     os.rmdir(EXT_BACKUP_LANG_DIR + file)
  16.  
  17.             # Call function to zip previous backup
  18.             MAKE_ARCHIVE(PROJECT_NAME + '--OLD', EXTERNAL_LOCATION)
  19.             # Print to console when the zipping has finished
  20.             print(PROJECT_NAME +
  21.                   ' has been zipped. Proceeding to delete non-zipped folder.')
  22.             # Delete the non-zipped folder before copying new backup
  23.             for the_file in os.listdir(EXTERNAL_LOCATION):
  24.                 file_path = os.path.join(EXTERNAL_LOCATION, the_file)
  25.                 try:
  26.                     if os.path.isfile(file_path):
  27.                         os.unlink(file_path)
  28.                     elif os.path.isdir(file_path):
  29.                         shutil.rmtree(file_path)
  30.                 except Exception as e:
  31.                     print(e)
  32.  
  33.             print('Non-zipped folder has been deleted. Proceeding to backup.')
  34.             shutil.copytree(SOURCE_DIRECTORY, EXTERNAL_LOCATION)
  35.             print('Successfully backed up.\n')
  36.         else:
  37.             # Copy project folder as normal
  38.             print('\nBackup to external has begun.')
  39.             shutil.copytree(SOURCE_DIRECTORY, EXTERNAL_LOCATION)
  40.             print('Backup to the external has been completed.')
  41.     elif not os.path.exists(EXT_BACKUP_LANG_DIR):
  42.         # Backup language does not exist, so create the fodler before performing backup
  43.         os.mkdir(BACKUP_LANGUAGE)
  44.         print('\n' + BACKUP_LANGUAGE + ' not found. Created a new folder called '
  45.               + BACKUP_LANGUAGE + '. Proceeding to backup files to external HDD.')
  46.         shutil.copytree(SOURCE_DIRECTORY, EXTERNAL_LOCATION)
  47.         print('Backup to the external has been completed.')
  48.     return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement