iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >PostgreSQL的备份工具pg_basebackup源码中的主函数分析
  • 645
分享到

PostgreSQL的备份工具pg_basebackup源码中的主函数分析

2024-04-02 19:04:59 645人浏览 安东尼
摘要

本篇内容主要讲解“postgresql的备份工具pg_basebackup源码中的主函数分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Postgresql的

本篇内容主要讲解“postgresql的备份工具pg_basebackup源码中的主函数分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Postgresql的备份工具pg_basebackup源码中的主函数分析”吧!

一、数据结构

option
使用工具时存储选项的数据结构

#ifndef HAVE_STRUCT_OPTioN
//工具软件选项
struct option
{
    const char *name;//名称
    int         has_arg;//是否包含参数,no_argument/required_argument/optional_argument
    int        *flag;//标记
    int         val;//参数值
};
#define no_argument 0
#define required_argument 1
#define optional_argument 2
#endif

#ifndef HAVE_INT_OPTERR
int         opterr = 1,         
            optind = 1,         
            optopt;             
char       *optarg;             
#endif
#define BADCH   (int)'?'
#define BADARG  (int)':'
#define EMSG    ""

二、源码解读

pg_basebackup主函数,源码较为简单,获取选项,校验,调用BaseBackup()函数进行备份.

int
main(int arGC, char **argv)
{
    static struct option long_options[] = {
        {"help", no_argument, NULL, '?'},
        {"version", no_argument, NULL, 'V'},
        {"pgdata", required_argument, NULL, 'D'},
        {"fORMat", required_argument, NULL, 'F'},
        {"checkpoint", required_argument, NULL, 'c'},
        {"create-slot", no_argument, NULL, 'C'},
        {"max-rate", required_argument, NULL, 'r'},
        {"write-recovery-conf", no_argument, NULL, 'R'},
        {"slot", required_argument, NULL, 'S'},
        {"tablespace-mapping", required_argument, NULL, 'T'},
        {"wal-method", required_argument, NULL, 'X'},
        {"gzip", no_argument, NULL, 'z'},
        {"compress", required_argument, NULL, 'Z'},
        {"label", required_argument, NULL, 'l'},
        {"no-clean", no_argument, NULL, 'n'},
        {"no-sync", no_argument, NULL, 'N'},
        {"dbname", required_argument, NULL, 'd'},
        {"host", required_argument, NULL, 'h'},
        {"port", required_argument, NULL, 'p'},
        {"username", required_argument, NULL, 'U'},
        {"no-passWord", no_argument, NULL, 'w'},
        {"password", no_argument, NULL, 'W'},
        {"status-interval", required_argument, NULL, 's'},
        {"verbose", no_argument, NULL, 'v'},
        {"progress", no_argument, NULL, 'P'},
        {"waldir", required_argument, NULL, 1},
        {"no-slot", no_argument, NULL, 2},
        {"no-verify-checksums", no_argument, NULL, 3},
        {NULL, 0, NULL, 0}
    };//(完整)选项
    //选项的ASCII值
    int         c;
    //选项索引编号
    int         option_index;
    //程序名称
    progname = get_progname(argv[0]);
    set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
    if (argc > 1)
    {
        //显示帮助信息
        if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
        {
            usage();
            exit(0);
        }
        else if (strcmp(argv[1], "-V") == 0
                 || strcmp(argv[1], "--version") == 0)
        {
            //显示版本信息
            puts("pg_basebackup (PostgreSQL) " PG_VERSION);
            exit(0);
        }
    }
    atexit(cleanup_directories_atexit);
    //getopt_long --> 获取选项
    while ((c = getopt_long(argc, argv, "CD:F:r:RS:T:X:l:nNzZ:d:c:h:p:U:s:wWkvP",
                            long_options, &option_index)) != -1)
    {
        switch (c)//根据选项设置参数
        {
            case 'C':
                create_slot = true;
                break;
            case 'D':
                basedir = pg_strdup(optarg);
                break;
            case 'F':
                if (strcmp(optarg, "p") == 0 || strcmp(optarg, "plain") == 0)
                    format = 'p';//不压缩
                else if (strcmp(optarg, "t") == 0 || strcmp(optarg, "tar") == 0)
                    format = 't';//tar包
                else
                {
                    fprintf(stderr,
                            _("%s: invalid output format \"%s\", must be \"plain\" or \"tar\"\n"),
                            progname, optarg);
                    exit(1);
                }
                break;
            case 'r':
                maxrate = parse_max_rate(optarg);
                break;
            case 'R':
                writerecoveryconf = true;
                break;
            case 'S':
                
                replication_slot = pg_strdup(optarg);
                temp_replication_slot = false;
                break;
            case 2:
                no_slot = true;
                break;
            case 'T':
                tablespace_list_append(optarg);
                break;
            case 'X':
                if (strcmp(optarg, "n") == 0 ||
                    strcmp(optarg, "none") == 0)
                {
                    includewal = NO_WAL;
                }
                else if (strcmp(optarg, "f") == 0 ||
                         strcmp(optarg, "fetch") == 0)
                {
                    includewal = FETCH_WAL;
                }
                else if (strcmp(optarg, "s") == 0 ||
                         strcmp(optarg, "stream") == 0)
                {
                    includewal = STREAM_WAL;
                }
                else
                {
                    fprintf(stderr,
                            _("%s: invalid wal-method option \"%s\", must be \"fetch\", \"stream\", or \"none\"\n"),
                            progname, optarg);
                    exit(1);
                }
                break;
            case 1:
                xlog_dir = pg_strdup(optarg);
                break;
            case 'l':
                label = pg_strdup(optarg);
                break;
            case 'n':
                noclean = true;
                break;
            case 'N':
                do_sync = false;
                break;
            case 'z':
#ifdef HAVE_LIBZ
                compresslevel = Z_DEFAULT_COMPRESSION;
#else
                compresslevel = 1;  
#endif
                break;
            case 'Z':
                compresslevel = atoi(optarg);
                if (compresslevel < 0 || compresslevel > 9)
                {
                    fprintf(stderr, _("%s: invalid compression level \"%s\"\n"),
                            progname, optarg);
                    exit(1);
                }
                break;
            case 'c':
                if (pg_strcasecmp(optarg, "fast") == 0)
                    fastcheckpoint = true;
                else if (pg_strcasecmp(optarg, "spread") == 0)
                    fastcheckpoint = false;
                else
                {
                    fprintf(stderr, _("%s: invalid checkpoint argument \"%s\", must be \"fast\" or \"spread\"\n"),
                            progname, optarg);
                    exit(1);
                }
                break;
            case 'd':
                connection_string = pg_strdup(optarg);
                break;
            case 'h':
                dbhost = pg_strdup(optarg);
                break;
            case 'p':
                dbport = pg_strdup(optarg);
                break;
            case 'U':
                dbuser = pg_strdup(optarg);
                break;
            case 'w':
                dbgetpassword = -1;
                break;
            case 'W':
                dbgetpassword = 1;
                break;
            case 's':
                standby_message_timeout = atoi(optarg) * 1000;
                if (standby_message_timeout < 0)
                {
                    fprintf(stderr, _("%s: invalid status interval \"%s\"\n"),
                            progname, optarg);
                    exit(1);
                }
                break;
            case 'v':
                verbose++;
                break;
            case 'P':
                showprogress = true;
                break;
            case 3:
                verify_checksums = false;
                break;
            default:
                
                fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                        progname);
                exit(1);
        }
    }
    
    if (optind < argc)
    {
        fprintf(stderr,
                _("%s: too many command-line arguments (first is \"%s\")\n"),
                progname, argv[optind]);
        fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                progname);
        exit(1);
    }
    
    if (basedir == NULL)
    {
        fprintf(stderr, _("%s: no target directory specified\n"), progname);
        fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                progname);
        exit(1);
    }
    
    if (format == 'p' && compresslevel != 0)
    {
        fprintf(stderr,
                _("%s: only tar mode backups can be compressed\n"),
                progname);
        fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                progname);
        exit(1);
    }
    if (format == 't' && includewal == STREAM_WAL && strcmp(basedir, "-") == 0)
    {
        fprintf(stderr,
                _("%s: cannot stream write-ahead logs in tar mode to stdout\n"),
                progname);
        fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                progname);
        exit(1);
    }
    if (replication_slot && includewal != STREAM_WAL)
    {
        fprintf(stderr,
                _("%s: replication slots can only be used with WAL streaming\n"),
                progname);
        fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                progname);
        exit(1);
    }
    if (no_slot)
    {
        if (replication_slot)
        {
            fprintf(stderr,
                    _("%s: --no-slot cannot be used with slot name\n"),
                    progname);
            fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                    progname);
            exit(1);
        }
        temp_replication_slot = false;
    }
    if (create_slot)
    {
        if (!replication_slot)
        {
            fprintf(stderr,
                    _("%s: %s needs a slot to be specified using --slot\n"),
                    progname, "--create-slot");
            fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                    progname);
            exit(1);
        }
        if (no_slot)
        {
            fprintf(stderr,
                    _("%s: --create-slot and --no-slot are incompatible options\n"),
                    progname);
            fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                    progname);
            exit(1);
        }
    }
    if (xlog_dir)
    {
        if (format != 'p')
        {
            fprintf(stderr,
                    _("%s: WAL directory location can only be specified in plain mode\n"),
                    progname);
            fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                    progname);
            exit(1);
        }
        
        canonicalize_path(xlog_dir);
        if (!is_absolute_path(xlog_dir))
        {
            fprintf(stderr, _("%s: WAL directory location must be "
                              "an absolute path\n"), progname);
            fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
                    progname);
            exit(1);
        }
    }
