Add an OpenLDAP database hook for dumping and restoring OpenLDAP databases with slapcat and slapadd (#1137). #1346

Merged
witten merged 4 commits from ebuzerdrmz44/borgmatic:openldap-hook into main 2026-09-07 23:59:08 +00:00
Contributor

Description

Adds an openldap_databases: hook. Dumps with slapcat, restores with slapadd,
streamed through a named pipe like the other database hooks.

  • name is an LDAP suffix, passed to -b.
  • No all. slapcat without -b dumps only the first database, so the schema
    rejects all and you get a validation error instead of a backup that fails
    partway through.
  • cn=config is listed like any other suffix and isn't special-cased. The docs
    say to restore it first, in its own run.
  • Local only. Stopping slapd, emptying the database directory and fixing file
    ownership are left to commands: with when: [restore].

The command parts don't go through shlex.quote, unlike the other hooks. There's
no shell involved (slapcat writes to the pipe with -l, slapadd reads stdin), and
quoting would break suffixes containing spaces such as
o=Example Corp,dc=example,dc=com. Two tests cover this.

Closes #1137

### Description Adds an `openldap_databases:` hook. Dumps with slapcat, restores with slapadd, streamed through a named pipe like the other database hooks. - `name` is an LDAP suffix, passed to `-b`. - No `all`. slapcat without `-b` dumps only the first database, so the schema rejects `all` and you get a validation error instead of a backup that fails partway through. - `cn=config` is listed like any other suffix and isn't special-cased. The docs say to restore it first, in its own run. - Local only. Stopping slapd, emptying the database directory and fixing file ownership are left to `commands:` with `when: [restore]`. The command parts don't go through `shlex.quote`, unlike the other hooks. There's no shell involved (slapcat writes to the pipe with `-l`, slapadd reads stdin), and quoting would break suffixes containing spaces such as `o=Example Corp,dc=example,dc=com`. Two tests cover this. Closes #1137
Add an OpenLDAP database hook for dumping and restoring OpenLDAP databases with slapcat and slapadd (#1137).
Some checks failed
build / test (pull_request) Has been cancelled
build / docs (pull_request) Has been cancelled
b8f1ff23b8
Update NEWS
All checks were successful
build / test (pull_request) Successful in 7m55s
build / docs (pull_request) Has been skipped
d46a664955
witten left a comment

Partial review. This is looking really good! I still need to get to docs and tests.

Partial review. This is looking really good! I still need to get to docs and tests.
@ -1,4 +1,7 @@
2.1.8.dev0
* #1346: Add an OpenLDAP database hook for dumping and restoring OpenLDAP databases with slapcat
and slapadd. See the documentation for more information:
https://torsion.org/borgmatic/reference/configuration/data-sources/openldap/
Owner

Thanks for adding this to the changelog!

Thanks for adding this to the changelog!
witten marked this conversation as resolved
@ -2397,0 +2403,4 @@
properties:
name:
type: string
not: {enum: ['all']}
Owner

Interesting. Does this give a clear enough error message to the user? I'm just wondering if borgmatic giving a more hand-crafted warning/error might be clearer. I assume you've seen borgmatic/hooks/data_source/sqlite.py:66. Maybe the difference with LDAP is that all isn't a legitimate database name, whereas it would be for SQLite...?

I do not feel strongly here.

Interesting. Does this give a clear enough error message to the user? I'm just wondering if borgmatic giving a more hand-crafted warning/error might be clearer. I assume you've seen `borgmatic/hooks/data_source/sqlite.py:66`. Maybe the difference with LDAP is that `all` isn't a legitimate database name, whereas it would be for SQLite...? I do not feel strongly here.
Author
Contributor

I guess jsonschema can't attach a custom message to not, so a better one means checking it in apply_logical_validation(), which puts LDAP knowledge in config/validate.py. Your call. Happy to write it, or leave the schema as is.

I guess jsonschema can't attach a custom message to not, so a better one means checking it in apply_logical_validation(), which puts LDAP knowledge in config/validate.py. Your call. Happy to write it, or leave the schema as is.
Owner

Probably fine as is. You've documented it, so hopefully this schema restriction is just a backstop.

Probably fine as is. You've documented it, so hopefully this schema restriction is just a backstop.
witten marked this conversation as resolved
@ -0,0 +49,4 @@
database['name'],
'-l',
dump_filename,
)
Owner

Might be nice to include -d whatever to increase the log level when the borgmatic/Python log level is high enough. See borgmatic/borg/repo_create.py:111-112 for some examples. Could also be applied to slapadd usage.

Do not feel strongly.

Might be nice to include `-d whatever` to increase the log level when the borgmatic/Python log level is high enough. See `borgmatic/borg/repo_create.py:111-112` for some examples. Could also be applied to `slapadd` usage. Do not feel strongly.
Author
Contributor

Added, modeled on repo_create.py. There's a make_debug_flags() helper now, and both command builders splat it in:

  • INFO: -d 256, which is slapd's "stats" level
  • DEBUG: -d 65535, every bit set
  • anything quieter: no flag at all

I also pinned the log level in the existing command tests, so they can't pick up debug flags by surprise.

Added, modeled on repo_create.py. There's a make_debug_flags() helper now, and both command builders splat it in: - INFO: -d 256, which is slapd's "stats" level - DEBUG: -d 65535, every bit set - anything quieter: no flag at all I also pinned the log level in the existing command tests, so they can't pick up debug flags by surprise.
Owner

Awesome, thanks!

Awesome, thanks!
witten marked this conversation as resolved
@ -0,0 +174,4 @@
port,
container,
label,
),
Owner

