Bacula-users

[Bacula-users] Subject: Python in bacula 7

2015-04-23 04:10:30
Subject: [Bacula-users] Subject: Python in bacula 7
From: Olivier Delestre <olivier.delestre AT univ-rouen DOT fr>
To: bacula-users AT lists.sourceforge DOT net
Date: Thu, 23 Apr 2015 10:07:23 +0200
Hi,

I use a python script with
RunBeforeJob = "/usr/local/sbin/delete_client_trolling_pool_1mois.py %c %l"

( not in run time ).

And my bacula is 7.0.5

i place my script for an exemple, :
-------------------------------------------------------------------------------
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
# Script pour effacer tout ce qui est avant la derniere Full
# Uniquement executer par Bacula en before
# param 1 : nom du client post fixer avec -fd
# param 2 : JobLevel ( Full ou Incremental )
# Attention dangereux si un job est splitte en plusieurs bande ( Maximum 
Volume Bytes )

import collections
import os
import re
import subprocess
import sys
import psycopg2

Volume = collections.namedtuple('Volume', 'level, startTime, volumeName')

hostname = re.match(r'^(.+?)(-fd)?$', sys.argv[1]).group(1)
joblevel=sys.argv[2]

# hostname exception
if hostname == "xxxxx":
  sys.exit()

if joblevel != "Full":
  sys.exit()

conn = psycopg2.connect("dbname=bacula user=postgres")
cur  = conn.cursor()
cur.execute("""SELECT DISTINCT Level,StartTime,VolumeName
FROM Client,Job,JobMedia,Media,FileSet
WHERE Client.Name='{hostname}-fd'
   AND FileSet='fileset_{hostname}'
   AND Client.ClientId=Job.ClientId
   AND Job.Type='B'
   AND Job.JobStatus='T'
   AND Job.FileSetId=FileSet.FileSetId
   AND JobMedia.JobId=Job.JobId
   AND JobMedia.MediaId=Media.MediaId
ORDER BY Job.StartTime;""".format(hostname=hostname))

results = [ Volume._make(item) for item in cur.fetchall() ]
# recupere le nombre de totale de la requete SQL
fullNum = len([ item for item in results if item.level == 'F' ])

deleteList = []
for item in results:
     if fullNum > 1:
         if item.level == 'F':
             fullNum -= 1
         deleteList.append(item)
     else:
         if item.level == 'F':
             break
         else:
             deleteList.append(item)

## Debugging
#for item in results:
#    if item in deleteList:
#        print '\033[1;31m%s\033[0m' % str(item)
#    else:
#        print item


p_stdin = ""
for item in deleteList:
     p_stdin += "purge volume=%s\n" % item.volumeName
p_bconsole = subprocess.Popen(['/usr/sbin/bconsole'], 
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p_stdout, p_stderr = p_bconsole.communicate( p_stdin )
##print p_stdout, p_stderr

for item in deleteList:
     _path = os.path.join('/sauvegarde', hostname, item.volumeName)
#    print _path
     try:
         os.unlink(_path)
     except OSError:
         pass

# EOF - vim:ts=4:sw=4:et:bg=dark:
-------------------------------------------------------------------------------


------------------------------------------------------------------------------
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual- event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF
_______________________________________________
Bacula-users mailing list
Bacula-users AT lists.sourceforge DOT net
https://lists.sourceforge.net/lists/listinfo/bacula-users

<Prev in Thread] Current Thread [Next in Thread>
  • [Bacula-users] Subject: Python in bacula 7, Olivier Delestre <=