Commits

FredGan committed c50c427

Rotation arch files

Comments (0)

Files changed (2)

     archivepach="/usr/NAS/backups/"
     date=datetime.now().strftime('%Y'"."'%m'"."'%d')
     logging.basicConfig(filename=archivepach+'logs'+'_'+date+'.log',format='%(asctime)s %(message)s',level=logging.DEBUG)
+    body=''
     for i in paths:
         logging.info('Start task: '+str(i))
         tar = tarfile.open(archivepach+str(i)+'_'+date+".tar", "w")
                       logging.warning("Error: "+fullpath)
         logging.info('Finish task: '+str(i))
         tar.close()
+        body=body+'Job '+str(i)+' is done.\n'
 #Send email          
-        modules.Sendemail(fro_m='service@redsails.ru', to='backup@redsails.ru', body='Job '+str(i)+' is done.' , subject='BackUP')
+    modules.Sendemail(fro_m='service@redsails.ru', to='backup@redsails.ru', body=body , subject='BackUP')
 #Run processing
 Archive(Path())
+modules.Rotation(Path())
 #Import extentions
 import smtplib
 from email.mime.text import MIMEText
+import os,re, datetime
+from datetime import date
 #Sending email
 def Sendemail(fro_m,to,subject,body):
     msg = MIMEText(body)
     send = smtplib.SMTP('192.168.50.254')
     send.sendmail(fro_m, to,  msg.as_string())
     send.quit()
+#Rotation files
+def Rotation(arch):
+#Create list name of arch
+    list=[]
+    for i in arch.keys():
+       list.append(str(i))
+#Create list of files at directory /usr/NAS/backups
+    file=[]
+    for root,dir,files in os.walk('/usr/NAS/backups'):
+        file=files
+#Create dictionary of arch wich param date:name and delete old  file if number arch more three
+    for i in list:   
+        delfile={}
+        for j in file:
+            if re.search(re.compile(i), j):
+               delfile[datetime.date.fromtimestamp(os.path.getmtime('/usr/NAS/backups/'+j))]=j   
+        while len(delfile)>3:
+              os.remove('/usr/NAS/backups/'+delfile[min(delfile)])
+              delfile.pop(min(delfile))