#ifndef HAVE_LIBZ
    if (compresslevel != 0)
    {
        fprintf(stderr,
                _("%s: this build does not support compression\n"),
                progname);
        exit(1);
    }
#endif
    
    //以复制模式连接到数据库
    conn = GetConnection();
    if (!conn)
    {
        
        //连接不成功
        exit(1);
    }
    atexit(disconnect_atexit);
    
    umask(pg_mode_mask);
    
    if (format == 'p' || strcmp(basedir, "-") != 0)
        verify_dir_is_empty_or_create(basedir, &made_new_pgdata, &found_existing_pgdata);
    
    //确定远程服务器的xlog segment大小
    if (!RetrieveWalSegSize(conn))
        exit(1);
    
    //如需要,创建pg_wal目录链接
    if (xlog_dir)
    {
        char       *linkloc;
        //
        verify_dir_is_empty_or_create(xlog_dir, &made_new_xlogdir, &found_existing_xlogdir);
        
        linkloc = psprintf("%s/%s", basedir,
                           PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
                           "pg_xlog" : "pg_wal");
#ifdef HAVE_SYMLINK
        if (symlink(xlog_dir, linkloc) != 0)
        {
            fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"),
                    progname, linkloc, strerror(errno));
            exit(1);
        }
#else
        fprintf(stderr, _("%s: symlinks are not supported on this platform\n"), progname);
        exit(1);
