k0lter / autopostgresqlbackup

Automated tool to make periodic backups of PostgreSQL databases
52 stars 17 forks source link

`$PGDUMP_ARGS` has an expansion-issue #25

Closed dennisse closed 1 year ago

dennisse commented 1 year ago

When running autopostgresqlbackup with the debug-flag, I noticed an extra space floating around:

 debug|Running command: pg_dump homeassistant --username postgres  --create

(Notice the extra space before --create)

After doing some digging, I think I found the problem. $PGDUMP_ARGS is set as an array here, and gets transformed to a string at line 356.

Demonstration:

$ PGDUMP_ARGS=()
$ pg_args="${PGDUMP_ARGS[*]}"
$ declare -p pg_args
declare -- pg_args="" # <- empty string
$ pg_args+=(--create)
$ declare -p pg_args
declare -a pg_args=([0]="" [1]="--create") # <- first element in array is an empty string
$ unset pg_args PGDUMP_ARGS
$ PGDUMP_ARGS=(--foo --bar)
$ pg_args="${PGDUMP_ARGS[*]}"
$ declare -p pg_args
declare -- pg_args="--foo --bar" # <- one string with both arguments
$ pg_args+=(--create)
$ declare -p pg_args
declare -a pg_args=([0]="--foo --bar" [1]="--create") # <-- first element in array is string with both arguments

The same goes for $PGDUMPALL_ARGS, which is handled in the same way.

This apparantly not a problem on Linux, where bash and pg_dump chugs along happily with this. But, on OpenBSD I get this error when $PGDUMP_OPTS is empty:

 error|pg_dump: error: too many command-line arguments (first is "")
 error|pg_dump: hint: Try "pg_dump --help" for more information.
 error|Running command 'pg_dump authelia --username root --create has failed

And this when I set more than one option in $PGDUMP_OPTS:

 error|pg_dump: unknown option -- no-comments --no-blobs
 error|pg_dump: hint: Try "pg_dump --help" for more information.
 error|Running command 'pg_dump authelia --username root --no-comments --no-blobs --create has failed

I'm not sure why this is handled differently on OpenBSD. It's the same shell (bash 5.2.15), with the same BASHOPTS and SHELLOPTS.