systemd 不读取脚本所需的环境变量

systemd 不读取脚本所需的环境变量

我想使用borg备份和systemd计时器来进行备份。我正在使用 borg 备份网站上的脚本,稍作调整。你可以看看原图这里和我的脚本如下:

#!/bin/sh

# Setting this, so the repo does not need to be given on the commandline:
export [email protected]:/path/to/repo

# See the section "Passphrase notes" for more infos.
export BORG_PASSPHRASE=SecretPassphrase

# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM

info "Starting backup"

# Backup the most important directories into an archive named after
# the machine this script is currently running on:

borg create                            \
    --remote-path=/usr/local/bin/borg  \
    --verbose                          \
    --filter AME                       \
    --list                             \
    --stats                            \
    --show-rc                          \
    --compression lz4                  \
    --exclude-caches                   \
                                       \
    ::'{hostname}-{now}'               \
    ~/.ssh                             \
    ~/db/dump_*                        \

backup_exit=$?

info "Pruning repository"

# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
# archives of THIS machine. The '{hostname}-' prefix is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:

borg prune                             \
    --remote-path=/usr/local/bin/borg  \
    --list                             \
    --prefix '{hostname}-'             \
    --show-rc                          \
    --keep-daily    7                  \
    --keep-weekly   4                  \
    --keep-monthly  6                  \

prune_exit=$?

# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))

if [ ${global_exit} -eq 0 ]; then
    info "Backup and Prune finished successfully"
elif [ ${global_exit} -eq 1 ]; then
    info "Backup and/or Prune finished with warnings"
else
    info "Backup and/or Prune finished with errors"
fi

exit ${global_exit}

ATM 我只是想设置一项服务,没有计时器。

n.service在 中创建了(工作名称)/etc/systemd/system,内容如下:

[Unit]
Description=Example Service

[Service]
Type=simple
User=callmebob
Group=callmebob
Environment="[email protected]:/path/to/repo"
Environment="BORG_PASSPHRASE=SecretPassphrase"
ExecStart=/bin/sh /home/callmebob/backup.sh

[Install]
WantedBy=multi-user.targetshell

现在从控制台:

sudo systemctl daemon-reload
sudo systemctl start n
sudo systemctl status n

服务似乎运行正常,但它永远无法读取环境变量:

May 18 05:45:31 mydomain systemd[1]: Started Example Service.
May 18 05:45:31 mydomain sh[213874]: Wed 18 May 05:45:31 UTC 2022 Starting backup
May 18 05:45:38 mydomain sh[213903]: passphrase supplied in BORG_PASSPHRASE, by BORG_PASSCOMMAND or via BORG_PASSPHRASE_FD is incorrect.
May 18 05:45:38 mydomain sh[213903]: terminating with error status, rc 2

我尝试使用EnvironmentFile,在其中放置键/值对,没有引号等,但也不起作用。使用PassEnvironment也没有任何作用。我也尝试了不同的服务类型,但没有成功。

知道我在这里缺少什么吗?

相关内容