0

Background

I would like to use Borg Backup as a backup solution (backup on remote server). Borg offer sa very convenient sh script that they say you can just run as a cron job. While the script works flawlessly in the command line, but it's not working in cron.

I run Debian Bullseye.

How can I make it run on cron?

Here's a similar description of the problem, however, my repository is encrypted.

Data

Root's Crontab

* * * * * /usr/local/bin/scriptborg.sh >> /home/t/dwnlds/log.txt 2>&1

scriptborg.sh

#!/bin/sh

# Setting this, so the repo does not need to be given on the commandline:
export BORG_REPO=ssh://[email protected]:2222/nas/mydirectory/backup/main

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

# 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                         \
    --verbose                       \
    --filter AME                    \
    --list                          \
    --stats                         \
    --show-rc                       \
    --compression lz4               \
    --exclude-caches                \
    --exclude '/home/*/.cache/*'    \
    --exclude '/var/tmp/*'          \
    --exclude '/home/user/vids/*'         \
    --exclude '/home/user/dwnlds/*'       \
    --exclude '/home/user/.local/share/Trash/*' \
                                    \
    ::'{hostname}-{now}'            \
    /etc                            \
    /home                           \
    /root                           \
    /var                            \

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                          \
    --list                          \
    --prefix '{hostname}-'          \
    --show-rc                       \
    --keep-within  1d               \
    --keep-daily    7               \
    --keep-weekly   4               \
    --keep-monthly  6               \

prune_exit=$?

# actually free repo disk space by compacting segments

# info "Compacting repository"

# borg compact

# compact_exit=$?

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

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

exit ${global_exit}

"ls -l ~/scriptborg.sh"

-rwx------ 1 root root 2562 4. Jun 19:35 scriptborg.sh

Output from logfile

So 05 Jun 2022 12:20:01 CEST Starting backup

Remote: ssh: connect to host 192.168.1.101 port 2222: Connection timed out
Connection closed by remote host. Is borg working on the server?
terminating with error status, rc 2

So 05 Jun 2022 12:22:11 CEST Pruning repository

Remote: ssh: connect to host 192.168.1.101 port 2222: Connection timed out
Connection closed by remote host. Is borg working on the server?
terminating with error status, rc 2

So 05 Jun 2022 12:24:22 CEST Backup, Prune, and/or Compact finished with errors

Notes

  • I excluded borg compact from the script as this command is not working on Debian Bullseye

I'm very happy to get some help to resolve this issue!

12
  • What happens when cron tries to run the script? Does borg exit with non-0? Does it log anything to stdout/stderr? Commented Jun 4, 2022 at 18:16
  • Does this post help with your question?
    – harrymc
    Commented Jun 4, 2022 at 19:52
  • @harrymc: i referenced this post myself. it looks as the same problem, however it doesn't seem relevant to my problem as i encrypted my repository. Commented Jun 5, 2022 at 10:03
  • A difference between the two methods of running the script is usually that of permissions, although the crontab job should be running as root.
    – harrymc
    Commented Jun 5, 2022 at 10:09
  • @harrymc, yes, to my knowledge the job is run in the root's crontab... Commented Jun 5, 2022 at 10:21

0

You must log in to answer this question.

Browse other questions tagged .