import os,shutil, subprocess
from datetime import datetime
file_destination = ["/home/borko/base/firebird.fdb"]
#Must change path to folder if user not called "borko"
copy_destination = "/home/borko/base/archive/"
#Full path to gbak
gback_file = "/opt/firebird/bin/gbak"
#Get time and date to add into archive
date = str(datetime.now())
datetime = date.replace(' ','')
count=0
#Input how much file save in folder!!!!
save_file = 2
#Set params for Gbak
server='127.0.0.1'
user = "SYSDBA"
password = "masterkey"
#List with older file which must delete
list_file_delete = []
#Function check for problems
def check_file_and_filder(folder,name):
if not os.path.isdir(folder):
try:
os.makedirs(folder)
except:
pass
for file_search in name:
if not os.path.isfile(file_search):
print "File not exists"
#Function calculate file in folder
def calculate():
count = 0
del_count=1
for i in file_destination:
search_name = i.split("/")[-1]
for search in os.listdir(copy_destination):
if search.startswith(search_name):
list_file_delete.append(os.path.join(copy_destination,search))
count+=1
if count > save_file:
print "Poveche sa s %d" % (count-save_file)
how_much = (count-save_file)
new_list_for_delete = {i:float(os.stat(i).st_mtime) for i in list_file_delete}
elem_to_delete = dict(list(sorted(new_list_for_delete.items(), key=lambda x: x[1]))[:(count-save_file)])
for del_elem in elem_to_delete.keys():
os.remove(del_elem)
#Function mace backup
def backup_resurse():
for i in file_destination:
backup = subprocess.Popen('''{0} -v -t -user {1} -password {2} {3}:{4} {5}{6}.fbk'''
.format(gback_file,user,password,server,i,copy_destination, i.split("/")[-1]+datetime),shell=True)
#Start the magic
if __name__ == "__main__":
check_file_and_filder(copy_destination,file_destination)
calculate()
backup_resurse()