You don't need this last dump.make_data_source_dump_filename(). The borgmatic_source_directory pattern is for compatibility with archives containing old dumps made before borgmatic's switch to the borgmatic_runtime_directory—so there will be exactly zero old OpenLDAP dumps made before that point because the OpenLDAP hook is brand new!

You don't need this last `dump.make_data_source_dump_filename()`. The `borgmatic_source_directory` pattern is for compatibility with archives containing old dumps made before borgmatic's switch to the `borgmatic_runtime_directory`—so there will be exactly zero old OpenLDAP dumps made before that point because the OpenLDAP hook is brand new!
Author
Contributor

right , removed it .

right , removed it .
witten marked this conversation as resolved
code review fixes for the openldap database hook
All checks were successful
build / test (pull_request) Successful in 5m20s
build / docs (pull_request) Has been skipped
b2d97c27ef
witten left a comment

Great docs and tests! A few minor suggestions, and then I think this is ready to merge.

Great docs and tests! A few minor suggestions, and then I think this is ready to merge.
@ -113,2 +113,3 @@
The SQLite hook in particular does not consider "all" a special database name.
The SQLite hook in particular does not consider "all" a special database name,
and the OpenLDAP hook doesn't accept it at all.
Owner

You could also add OpenLDAP configuration to some of the examples in this file if you want. (See above.)

Do not feel strongly.

You could also add OpenLDAP configuration to some of the examples in this file if you want. (See above.) Do not feel strongly.
Author
Contributor

I added it to the involved example, right after sqlite_databases , that hook is local-only too, so a local hook in that block has precedent. Used cn=config plus a data suffix, since that's the pairing most people will actually want. Left it out of the "all databases" example for obvious reasons.

I added it to the involved example, right after sqlite_databases , that hook is local-only too, so a local hook in that block has precedent. Used cn=config plus a data suffix, since that's the pairing most people will actually want. Left it out of the "all databases" example for obvious reasons.
Owner

Great, thanks!

