Rclone script problem when run as a cron job

Also how do I find out location of rclone config file?

For the config file:

felix@gemini:~/logs$ rclone config file
Configuration file is stored at:
/data/rclone/rclone.conf

Sorry as you also need to pipe it to a file so you can see it as I neglected to mention that:

* * * * * /home/felix/scripts/test.sh >> /home/felix/logs/crontest.log 2>&1

So I added in just the word blah and you can see that is the command that caused the error.

felix@gemini:~/logs$ cat crontest.log
+ ls /etc/hosts
/etc/hosts
+ blah
/home/felix/scripts/test.sh: line 3: blah: command not found

mentioned the config file now in the script. cron still won’t work.

Can you share what’s in cron?

This is what the crontab file looks like.

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
@hourly root sh /rcloneNEW.sh > /output-crontab.log 2>&1

I am not sure what is going on here and the script works normally but not in cron. Does anyone here have experience with this situation?

Yes, share your script. I’m guessing you do not have a full path for something, which is why it isn’t working.

This is the script below for auto backup rclone backup data to microsoft azure.

#!/usr/bin/env bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
set -e
today=$(date +\%d-\%m-\%Y-\%H:\%M)
### Rclone install ###
# download and install the appropiate version of rclone for distro of choice.
# https://rclone.org/downloads/
# logfile location
logs="/rclone.log"
# name of the directory where the archive_file will be created
backupdir="/tmp"
# Container name on AZURE
container="backup2"
# name and location of sql dump file
sql_dump_file="/dump.sql"
# AZURE location
backend="azure:${container}"
# name of the archive file, change backup_ to backup_servername if using script on multiple different servers to avoid confusion and possibly overwriting of backups.
archive_file="$backupdir/backup_$(date '+%Y-%m-%d_%H%M%S').tar.gz"
# Rclone exports for connecting to AZURE
export RCLONE_CONFIG_AZURE_TYPE=azureblob
export RCLONE_CONFIG_AZURE_ACCOUNT=******
export RCLONE_CONFIG_AZURE_KEY=******

# dumps the sql database into sql_dump_file
echo $today "Creating sql dump" >> $logs
mysqldump --quick --single-transaction --all-databases > "${sql_dump_file}"
if [ -e "${sql_dump_file}" ] ; then
        echo $today "sql dump file created" >> $logs
else
        echo $today "creation of sql dump failed, exiting" >> $logs
        exit 1
fi
# archives the dirs var/www, root and the sql_dump_file into a tar.gz file (excluding .gnupg dir, 
echo $today "Creating archive file" >> $logs
tar --exclude='.gnupg' -czf "${archive_file}" var/www root "${sql_dump_file}"
if [ -e "${archive_file}" ] ; then
        echo $today "Archive file created" >> $logs
else
        echo $today "Creation of Archive file failed, cleaning and exiting" >> $logs
        rm -rf "${sql_dump_file}"
        exit 1
fi
# copies the archive file to the AZURE backup location
echo $today "Copying archive to AZURE" >> $logs
/usr/bin/rclone copy -v "${archive_file}" "${backend}" >> $logs 2>&1

        echo $today "Cleanup" >> $logs
        echo "----------------------------------------------------" >> $logs
        rm "${archive_file}" "${sql_dump_file}"
exit 0

And this is /etc/crontab. I am using CentOS 7

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
@hourly root sh /rclone.sh > /output-crontab.log 2>&1

You can try running this script locally on your side. it will work, but not as cron. cron will always fail, and leave dump.sql and .tar.gz. in the tmp folder, without uploading to azure.

Can you share the output log you are capturing?

It is coming out empty.

Are you sure it’s running? If it’s producing no output, something has to be off.

My test script looks like:

* * * * * /home/felix/scripts/test.sh >> /home/felix/logs/crontest.log 2>&1
felix@gemini:~/scripts$ cat test.sh
#!/bin/bash -x
ls /etc/hosts
blah

You can see the logs show the command isn’t in the path and errors out.

The >> keeps the log from being overwritten.

felix@gemini:~/logs$ tail crontest.log
+ blah
/home/felix/scripts/test.sh: line 3: blah: command not found
+ ls /etc/hosts
/etc/hosts
+ blah
/home/felix/scripts/test.sh: line 3: blah: command not found
+ ls /etc/hosts
/etc/hosts
+ blah
/home/felix/scripts/test.sh: line 3: blah: command not found

i’m not getting that error.

i sent you pm with server login, please see.

What’s the actual script that runs? I don’t see a rclone.sh or an entry in cron?

this one - rclone-no-encrypt6.sh

Ok. Let me add it into cron and see.

I think that did it.

In the script, I added a in /var/www and /root for the paths as that was bombing out.

  • tar --exclude=.gnupg -czf /tmp/backup_2019-01-18_165404.tar.gz /var/www /root /dump.sql

I left it at @hourly in the crontab and logged out as I’m running out for lunch.

Let me know if that seems to be doing what you wanted and I can check once I get back if needed.

The crontab file still has the job commented out with a ‘#’ but I can see it has been uploading files online. Did you use a different cron method?

Nope. It has a @ in front of it :slight_smile:

# RCLONE BACKUP SCRIPT
@hourly  /rclone-no-encrypt6.sh >> /output-crontab.log 2>&1

I have been adding cron jobs to
/etc/crontab file

what is the difference?

I see your cron job only when I execute “crontab -e” command

You need to use ‘crontab -e’ to change the crontab.

can you tell me what else you changed in the script? The name appears in green so I guess you changed it to executable as well?