SmtC: Show me the Code
Ole Peter Smith
Instituto de Matemática e Estatística
Universidade Federal de Goiás
http://www.olesmith.com.br

Files Backup
Quando eu dou de comer aos pobres
Me chamam de santo
Quando eu pergunto por que eles são pobres
Me chamam de comunista
Dom Helder Câmera
< Crontab | Backup one Directory | Backup Several Directories >

Backup one Directory

Python Listing: ../../dayly.tar.py.
#!/usr/bin/python

import os,sys,time

basepath="/usr/local/tars/systems"

tarbin="/bin/tar"
tarflags="fz"
scpbin="/usr/bin/scp"
mkdirbin="/bin/mkdir"

srchost="sysime"

def Read_Args():
   args=list(sys.argv)
   args.pop(0)

   srcpath=tarfile=None

   if (len(args)>0):
      srcpath=args[0]
      args.pop(0)

   if (len(args)>0):
      tarfile=args[0]
      args.pop(0)

   hosts=args


   if (not srcpath or not tarfile):
      print "Insufficient arguments, usage: "+sys.argv[0],"srcpath tarfile desthost1 ... desthostN"


   return srcpath,tarfile,hosts

def Run_Command(command):
   print "\t"," ".join(command)+":",
   res=os.system(" ".join(command))
   print res

   return res



def Date_Comps_List():
   return [
      time.strftime("%Y",time.localtime()),
      time.strftime("%m",time.localtime()),
      time.strftime("%d",time.localtime()),
   ]

def Tar_File(basepath,tarfile):
   dates=Date_Comps_List()
   name="-".join(   [tarfile,srchost,".".join(dates+["tgz"])]   )

   dates.pop()
   return "/".join(   [basepath]+dates+[name]  )

def Create_Tar(srcpath,basepath,tarfile,hosts):
   tarfile=Tar_File(basepath,tarfile)
   tarpath=os.path.dirname(tarfile)

   mkdircommands=[mkdirbin,"-p"]
   Run_Command(mkdircommands+[tarpath])

   for host in hosts:
      Run_Command(["/usr/local/sbin/"+host]+mkdircommands+[tarpath])



   if (not os.path.isdir(srcpath)):
      print "No such source directory:",srcpath
      return

   Run_Command([
     tarbin,
     "c"+tarflags,
     tarfile,
     srcpath+"/*"
   ])

   return tarfile

def Copy_Tar(tarfile,hosts):
   for host in hosts:
      Run_Command([
         scpbin,
         tarfile,
         host+":"+tarfile
      ])

srcpath,tarfile,hosts=Read_Args()

tarfile=Create_Tar(srcpath,basepath,tarfile,hosts)
Copy_Tar(tarfile,hosts)
< Crontab | Backup one Directory | Backup Several Directories >
Messages:
0 secs.