]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/python/SDStartUp.py
- Fix new Python code to work for Director.
[bacula/bacula] / bacula / examples / python / SDStartUp.py
1 #
2 # Bacula Python interface script
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.get("JobId")
28      client = job.get("Client")
29      numvols = job.get("NumVols") 
30      job.set(JobReport="Python StartJob: 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.get("JobId")
36      client = job.get("Client") 
37      job.set(JobReport="Python EndJob 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       noop = 1
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.get("JobId")
60      print "JobId=", jobid
61      client = job.get("Client") 
62      print "Client=" + client
63      numvol = job.get("NumVols");
64      print "NumVols=", numvol
65      job.set(JobReport="Python New Volume set for Job.\n") 
66      job.set(VolumeName="TestA-001")
67      return 1
68
69
70   # Pass output back to Bacula
71   def write(self, text):
72      self.job.write(text)
73
74   # Open file to be backed up. file is the filename
75   def open(self, file):
76      print "Open %s called" % file
77      self.fd = open('m.py', 'rb')
78      jobid = self.job.get("JobId")
79      print "Open: JobId=%d" % jobid
80      print "name=%s" % bacula.name
81
82   # Read file data into Bacula memory buffer (mem)
83   #  return length read. 0 => EOF, -1 => error
84   def read(self, mem):
85      print "Read called\n"
86      len = self.fd.readinto(mem)
87      print "Read %s bytes into mem.\n" % len
88      return len
89
90   # Close file
91   def close(self):
92      self.fd.close()