]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/python/DirStartUp.py
Start Python documentation
[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      job.set_events(events)     # register events desired
26      sys.stderr = events          # send error output to Bacula
27      sys.stdout = events          # send stdout to Bacula
28      jobid = job.JobId; client = job.Client
29      numvols = job.NumVols 
30      job.JobReport="Python Dir JobStart: JobId=%d Client=%s NumVols=%d\n" % (jobid,client,numvols) 
31      return 1
32
33   # Bacula Job is going to terminate
34   def JobEnd(self, job):    
35      jobid = job.JobId
36      client = job.Client 
37      job.JobReport="Python Dir JobEnd output: JobId=%d Client=%s.\n" % (jobid, client) 
38      if (jobid < 2) :
39         startid = job.run("run kernsave")
40         print "Python started new Job: jobid=", startid
41      return 1
42
43   # Called here when the Bacula daemon is going to exit
44   def Exit(self, job):
45       print "Daemon exiting."
46      
47 bacula.set_events(BaculaEvents()) # register daemon events desired
48
49 """
50   There are the Job events that you can receive.
51 """
52 class JobEvents:
53   def __init__(self):
54      # Called here when you instantiate the Job. Not
55      # normally used
56      noop = 1
57
58   def NewVolume(self, job):
59      jobid = job.JobId
60      client = job.Client 
61      numvol = job.NumVols;
62      print "JobId=%d Client=%s NumVols=%d" % (jobid, client, numvol)
63      job.JobReport="Python before New Volume set for Job.\n"
64      job.VolumeName="TestA-001"
65      job.JobReport="Python after New Volume set for Job.\n"  
66      return 1
67
68
69   # Pass output back to Bacula
70   def write(self, text):
71      self.job.write(text)
72
73   # Open file to be backed up. file is the filename
74   def open(self, file):
75      print "Open %s called" % file
76      self.fd = open('m.py', 'rb')
77      jobid = self.job.JobId
78      print "Open: JobId=%d" % jobid
79      print "name=%s" % bacula.name
80
81   # Read file data into Bacula memory buffer (mem)
82   #  return length read. 0 => EOF, -1 => error
83   def read(self, mem):
84      print "Read called\n"
85      len = self.fd.readinto(mem)
86      print "Read %s bytes into mem.\n" % len
87      return len
88
89   # Close file
90   def close(self):
91      self.fd.close()