Friday 13 January 2012

Embed python with rsync

This is a nice way to control the backup in our company. I just Embed python with rsync 
This is a basci scheme.
'a' copy to 'b', files in production are copy to 'a' and 'b' copy to 'c' 
--------------------------
A—->  B —> C
-------------------------

 
#!/usr/bin/env python
#better backup, made in python
#Autor: peter pjl@hpc.com.py
from subprocess import call
import sys
import time
import datetime

def bkp()
    for bkp in [0, 1, 2]:
 if bkp == 0:
 #B to C
    sync("/mnt/company/","192.168.1.16:/mnt/companybckall","company")
    sync("/mnt/company1/","192.168.1.16:/mnt/company1bckall","company1")
    sync("/mnt/company2/","192.168.1.16:/mnt/company2bckall","company2")
    sync("/mnt/company3/","192.168.1.16:/mnt/company3bckall","company3")
     elif bkp == 1:
     #files in production to A
    sync("/media/company/","/mnt/companybckall","company")
    sync("192.168.12.2:/media/users/company/","/mnt/companybckall","company1")
    sync("192.168.13.2:/media/users/company/","/mnt/companybckall","company2")
    sync("192.168.10.8:/media/company/","/mnt/companybckall","company3")
     elif bkp == 2:
     #A to B
    sync("/mnt/comanybckall/","192.168.10.8:/mnt/companybckall","company")
    sync("/mnt/company1bckall/","192.168.10.8:/mnt/company1bckall","company1")
    sync("/mnt/company2bckall/","192.168.10.8:/mnt/company2bckall","company2")
    sync("/mnt/company3bckall/","192.168.10.8:/mnt/company3bckall","company3")

def sync(sources, destins, logs):
    fecha = datetime.date.today()
    fechastr = fecha.strftime("%Y-%m-%d")
    lognom = destins + "/" + fechastr + "-" + logs + ".log"
    logerror = destins + "/" + fechastr + "-" + logs + "err" + ".log"
    rsync = "rsync"
    # the arguments of the ssh must be in this way to work with crontab :)
    argum = "-agEvvz --exclude-from=/root/excluir -e \"ssh -i /home/peter/.ssh/id_rsa\""
    cmd = "%s %s %s %s" % (rsync, argum, sources, destins)
    while True:
 ret = call(cmd, shell=True, stdout=open(lognom, "a+"), stderr=open(logerror, "a+"))
 if ret != 0:
    time.sleep(30)
 else:
    sys.exit(0)

if __name__ == "__main__":
   bkp()
 
 
 
this is just a basic use 

No comments:

Post a Comment