]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/python/DirStartUp.py
- Add Database vendor to CatalogRes tuple for Python.
[bacula/bacula] / bacula / examples / python / DirStartUp.py
1 #
2 # Bacula Python interface script for the Director
3 #
4
5 # You must import both sys and bacula
6 import sys, bacula
7
8 # This is the list of Bacula daemon events that you
9 #  can receive.
10 class BaculaEvents:
11   def __init__(self):
12      # Called here when a new Bacula Events class is
13      #  is created. Normally not used 
14      noop = 1
15
16   def JobStart(self, job):
17      """
18        Called here when a new job is started. If you want
19        to do anything with the Job, you must register
20        events you want to receive.
21      """
22      events = JobEvents()         # create instance of Job class
23      events.job = job             # save Bacula's job pointer
24      job.set_events(events)       # register events desired
25      sys.stderr = events          # send error output to Bacula
26      sys.stdout = events          # send stdout to Bacula
27      jobid = job.JobId; client = job.Client
28      numvols = job.NumVols 
29      job.JobReport="Python Dir JobStart: JobId=%d Client=%s NumVols=%d\n" % (jobid,client,numvols) 
30
31   # Bacula Job is going to terminate
32   def JobEnd(self, job):    
33      jobid = job.JobId
34      client = job.Client 
35      job.JobReport="Python Dir JobEnd output: JobId=%d Status=%s Client=%s.\n" % (jobid, job.JobStatus, client) 
36
37   # Called here when the Bacula daemon is going to exit
38   def Exit(self, job):
39       print "Daemon exiting."
40      
41 bacula.set_events(BaculaEvents()) # register daemon events desired
42
43 """
44   There are the Job events that you can receive.
45 """
46 class JobEvents:
47   def __init__(self):
48      # Called here when you instantiate the Job. Not
49      # normally used
50      noop = 1
51
52   def JobInit(self, job):
53      noop = 1
54      if (job.JobId < 2):
55         startid = job.run("run kernsave")
56         job.JobReport = "Python started new Job: jobid=%d\n" % startid
57
58   def JobRun(self, job):
59      noop = 1
60
61   def NewVolume(self, job):
62      jobid = job.JobId
63      client = job.Client 
64      numvol = job.NumVols;
65      print job.CatalogRes
66      job.JobReport = "JobId=%d Client=%s NumVols=%d" % (jobid, client, numvol)
67      job.JobReport="Python before New Volume set for Job.\n"
68      Vol = "TestA-%d" % numvol
69      job.JobReport = "Exists=%d TestA-%d" % (job.DoesVolumeExist(Vol), numvol)
70      job.VolumeName="TestA-%d" % numvol 
71      job.JobReport="Python after New Volume set for Job.\n"  
72      return 1
73
74
75   # Pass output back to Bacula
76   def write(self, text):
77      self.job.write(text)
78
79   # Open file to be backed up. file is the filename
80   def open(self, file):
81      print "Open %s called" % file
82      self.fd = open('m.py', 'rb')
83      jobid = self.job.JobId
84      print "Open: JobId=%d" % jobid
85      print "name=%s" % bacula.name
86
87   # Read file data into Bacula memory buffer (mem)
88   #  return length read. 0 => EOF, -1 => error
89   def read(self, mem):
90      print "Read called\n"
91      len = self.fd.readinto(mem)
92      print "Read %s bytes into mem.\n" % len
93      return len
94
95   # Close file
96   def close(self):
97      self.fd.close()