(self)
Copy master data directory to slave.

        1. Obtain backup lock on slave.
        2. Rotate backups on slave
        3. Perform backup as usual
        4. Purge unneeded WAL-s from slave
        5. Release backup lock
Copy master data directory to slave.
Copy master data directory to slave .
def master_backup(self):
        """
        Copy master data directory to slave.

        1. Obtain backup lock on slave.
        2. Rotate backups on slave
        3. Perform backup as usual
        4. Purge unneeded WAL-s from slave
        5. Release backup lock
        """

        self.remote_xlock()
        errors = False

        try:
            self.pg_start_backup("FullBackup")
            self.remote_walmgr("xrotate")

            data_dir = self.cf.getfile("master_data")
            dst_loc = self.cf.getfile("full_backup")
            if dst_loc[-1] != "/":
                dst_loc += "/"

            master_spc_dir = os.path.join(data_dir, "pg_tblspc")
            slave_spc_dir = dst_loc + "tmpspc"

            # copy data
            self.chdir(data_dir)
            cmdline = [
                    "--delete",
                    "--exclude", ".*",
                    "--exclude", "*.pid",
                    "--exclude", "*.opts",
                    "--exclude", "*.conf",
                    "--exclude", "pg_xlog",
                    "--exclude", "pg_tblspc",
                    "--exclude", "pg_log",
                    "--exclude", "base/pgsql_tmp",
                    "--copy-unsafe-links",
                    ".", dst_loc]
            self.exec_big_rsync(cmdline)

            # copy tblspc first, to test
            if os.path.isdir(master_spc_dir):
                self.log.info("Checking tablespaces")
                list = os.listdir(master_spc_dir)
                if len(list) > 0:
                    self.remote_mkdir(slave_spc_dir)
                for tblspc in list:
                    if tblspc[0] == ".":
                        continue
                    tfn = os.path.join(master_spc_dir, tblspc)
                    if not os.path.islink(tfn):
                        self.log.info("Suspicious pg_tblspc entry: %s", tblspc)
                        continue
                    spc_path = os.path.realpath(tfn)
                    self.log.info("Got tablespace %s: %s", tblspc, spc_path)
                    dstfn = slave_spc_dir + "/" + tblspc

                    try:
                        os.chdir(spc_path)
                    except Exception, det:
                        self.log.warning("Broken link: %s", det)
                        continue
                    cmdline = [ "--delete", "--exclude", ".*", "--copy-unsafe-links", ".", dstfn]
                    self.exec_big_rsync(cmdline)

            # copy the pg_log and pg_xlog directories, these may be
            # symlinked to nonstandard location, so pay attention
            self.rsync_log_directory(os.path.join(data_dir, "pg_log"),  dst_loc)
            self.rsync_log_directory(os.path.join(data_dir, "pg_xlog"), dst_loc)

            # copy config files
            conf_dst_loc = self.cf.getfile("config_backup", "")
            if conf_dst_loc:
                master_conf_dir = os.path.dirname(self.cf.getfile("master_config"))
                self.log.info("Backup conf files from %s", master_conf_dir)
                self.chdir(master_conf_dir)
                cmdline = [
                     "--include", "*.conf",
                     "--exclude", "*",
                     ".", conf_dst_loc]
                self.exec_big_rsync(cmdline)

            self.remote_walmgr("xpurgewals")
        except Exception, e:
            self.log.error(e)
            errors = True
        finally:
            try:
                self.pg_stop_backup()
            except:
                pass

        try:
            self.remote_walmgr("xrelease")
        except:
            pass

        if not errors:
            self.log.info("Full backup successful")
        else:
            self.log.error("Full backup failed.")
def master_backup ( self ) : self . remote_xlock ( ) errors = False try : self . pg_start_backup ( "FullBackup" ) self . remote_walmgr ( "xrotate" ) data_dir = self . cf . getfile ( "master_data" ) dst_loc = self . cf . getfile ( "full_backup" ) if dst_loc [ - 1 ] != "/" : dst_loc += "/" master_spc_dir = os . path . join ( data_dir , "pg_tblspc" ) slave_spc_dir = dst_loc + "tmpspc" # copy data self . chdir ( data_dir ) cmdline = [ "--delete" , "--exclude" , ".*" , "--exclude" , "*.pid" , "--exclude" , "*.opts" , "--exclude" , "*.conf" , "--exclude" , "pg_xlog" , "--exclude" , "pg_tblspc" , "--exclude" , "pg_log" , "--exclude" , "base/pgsql_tmp" , "--copy-unsafe-links" , "." , dst_loc ] self . exec_big_rsync ( cmdline ) # copy tblspc first, to test if os . path . isdir ( master_spc_dir ) : self . log . info ( "Checking tablespaces" ) list = os . listdir ( master_spc_dir ) if len ( list ) > 0 : self . remote_mkdir ( slave_spc_dir ) for tblspc in list : if tblspc [ 0 ] == "." : continue tfn = os . path . join ( master_spc_dir , tblspc ) if not os . path . islink ( tfn ) : self . log . info ( "Suspicious pg_tblspc entry: %s" , tblspc ) continue spc_path = os . path . realpath ( tfn ) self . log . info ( "Got tablespace %s: %s" , tblspc , spc_path ) dstfn = slave_spc_dir + "/" + tblspc try : os . chdir ( spc_path ) except Exception , det : self . log . warning ( "Broken link: %s" , det ) continue cmdline = [ "--delete" , "--exclude" , ".*" , "--copy-unsafe-links" , "." , dstfn ] self . exec_big_rsync ( cmdline ) # copy the pg_log and pg_xlog directories, these may be # symlinked to nonstandard location, so pay attention self . rsync_log_directory ( os . path . join ( data_dir , "pg_log" ) , dst_loc ) self . rsync_log_directory ( os . path . join ( data_dir , "pg_xlog" ) , dst_loc ) # copy config files conf_dst_loc = self . cf . getfile ( "config_backup" , "" ) if conf_dst_loc : master_conf_dir = os . path . dirname ( self . cf . getfile ( "master_config" ) ) self . log . info ( "Backup conf files from %s" , master_conf_dir ) self . chdir ( master_conf_dir ) cmdline = [ "--include" , "*.conf" , "--exclude" , "*" , "." , conf_dst_loc ] self . exec_big_rsync ( cmdline ) self . remote_walmgr ( "xpurgewals" ) except Exception , e : self . log . error ( e ) errors = True finally : try : self . pg_stop_backup ( ) except : pass try : self . remote_walmgr ( "xrelease" ) except : pass if not errors : self . log . info ( "Full backup successful" ) else : self . log . error ( "Full backup failed." )