Source

Simple_BackUP / modules.py

Full commit
#!/usr/bin/env python

#There is additional function
#Import extentions
import smtplib
from email.mime.text import MIMEText
import os,re, datetime
from datetime import date
import calendar
#Sending email
def Sendemail(fro_m,to,subject,body):
    msg = MIMEText(body)

    msg['Subject'] = subject
    msg['From'] = fro_m
    msg['To'] = to

    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))

#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]

#List of day archives.
def ListDayArch(year,month):
    day_arch=(0,2,4)	
    l_day=[]
    mday=calendar.monthrange(year, month)
    for i in range(1,mday[1]):
        if calendar.weekday(year,month,i) in day_arch:
           l_day.append(i)
    return l_day