Great, thanks!
witten marked this conversation as resolved
@ -0,0 +50,4 @@
- name: dc=example,dc=com
```
`cn=config` gets dumped and restored just like any other suffix. When restoring,
Owner

Is there also an LDAP config file that should be backed up (separately, say in source_directories)? Or does cn=config cover that?

Is there also an LDAP config file that should be backed up (separately, say in `source_directories`)? Or does `cn=config` cover that?
Author
Contributor

Checked the manual for this (slapd-config(5)): the root is hardcoded to cn=config with cn=Schema under it, so schema is in the dump . verified here, it comes back as cn=schema,cn=config. What's not in it is files the config only points at, like olcTLSCertificateFile, which is just a path. And with a legacy slapd.conf there's no config database at all: slapcat -f slapd.conf -b cn=config exits 1 with "could not open database". Both cases go in source_directories, I documented it on the reference page.

Checked the manual for this ([slapd-config(5)](https://www.openldap.org/software/man.cgi?query=slapd-config)): the root is hardcoded to cn=config with cn=Schema under it, so schema is in the dump . verified here, it comes back as cn=schema,cn=config. What's not in it is files the config only points at, like olcTLSCertificateFile, which is just a path. And with a legacy slapd.conf there's no config database at all: slapcat -f slapd.conf -b cn=config exits 1 with "could not open database". Both cases go in source_directories, I documented it on the reference page.
Owner

Awesome!

Awesome!
witten marked this conversation as resolved
@ -0,0 +84,4 @@
def test_dump_data_sources_logs_and_skips_if_dump_already_exists():
databases = [{'name': 'dc=example,dc=com'}]
flexmock(module).should_receive('make_dump_path').and_return('/run/borgmatic')
Owner

I recommend also mocking out build_dump_command() in these tests, so that you're not reliant on the behavior of a second unit (and subject to changes in it).

I recommend also mocking out `build_dump_command()` in these tests, so that you're not reliant on the behavior of a second unit (and subject to changes in it).
Author
Contributor

done. Three of them stub it out; in the skip test it's a .never(), since that path returns before the builder is reached.

done. Three of them stub it out; in the skip test it's a .never(), since that path returns before the builder is reached.
witten marked this conversation as resolved
@ -0,0 +298,4 @@
insert_logging_mock(logging.WARNING)
flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
Owner

I recommend also mocking build_restore_command() in these test functions.

I recommend also mocking `build_restore_command()` in these test functions.
Author
Contributor

applied

applied
witten marked this conversation as resolved
@ -0,0 +269,4 @@
)
def test_build_restore_command_with_info_log_level_includes_stats_debug_flag():
Owner

Normally I would recommend testing the make_debug_flags() function separately (with its own unit tests) and then mocking it out in tests of any units that call it (like build_restore_command()).

Do not feel strongly, as it is a pretty minor function. But it would allow you to cut out a couple tests at the cost of some additional mocking.

Normally I would recommend testing the `make_debug_flags()` function separately (with its own unit tests) and then mocking it out in tests of any units that call it (like `build_restore_command()`). Do not feel strongly, as it is a pretty minor function. But it would allow you to cut out a couple tests at the cost of some additional mocking.
Author
Contributor

done. it has its own three tests now and is mocked in the build_* tests. I kept one test per builder with the mock returning ('-d', '256'), just so the splat into the command stays pinned.

done. it has its own three tests now and is mocked in the `build_* `tests. I kept one test per builder with the mock returning `('-d', '256')`, just so the splat into the command stays pinned.
witten marked this conversation as resolved
more code review fixes for the openldap database hook
All checks were successful
build / test (pull_request) Successful in 4m45s
build / docs (pull_request) Has been skipped
2324556229
witten merged commit e5619291cc into main 2026-09-07 23:59:08 +00:00
Owner

Thanks for all your hard work here. Merged!

Thanks for all your hard work here. Merged!
witten referenced this pull request from a commit 2026-09-08 15:39:58 +00:00
witten referenced this pull request from a commit 2026-09-08 15:57:02 +00:00
Owner

Released in borgmatic 2.1.8!

Released in borgmatic 2.1.8!
Sign in to join this conversation.
No reviewers
No milestone
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
borgmatic-collective/borgmatic!1346
No description provided.