Skip to content

Commit

Permalink
Port scripts to strict POSIX implementations (#41)
Browse files Browse the repository at this point in the history
This commit refactors scripts to pure POSIX implementations.

* Testing was done in `dash` on macOS as well as FreeBSD's `sh` shell.
* Test cases in `test/*.sh` have been significantly improved.
* This is the current recommended version of this script.
  • Loading branch information
aaronhurt authored Sep 1, 2024
1 parent 42f5bfe commit 5a9cee7
Show file tree
Hide file tree
Showing 9 changed files with 845 additions and 451 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/status-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
level: info
filter_mode: nofilter
fail_on_error: true
shfmt_flags: '-ln bash -ci -sr -i 2'
shfmt_flags: '-ci -sr -i 2'

shellcheck:
name: runner / shellcheck
Expand All @@ -76,6 +76,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: bash ./test.sh
- run: cd test && ./test.sh
env:
TMPDIR: ${{ runner.temp }}
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# zfs-replicate

A Bash script to automate ZFS Replication.
A POSIX shell script to automate ZFS Replication.

## Features

- The script follows strict POSIX standards and should be usable on any host with a POSIX compliant shell.
- Source pools and datasets are always authoritative, the script will always defer to the source.
- Supports push and pull replication with local and remote datasets.
- Supports multiple pool/dataset pairs to replicate.
Expand All @@ -12,8 +13,8 @@ A Bash script to automate ZFS Replication.
- Includes a well documented `config.sh` file that may be used as configuration or as reference for environment
variables passed to the script.
- May be run on any schedule using cron or similar mechanism.
- May be sourced and/or leveraged by/in other Bash scripts.
- Test coverage of core functions via mocks in the test.sh script.
- Fully source compliant and may be used by other scripts.
- Test coverage of core functions via mocks in the test/test.sh script.
- Includes a `--status` option for XigmaNAS that can be used to email the last log output at your preferred schedule.
Simply add it as a custom script in the email settings under "System > Advanced > Email Reports"

Expand Down Expand Up @@ -54,18 +55,18 @@ is not met.
```text
Usage: ./zfs-replicate.sh [options] [config]
Bash script to automate ZFS Replication
POSIX shell script to automate ZFS Replication
Options:
-c, --config <configFile> bash configuration file
-c, --config <configFile> configuration file
-s, --status print most recent log messages to stdout
-h, --help show this message
```

### Config File and Environment Variable Reference

```bash
#!/usr/bin/env bash
```sh
#!/usr/bin/env sh
## zfs-replicate configuration file
# shellcheck disable=SC2034

Expand Down Expand Up @@ -210,18 +211,18 @@ Options:
##
#FIND=$(which find)

## Path to the system "zfs" binary. The default uses the first "zfs"
## executable found in $PATH.
##
#ZFS=$(which zfs)

## Path to the system "ssh" binary. You may also include custom arguments
## to SSH here or in the "DEST_PIPE_WITH_HOST" option above.
## Example: SSH="ssh -l root" to login as root to target host.
## The default uses the first "ssh" executable found in $PATH.
##
#SSH=$(which ssh)

## Path to the system "zfs" binary. The default uses the first "zfs"
## executable found in $PATH.
##
#ZFS=$(which zfs)

## Set the pipe to the destination pool. But DO NOT INCLUDE the pipe (|)
## character in this setting. Filesystem names from the source will be
## sent to the destination. For increased transfer speed to remote hosts you
Expand Down
12 changes: 6 additions & 6 deletions config.sample.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
## zfs-replicate configuration file
# shellcheck disable=SC2034

Expand Down Expand Up @@ -143,18 +143,18 @@
##
#FIND=$(which find)

## Path to the system "zfs" binary. The default uses the first "zfs"
## executable found in $PATH.
##
#ZFS=$(which zfs)

## Path to the system "ssh" binary. You may also include custom arguments
## to SSH here or in the "DEST_PIPE_WITH_HOST" option above.
## Example: SSH="ssh -l root" to login as root to target host.
## The default uses the first "ssh" executable found in $PATH.
##
#SSH=$(which ssh)

## Path to the system "zfs" binary. The default uses the first "zfs"
## executable found in $PATH.
##
#ZFS=$(which zfs)

## Set the pipe to the destination pool. But DO NOT INCLUDE the pipe (|)
## character in this setting. Filesystem names from the source will be
## sent to the destination. For increased transfer speed to remote hosts you
Expand Down
138 changes: 0 additions & 138 deletions test.sh

This file was deleted.

14 changes: 14 additions & 0 deletions test/find.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env sh
## test/zfs.sh
set -eu

# check pipefail in a subshell and set if supported
# shellcheck disable=SC3040
(set -o pipefail 2> /dev/null) && set -o pipefail

_fakeFIND() {
printf "find %s\n" "$*"
return 0
}

_fakeZFS "$@"
25 changes: 25 additions & 0 deletions test/ssh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env sh
## test/zfs.sh
set -eu

# check pipefail in a subshell and set if supported
# shellcheck disable=SC3040
(set -o pipefail 2> /dev/null) && set -o pipefail

_fakeSSH() {
host=$1
shift
cmd=$1
shift
case "$cmd" in
*zfs*)
./zfs.sh "$@"
;;
*)
printf "ssh $host $cmd %s\n" "$*"
;;
esac
return 0
}

_fakeSSH "$@"
Loading

0 comments on commit 5a9cee7

Please sign in to comment.