#endif
        free(linkloc);
    }
    //执行备份
    BaseBackup();
    //成功标记
    success = true;
    return 0;
}

void
set_pglocale_pgservice(const char *argv0, const char *app)
{
    char        path[MAXPGPATH];
    char        my_exec_path[MAXPGPATH];
    char        env_path[MAXPGPATH + sizeof("PGSYSCONFDIR=")];  
    char       *dup_path;
    
    //不要在后台进程中设置LC_ALL
    if (strcmp(app, PG_TEXTDOMAIN("postgres")) != 0)
    {
        setlocale(LC_ALL, "");
        
    }
    if (find_my_exec(argv0, my_exec_path) < 0)
        return;
#ifdef ENABLE_NLS
    get_locale_path(my_exec_path, path);
    bindtextdomain(app, path);
    textdomain(app);
    if (getenv("PGLOCALEDIR") == NULL)
    {
        //------ PGLOCALEDIR
        
        //设置libpq
        snprintf(env_path, sizeof(env_path), "PGLOCALEDIR=%s", path);
        canonicalize_path(env_path + 12);
        dup_path = strdup(env_path);
        if (dup_path)
            putenv(dup_path);
    }
#endif
    if (getenv("PGSYSCONFDIR") == NULL)
    {
        //---- PGSYSCONFDIR
        get_etc_path(my_exec_path, path);
        
        snprintf(env_path, sizeof(env_path), "PGSYSCONFDIR=%s", path);
        canonicalize_path(env_path + 13);
        dup_path = strdup(env_path);
        if (dup_path)
            putenv(dup_path);
    }
}

