(self)
Create backup on slave host.

        1. Obtain backup lock
        2. Pause WAL apply
        3. Wait for WAL apply to complete (look at PROGRESS file)
        4. Rotate old backups
        5. Copy data directory to data.master
        6. Create backup label and history file.
        7. Purge unneeded WAL-s
        8. Resume WAL apply
        9. Release backup lock
Create backup on slave host.
Create backup on slave host .
def slave_backup(self):
        """
        Create backup on slave host.

        1. Obtain backup lock
        2. Pause WAL apply
        3. Wait for WAL apply to complete (look at PROGRESS file)
        4. Rotate old backups
        5. Copy data directory to data.master
        6. Create backup label and history file.
        7. Purge unneeded WAL-s
        8. Resume WAL apply
        9. Release backup lock
        """
        self.assert_is_master(False)
        if self.slave_lock_backups() != 0:
            self.log.error("Cannot obtain backup lock.")
            sys.exit(1)

        try:
            self.slave_pause(waitcomplete=1)

            try:
                self.slave_rotate_backups()
                src = self.cf.getfile("slave_data")
                dst = self.cf.getfile("full_backup")

                start_time = time.localtime()
                cmdline = ["cp", "-a", src, dst ]
                self.log.info("Executing %s", " ".join(cmdline))
                if not self.not_really:
                    self.exec_cmd(cmdline)
                stop_time = time.localtime()

                # Obtain the last restart point information
                ctl = PgControlData(self.cf.getfile("slave_bin", ""), dst, True)

                # TODO: The newly created backup directory probably still contains
                # backup_label.old and recovery.conf files. Remove these.

                if not ctl.is_valid:
                    self.log.warning("Unable to determine last restart point, backup_label not created.")
                else:
                    # Write backup label and history file

                    backup_label = \
"""START WAL LOCATION: %(xlogid)X/%(xrecoff)X (file %(wal_name)s)
CHECKPOINT LOCATION: %(xlogid)X/%(xrecoff)X
START TIME: %(start_time)s
LABEL: SlaveBackup"
"""
                    backup_history = \
"""START WAL LOCATION: %(xlogid)X/%(xrecoff)X (file %(wal_name)s)
STOP WAL LOCATION: %(xlogid)X/%(xrecoff)X (file %(wal_name)s)
CHECKPOINT LOCATION: %(xlogid)X/%(xrecoff)X
START TIME: %(start_time)s
LABEL: SlaveBackup"
STOP TIME: %(stop_time)s
"""

                    label_params = {
                        "xlogid":       ctl.xlogid,
                        "xrecoff":      ctl.xrecoff,
                        "wal_name":     ctl.wal_name,
                        "start_time":   time.strftime("%Y-%m-%d %H:%M:%S %Z", start_time),
                        "stop_time":    time.strftime("%Y-%m-%d %H:%M:%S %Z", stop_time),
                    }

                    # Write the label
                    filename = os.path.join(dst, "backup_label")
                    if self.not_really:
                        self.log.info("Writing backup label to %s", filename)
                    else:
                        lf = open(filename, "w")
                        lf.write(backup_label % label_params)
                        lf.close()

                    # Now the history
                    histfile = "%s.%08X.backup" % (ctl.wal_name, ctl.xrecoff % ctl.wal_size)
                    completed_wals = self.cf.getfile("completed_wals")
                    filename = os.path.join(completed_wals, histfile)
                    if os.path.exists(filename):
                        self.log.warning("%s: already exists, refusing to overwrite.", filename)
                    else:
                        if self.not_really:
                            self.log.info("Writing backup history to %s", filename)
                        else:
                            lf = open(filename, "w")
                            lf.write(backup_history % label_params)
                            lf.close()

                self.slave_purge_wals()
            finally:
                self.slave_continue()
        finally:
            self.slave_resume_backups()
def slave_backup ( self ) : self . assert_is_master ( False ) if self . slave_lock_backups ( ) != 0 : self . log . error ( "Cannot obtain backup lock." ) sys . exit ( 1 ) try : self . slave_pause ( waitcomplete = 1 ) try : self . slave_rotate_backups ( ) src = self . cf . getfile ( "slave_data" ) dst = self . cf . getfile ( "full_backup" ) start_time = time . localtime ( ) cmdline = [ "cp" , "-a" , src , dst ] self . log . info ( "Executing %s" , " " . join ( cmdline ) ) if not self . not_really : self . exec_cmd ( cmdline ) stop_time = time . localtime ( ) # Obtain the last restart point information ctl = PgControlData ( self . cf . getfile ( "slave_bin" , "" ) , dst , True ) # TODO: The newly created backup directory probably still contains # backup_label.old and recovery.conf files. Remove these. if not ctl . is_valid : self . log . warning ( "Unable to determine last restart point, backup_label not created." ) else : # Write backup label and history file backup_label = """START WAL LOCATION: %(xlogid)X/%(xrecoff)X (file %(wal_name)s)
CHECKPOINT LOCATION: %(xlogid)X/%(xrecoff)X
START TIME: %(start_time)s
LABEL: SlaveBackup"
""" backup_history = """START WAL LOCATION: %(xlogid)X/%(xrecoff)X (file %(wal_name)s)
STOP WAL LOCATION: %(xlogid)X/%(xrecoff)X (file %(wal_name)s)
CHECKPOINT LOCATION: %(xlogid)X/%(xrecoff)X
START TIME: %(start_time)s
LABEL: SlaveBackup"
STOP TIME: %(stop_time)s
""" label_params = { "xlogid" : ctl . xlogid , "xrecoff" : ctl . xrecoff , "wal_name" : ctl . wal_name , "start_time" : time . strftime ( "%Y-%m-%d %H:%M:%S %Z" , start_time ) , "stop_time" : time . strftime ( "%Y-%m-%d %H:%M:%S %Z" , stop_time ) , } # Write the label filename = os . path . join ( dst , "backup_label" ) if self . not_really : self . log . info ( "Writing backup label to %s" , filename ) else : lf = open ( filename , "w" ) lf . write ( backup_label % label_params ) lf . close ( ) # Now the history histfile = "%s.%08X.backup" % ( ctl . wal_name , ctl . xrecoff % ctl . wal_size ) completed_wals = self . cf . getfile ( "completed_wals" ) filename = os . path . join ( completed_wals , histfile ) if os . path . exists ( filename ) : self . log . warning ( "%s: already exists, refusing to overwrite." , filename ) else : if self . not_really : self . log . info ( "Writing backup history to %s" , filename ) else : lf = open ( filename , "w" ) lf . write ( backup_history % label_params ) lf . close ( ) self . slave_purge_wals ( ) finally : self . slave_continue ( ) finally : self . slave_resume_backups ( )