can't automatize backups via cronjob

kimawari

Cadet
Joined
Mar 7, 2023
Messages
3
I'm trying to use the following script as a cronjob for the root user of my TRUENAS SCALE setup:
#!/bin/bash
# my own rsync-based snapshot-style backup procedure
## (cc) marcio rps AT gmail.com
remote_host="192.168.XXX.YYY"
remote_username="my_user"
# Function to check if the remote host is up
function is_remote_host_up() {
if ping -c 5 -W 5 "$remote_host" > /dev/null; then
return 0 # Remote host is up
else
return 1 # Remote host is down
fi
}
# config vars
SRC="/home/$remote_username/" # dont forget trailing slash!
SNAP="/mnt/Storage/Backups/home" # this directory must exists
OPTS="-a --delay-updates --delete"
MINCHANGES=20
# run this process with real low priority
ionice -c 3 -p $$
renice +12 -p $$
# Check if the remote host is up
if is_remote_host_up; then
echo "$(date '+%Y-%m-%d %H:%M') - Remote host is up. Starting backup." >> $SNAP/rsync.log
rsync $OPTS $remote_username@$remote_host:$SRC $SNAP/latest >> $SNAP/rsync.log
# check if enough has changed and if so
# make a hardlinked copy named as the date
COUNT=$( wc -l $SNAP/rsync.log|cut -d" " -f1 )
if [ "$COUNT" -gt "$MINCHANGES" ] ; then
DATETAG=$(date +%Y-%m-%d)
if [ ! -e $SNAP/$DATETAG ] ; then
cp -al $SNAP/latest $SNAP/$DATETAG
chmod u+w $SNAP/$DATETAG
mv $SNAP/rsync.log $SNAP/$DATETAG
chmod u-w $SNAP/$DATETAG
fi
fi
echo "$(date '+%Y-%m-%d %H:%M') - Backup completed." >> $SNAP/rsync.log
else
echo "$(date '+%Y-%m-%d %H:%M') - Remote host is down. Backup not performed." >> $SNAP/rsync.log
fi

The problem I'm facing is that the the host I want to backup seems to be always down when running the script.

In the host, I'm mounting the TRUENAS storage via NFS and that's maybe the problem, that the disks in the TRUENAS stop spinning at some poing, as it takes a bit to wake the storage up when accessing it after sometime.

I can launch the script manually and it works as intended, but I'd like to automatize the process.

Cheers,
David
 
Top