到此,相信大家对“PostgreSQL的备份工具pg_basebackup源码中的主函数分析”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

您可能感兴趣的文档:

--结束END--

本文标题: PostgreSQL的备份工具pg_basebackup源码中的主函数分析

本文链接: https://www.lsjlt.com/news/64196.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • oracle怎么查询当前用户所有的表
    要查询当前用户拥有的所有表,可以使用以下 sql 命令:select * from user_tables; 如何查询当前用户拥有的所有表 要查询当前用户拥有的所有表,可以使...
    99+
    2024-05-14
    oracle
  • oracle怎么备份表中数据
    oracle 表数据备份的方法包括:导出数据 (exp):将表数据导出到外部文件。导入数据 (imp):将导出文件中的数据导入表中。用户管理的备份 (umr):允许用户控制备份和恢复过程...
    99+
    2024-05-14
    oracle
  • oracle怎么做到数据实时备份
    oracle 实时备份通过持续保持数据库和事务日志的副本来实现数据保护,提供快速恢复。实现机制主要包括归档重做日志和 asm 卷管理系统。它最小化数据丢失、加快恢复时间、消除手动备份任务...
    99+
    2024-05-14
    oracle 数据丢失
  • oracle怎么查询所有的表空间
    要查询 oracle 中的所有表空间,可以使用 sql 语句 "select tablespace_name from dba_tablespaces",其中 dba_tabl...
    99+
    2024-05-14
    oracle
  • oracle怎么创建新用户并赋予权限设置
    答案:要创建 oracle 新用户,请执行以下步骤:以具有 create user 权限的用户身份登录;在 sql*plus 窗口中输入 create user identified ...
    99+
    2024-05-14
    oracle
  • oracle怎么建立新用户
    在 oracle 数据库中创建用户的方法:使用 sql*plus 连接数据库;使用 create user 语法创建新用户;根据用户需要授予权限;注销并重新登录以使更改生效。 如何在 ...
    99+
    2024-05-14
    oracle
  • oracle怎么创建新用户并赋予权限密码
    本教程详细介绍了如何使用 oracle 创建一个新用户并授予其权限:创建新用户并设置密码。授予对特定表的读写权限。授予创建序列的权限。根据需要授予其他权限。 如何使用 Oracle 创...
    99+
    2024-05-14
    oracle
  • oracle怎么查询时间段内的数据记录表
    在 oracle 数据库中查询指定时间段内的数据记录表,可以使用 between 操作符,用于比较日期或时间的范围。语法:select * from table_name wh...
    99+
    2024-05-14
    oracle
  • oracle怎么查看表的分区
    问题:如何查看 oracle 表的分区?步骤:查询数据字典视图 all_tab_partitions,指定表名。结果显示分区名称、上边界值和下边界值。 如何查看 Oracle 表的分区...
    99+
    2024-05-14
    oracle
  • oracle怎么导入dump文件
    要导入 dump 文件,请先停止 oracle 服务,然后使用 impdp 命令。步骤包括:停止 oracle 数据库服务。导航到 oracle 数据泵工具目录。使用 impdp 命令导...
    99+
    2024-05-14
    oracle
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作