<file_sep>#  python sync tool

import os as os
import datetime
import time
import shutil
import pdb 

class sync:

  def __init__(self, source, destination ):
    self.src = source
    self.dest = destination


  def checkRoot(self, src , dest):
    if not os.path.exists(dest):
        os.mkdir(dest)


  def syncDir(self, src, dest):
      srcList = os.listdir(src)
      srcTime = [None]*len(srcList)
      destList = os.listdir(src)
      destTime = [None]*len(srcList)
      
      for i in range(len(srcList)):
          srcList[i] = src + srcList[i]
          destList[i] = dest + destList[i]
          srcTime[i] = os.path.getmtime(srcList[i])
          print(srcList[i])
          
          # IF THE DESTINATION PATH EXISTS 
          if os.path.exists(destList[i]) == True:
              # pdb.set_trace()
              if os.path.isfile(srcList[i]) == False:
                os.chdir(srcList[i])

              else:
                pass

              destTime[i] = os.path.getmtime(destList[i])
              # IF THE DESTINATION IS OLDER
              if destTime[i] < srcTime[i]:
                  try: # IF DESTINATION IS A DIRECTORY USE COPYTREE
                    shutil.rmtree(destList[i])
                    shutil.copytree(srcList[i], destList[i])
              
                  except OSError: # IF NOT, USE COPY2
                    shutil.copy2(srcList[i], destList[i]) 
              else:
                  pass
            
          else:
              try:
                  shutil.copytree(srcList[i], destList[i])
              
              except OSError:
                  shutil.copy2(srcList[i], destList[i])             
     
      return srcList, srcTime, destList, destTime

          
  def main(self, src, dest):
    self.checkRoot(src, dest)
    srcList, srcTime, destList, destTime = self.syncDir(src,dest)
    now = time.mktime(datetime.datetime.now().timetuple())
    
    return srcList, srcTime, destList, destTime

#sy = sync('c:\users\Arl_guest\desktop','c:\users\Arl_guest\desktop\TEST')
# sy = sync('/home/michael/Dropbox/', '/home/michael/TEST/')
sy = sync('/users/michael/git/', '/users/michael/desktop/test/')
srcList, srcTime, destList, destTime = sy.main(sy.src, sy.dest)
print('Sync Finished')


<file_sep>
#program that backs up the specified directories to an external source
import shutil, errno, datetime, os, tarfile
def main():
        #declare sources 
        C = '/C/Shares/'

        #declare destination root
        Save_root = r'/External_HDD/Backups/Server/'

        #declare how long (in days) you want to keep daily backups
        dailyDuration = 7

        #declare what on which day you want to keep those past the above
        keepDay = 'Sat'     #Capitalize the first letter

        #find out what day it is
        today = str(datetime.date.today())

        #make a new folder to house all the backups
        save = Save_root+today+'_Backup'
        os.mkdir(save)

        Backup(C, save, 'Server')

        # create tarball of the newly created folder
        tar = tarfile.open(save+'.tar.bz2', 'w:bz2')
        tar.add(save)
        tar.close()

        # delete the non-compressed file 
        os.rmtree(save)

        ## remove any old backups
        clean_up(today, Save_root, dailyDuration, keepDay)

def Backup(source, destination_root, name):
        save =  destination_root +r'/'+ name
        shutil.copytree(source, save)

def clean_up(today, directory, dailyDuration, keepDay):
        os.chdir(directory)
        age = datetime.datetime.now() - datetime.timedelta(days = dailyDuration)

        for somefile in os.listdir('.'):
                 file_age = os.path.getmtime(somefile)
                 file_age = datetime.datetime.fromtimestamp(file_age)
                 file_day = file_age.strftime("%a")

                 #check if the file is older than specified date 
                 #check if the file was created on the keep day

                 if file_age < age and file_day != keepDay:
                    if os.path.isfile(somefile) == True:
                        os.remove(somefile)

                    else:
                        os.rmdir(somefile)

                 else:
                    pass

main()


