6.2. 导出数据


LDAP 数据交换格式(LDIF)文件用于从 Directory Server 数据库导出数据库条目。LDIF 是 RFC 2849 中所述的标准格式。
注意
导出操作不会导出配置信息(cn=config)、架构信息(cn=schema)或监控信息(cn=monitor)。
导出数据可用于以下内容:
  • 备份数据库中的数据。
  • 将数据复制到其他目录服务器.
  • 将数据导出到另一个应用程序。
  • 在更改目录拓扑后重新填充数据库。
    例如,如果目录包含一个数据库,并且其内容应分割为两个数据库,那么两个新数据库应通过导出旧数据库的内容并导入到两个新数据库中来接收其数据,如 图 6.1 “将数据库内容分成两个数据库” 所示。

    图 6.1. 将数据库内容分成两个数据库

警告
不要在导出操作期间停止服务器。
目录服务器以 dirsrv 用户身份运行导出操作。因此,目标目录的权限必须允许此用户写入该文件。

6.2.1. 使用命令行将数据导出到 LDIF 文件中

目录服务器支持在实例正在运行或实例离线时导出数据:
重要
由于以下原因,不要将 LDIF 文件导出到 /tmp 或 /var /tmp/ 目录中:
  • 目录服务器默认使用 systemdPrivateTmp 功能。如果您将 LDIF 文件放在 /tmp/var/tmp/ 系统目录中,目录服务器在导入过程中不会看到这些 LDIF 文件。有关 PrivateTmp 的更多信息,请参阅 systemd.exec (5) 手册页。
  • LDIF 文件通常包含敏感数据,如用户密码。因此,您不能使用临时系统目录来存储这些文件。

6.2.1.1. 导出数据库,但服务器正在运行

使用 dsconf backend export 命令自动创建将数据导出到 LDIF 文件的任务。
例如,要导出 userRoot 数据库:
# dsconf -D "cn=Directory Manager" ldap://server.example.com backend export userRoot
The export task has finished successfully
Copy to Clipboard Toggle word wrap
默认情况下,dsconf 将导出存储在 /var/lib/dirsrv/slapd -instance_name/export/ 目录中的名为instance_name_database_name-time_stamp.ldif 的文件中。或者,在命令中添加 the -l file_name 选项来指定不同的位置。
dsconf backend export 命令支持额外的选项,例如排除特定的后缀。要显示所有可用的选项,请输入:
# dsconf ldap://server.example.com backend export --help
Copy to Clipboard Toggle word wrap
6.2.1.1.2. 使用 cn=tasks Entry 导出数据库
Directory Server 配置中的 cn=tasks,cn=config 条目是服务器用来管理任务的临时条目的容器条目。要启动导出操作,请在 cn=export,cn=tasks,cn=config 条目中创建一个任务。
使用任务条目可让您在服务器运行时导出数据。
导出任务条目需要以下属性:
  • cn: 设置任务的唯一名称。
  • nsInstance :设置要导出的数据库的名称。
  • nsFilename :设置应将导出存储到的文件的名称。
导出任务支持其他参数,例如排除后缀。有关完整列表,请参阅 红帽目录服务器配置、命令和文件参考中的 cn=export 部分。
例如,添加一个将 userRoot 数据库内容导出到 /var/lib/dirsrv/slapd-instance_name/ldif/example.ldif 文件的任务:
# ldapadd -D "cn=Directory Manager" -W -H ldap://server.example.com -x

dn: cn=example_export,cn=export,cn=tasks,cn=config
changetype: add
objectclass: extensibleObject
cn: example_export
nsInstance: userRoot
nsFilename: /var/lib/dirsrv/slapd-instance_name/ldif/example.ldif
Copy to Clipboard Toggle word wrap
任务完成后,该条目会从目录配置中删除。

6.2.1.2. 导出数据库,但服务器处于离线状态

如果在导出数据时服务器离线,请使用 dsctl db2ldif 命令:
  1. 停止实例:
    # dsctl instance_name stop
    Copy to Clipboard Toggle word wrap
  2. 将数据库导出到 LDIF 文件中。例如,要将 userRoot 数据库导出到 /var/lib/dirsrv/slapd-instance_name/ldif/example.ldif 文件中:
    # dsctl instance_name db2ldif userroot /var/lib/dirsrv/slapd-instance_name/ldif/example.ldif 
    OK group dirsrv exists
    OK user dirsrv exists
    ldiffile: /var/lib/dirsrv/slapd-instance_name/ldif/example.ldif
    [18/Jul/2018:10:46:03.353656777 +0200] - INFO - ldbm_instance_config_cachememsize_set - force a minimal value 512000
    [18/Jul/2018:10:46:03.383101305 +0200] - INFO - ldbm_back_ldbm2ldif - export userroot: Processed 160 entries (100%).
    [18/Jul/2018:10:46:03.391553963 +0200] - INFO - dblayer_pre_close - All database threads now stopped
    db2ldif successful
    Copy to Clipboard Toggle word wrap
  3. 启动实例:
    # dsctl instance_name start
    Copy to Clipboard Toggle word wrap

6.2.2. 使用 Web 控制台将后缀导出到 LDIF 文件

