Commits

FredGan committed e99b686

Проверка пустого каталога

Comments (0)

Files changed (2)

    return paths
 #Create tar archives
 def Archive(paths):
-    archivepach="/usr/NAS/backups/"
+    archivepath="/usr/NAS/backups/"
     date=datetime.now().strftime('%Y'"."'%m'"."'%d')
     #Create logg objects
-    logging.basicConfig(filename=archivepach+'logs'+'_'+date+'.log',format='%(asctime)s %(message)s',level=logging.DEBUG)
+    logging.basicConfig(filename=archivepath+'logs'+'_'+date+'.log',format='%(asctime)s %(message)s',level=logging.DEBUG)
     body=''
     for i in paths:
-        logging.info('Start task: '+str(i))
-        archname=archivepach+str(i)+'_'+date+".tar"
-        tar = tarfile.open(archname, "w")
-        for root, dir, files in os.walk(str(paths[i])):
-            for file in files:
-                fullpath = os.path.join(root,file) 
-                try:
-                      tar.add(fullpath)
-                except:
-                      logging.warning("Error: "+fullpath)
-        logging.info('Finish task: '+str(i))
-        tar.close()
-        # Email body include: name and size of file 'os.stat(archname).st_size'
-        size = os.stat(archname).st_size/1048576 
-        body=body+'Job '+str(i)+' is done. '+'Size:'+str(size)+'Mb'+'\n'
+          if modules.Folderempty(str(i))=='non_empty':
+             logging.info('Start task: '+str(i))
+             archname=archivepath+str(i)+'_'+date+".tar"
+             tar = tarfile.open(archname, "w")
+             for root, dir, files in os.walk(str(paths[i])):
+                 for file in files:
+                     fullpath = os.path.join(root,file) 
+                     try:
+                             tar.add(fullpath)
+                     except:
+                             logging.warning("Error: "+fullpath)
+             logging.info('Finish task: '+str(i))
+             tar.close()
+             # Email body include: name and size of file 'os.stat(archname).st_size'
+             size = os.stat(archname).st_size/1048576
+             body=body+'Job '+str(i)+' is done. '+'Size:'+str(size)+'Mb'+'\n'
+          else:
+             logging.warning("Error: "+str(i)+" mount error or folder is empty.") 
+             body=body+str(i)+" mount error or folder is empty.\n"
+            
 #Send email          
     modules.Sendemail(fro_m='service@redsails.ru', to='backup@redsails.ru', body=body , subject='BackUP')
 #Run processing
         while len(delfile)>3:
               os.remove('/usr/NAS/backups/'+delfile[min(delfile)])
               delfile.pop(min(delfile))
+
+#Check folders empty
+def Folderempty(folder):
+    state=('empty', 'non_empty')
+    for root,dir,files in os.walk(folder):
+        if list(dir)==0 and list(files)==0:
+             return state[0]
+        else:
+             return state[1]