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