使用 Web 控制台导出后缀:
  1. 在 web 控制台中打开 Directory Server 用户界面。请参阅 第 1.4 节 “使用 Web 控制台登录目录服务器”
  2. 选择实例。
  3. 打开 Database 菜单。
  4. 选择后缀条目。
  5. Suffix Tasks,并选择 Export Suffix
  6. 输入您要在其中存储导出的 LDIF 文件的名称。目录服务器将使用指定文件名将文件存储在 /var/lib/dirsrv/slapd-instance_name/ldif/ 目录中。
  7. 单击 Export Database
您可以配置组成员具有导出数据的权限。这提高了安全性,因为您不再需要在脚本中设置 cn=Directory Manager 的凭证。另外,您可以通过修改组来轻松授予和撤销导出权限。

6.2.3.1. 启用组来导出数据

使用这个流程添加 cn=export_users,ou=groups,dc=example,dc=com 组,并启用此组的成员来创建导出任务。

流程

  1. 创建 cn=export_users,ou=groups,dc=example,dc=com 组:
    # dsidm -D "cn=Directory Manager" ldap://server.example.com -b "dc=example,dc=com" group create --cn export_users
    Copy to Clipboard Toggle word wrap
  2. 添加允许 cn=export_users,ou=groups,dc=example,dc=com 组的成员创建导出任务的访问控制指令(ACI):
    # ldapadd -D "cn=Directory Manager" -W -H ldap://server.example.com
    
    dn: cn=config
    changetype: modify
    add: aci
    aci: (target = "ldap:///cn=export,cn=tasks,cn=config")(targetattr="*")
     (version 3.0 ; acl "permission: Allow export_users
      group to export data" ; allow (add, read, search) groupdn
      = "ldap:///cn=export_users,ou=groups,dc=example,dc=com";)
    -
    add: aci
    aci: (target = "ldap:///cn=config")(targetattr =
      "objectclass || cn || nsslapd-suffix || nsslapd-ldifdir")
     (version 3.0 ; acl "permission: Allow export_users
      group to access ldifdir attribute" ; allow
      (read,search) groupdn = "ldap:///cn=export_users,ou=groups,dc=example,dc=com";)
    Copy to Clipboard Toggle word wrap
  3. 创建用户:
    1. 创建用户帐户:
      # dsidm -D "cn=Directory Manager" ldap://server.example.com -b "dc=example,dc=com" user create --uid="example" --cn="example" --uidNumber="1000" --gidNumber="1000" --homeDirectory="/home/example/" --displayName="Example User"
      Copy to Clipboard Toggle word wrap
    2. 在用户帐户中设置密码:
      # dsidm -D "cn=Directory Manager" ldap://server.example.com -b "dc=example,dc=com" account reset_password "uid=example,ou=People,dc=example,dc=com" "password"
      Copy to Clipboard Toggle word wrap
  4. uid=example,ou=People,dc=example,dc=com 用户添加到 cn=export_users,ou=groups,dc=example,dc=com 组中:
    # dsidm -D "cn=Directory Manager" ldap://server.example.com -b "dc=example,dc=com" group add_member export_users uid=example,ou=People,dc=example,dc=com
    Copy to Clipboard Toggle word wrap

验证

  • 显示 cn=config 上设置的 ACI:
    # ldapsearch -o ldif-wrap=no -LLLx -D "cn=Directory Manager" -W -H ldap://server.example.com -b cn=config aci=* aci -s base
    dn: cn=config
    aci: (target = "ldap:///cn=export,cn=tasks,cn=config")(targetattr="*")(version 3.0 ; acl "permission: Allow export_users group to export data" ; allow (add, read, search) groupdn = "ldap:///cn=export_users,ou=groups,dc=example,dc=com";)
    aci: (target = "ldap:///cn=config")(targetattr = "objectclass || cn || nsslapd-suffix || nsslapd-ldifdir")(version 3.0 ; acl "permission: Allow export_users group to access ldifdir attribute" ; allow (read,search) groupdn = "ldap:///cn=export_users,ou=groups,dc=example,dc=com";)
    ...
    Copy to Clipboard Toggle word wrap

6.2.3.2. 以普通用户身份执行导出

您可以以普通用户而不是 cn=Directory Manager 执行导出。

前提条件

  • 您启用了 cn=export_users,ou=groups,dc=example,dc=com 组的成员来导出数据。请参阅 第 6.2.3.1 节 “启用组来导出数据”
  • 您用来执行导出的用户是 cn=export_users,ou=groups,dc=example,dc=com 组的成员。

流程

  • 使用以下方法之一创建 export 任务:
    • 使用 dsconf backend export 命令:
      # dsconf -D "uid=example,ou=People,dc=example,dc=com" ldap://server.example.com backend export userRoot
      Copy to Clipboard Toggle word wrap
    • 通过手动创建任务:
      # ldapadd -D "uid=example,ou=People,dc=example,dc=com" -W -H ldap://server.example.com
      
      dn: cn=userRoot-2021_07_23_12:55_00,cn=export,cn=tasks,cn=config
      changetype: add
      objectClass: extensibleObject
      nsFilename: /var/lib/dirsrv/slapd-instance_name/ldif/None-userroot-2021_07_23_12:55_00.ldif
      nsInstance: userRoot
      cn: export-2021_07_23_12:55_00
      Copy to Clipboard Toggle word wrap

验证

  • 验证是否已创建备份:
    # ls -l /var/lib/dirsrv/slapd-instance_name/ldif/*.ldif
    total 0
    -rw-------. 1 dirsrv dirsrv 10306 Jul 23 12:55 None-userroot-2021_07_23_12_55_00.ldif
    ...
    Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat