Problem with automate backup script

Hi fellows,

I need your help with a script to backup my system and /home automatically.

On the backup-server I use publickey and top authentication with no password otp input only what works fine.

Now I am trying to automate my backup scripts in a way that they could run automatically by cronjobs 2 hourly for /home and weekly for the system-backup.

The scripts look like this `
#!/bin/sh

Load environment variables from the .env1 file

if [ -f “/home/uli/.env1” ]; then
export $(grep -v ‘^#’ /home/uli/.env1 | xargs)
else
echo “.env1 file not found!”
exit 1
fi

Define variables

RESTIC_REPOSITORY=“sftp:udapro.de:/srv/system”
EMAIL_RECIPIENT="info@ulikleemann.de"
DATE=$(date +“%Y-%m-%d”)
TIME=$(date +“%H:%M:%S”)
SUBJECT_SUCCESS=“System-Backup Successful”
SUBJECT_FAILURE=“System-Backup Failure”
SSH_KEY=“/home/uli/.ssh/id_ed25519”
OTP_SECRET=“$GOOGLE_SECRET”

Function to send email

send_email() {
local subject=“$1”
local body=“$2”
echo “$body” | mail -s “$subject” “$EMAIL_RECIPIENT”
}

Function to generate OTP

generate_otp() {
oathtool --totp --base32 “$OTP_SECRET”
}

Generate OTP

OTP=$(generate_otp)

Perform the backup

if restic -r “$RESTIC_REPOSITORY” --password-file <(echo “$RESTIC_PASSWORD”) --option ssh.command=“ssh -i $SSH_KEY -o BatchMode=yes -o StrictHostKeyChecking=no” backup /etc /lib /lib64 /usr /opt /var; then
send_email “$SUBJECT_SUCCESS” “The system backup completed successfully on $DATE at $TIME.”
else
send_email “$SUBJECT_FAILURE” “The system backup failed on $DATE at $TIME.”
exit 1
fi

```

It gives a oathtool warning oathtool: base32 decoding failed: Base32 string is invalid and I have to type in the top manually then it runs and ends with : Files: 4 new, 253 changed, 704160 unmodified
Dirs: 0 new, 133 changed, 71758 unmodified
Added to the repository: 299.977 MiB (93.794 MiB stored)

processed 704417 files, 43.223 GiB in 2:20
snapshot 72fa1090 saved
Warning: at least one source file could not be read``

`What do I have to change and how, that the script runs automatically without changing  pamd.d/sshd and /etc/ssh/sshd_config ?

Thanks in advance 

Uli
1 Like

(Hint: if you edit your post and add the three backticks before and after the script, it will be formatted so that it is easier to read. Make sure the backticks are on a line by themselves without any spaces.)

I would suggest adding this line in your generate_otp function so you can make sure the env var is what you are expecting:

echo "$OTP_SECRET"