0

I have created a simple backup script that creates .tar.gz of all my important folders and upload it to Google Drive using rclone.

Everything works perfectly fine when I run the script manually. But it seems to be failing when anacron runs it.

I use anacron -fnd to run anacron right away to test if everything is running correctly.

Following are the error I get in my log file
2022/11/28 23:58:03 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults
2022/11/28 23:58:03 Failed to create file system for "linuxbackuprclone:Linux-Backup": didn't find section in config file

linuxbackuprclone is the name of my remote in rclone.
Linux-Backup is the name of the folder in Google Drive.

Here is my backup script, it's called backup, located at /home/paramv/Bash-Script, and is named backup

#!/bin/bash

set -e

backupDirPath="/home/paramv/Linux-Backup" 

if [ ! -d $backupDirPath ]; then
    mkdir $backupDirPath
fi

if [ ! -d "/home/paramv/Bash-Script/Backup-Log" ]; then
    mkdir Backup-Log    
fi

# Create a log file named after the date the backup was created
logFile="/home/paramv/Bash-Script/Backup-Log/$(date).log"
logFile=${logFile// /_}
touch $logFile

# Create tar of dirs to backup
echo "Creating backup.tar.gz" >> $logFile

# sudo tar -zcf /home/paramv/Linux-Backup/backup.tar.gz -P /home/paramv/Documents/ &>> $logFile

if [ $? != 0 ]; then
    echo "Failed to create backup.tar.gz. Error code = $?" &>> $logFile
    exit 1
else
    echo "Completed creating backup.tar.gz" >> $logFile
fi 


# Upload local backup file to the cloud
echo "Uploading backup.tar.gz to Google Drive" >> $logFile

/usr/bin/rclone copy --update --transfers 30 --checkers 8 --contimeout 60s --timeout 300s --retries 3 --low-level-retries 10 "/home/paramv/Linux-Backup/backup.tar.gz" "linuxbackuprclone:Linux-Backup" &>> $logFile

if [ $? != 0 ]; then
    echo "Failed to upload backup.tar.gz to Google Drive. Error code = $?" &>> $logFile
    exit 1
else
    echo "Uploaded backup.tar.gz to Google Drive" >> $logFile
fi 

exit

Following is my anacron script located at /etc/cron.weekly, and is named prsnlbackup

#!/bin/sh

set -e

exec /home/paramv/Bash-Script/backup

exit

Following is the output of running rclone config.

Current remotes:

Name                 Type
====                 ====
linuxbackuprclone    drive

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> 

This is what my rclone.conf looks like. It is located at /home/paramv/.config/rclone

[linuxbackuprclone]
type = drive
scope = drive
token = {"omitted to make it readable"}
team_drive = 

I am running Ubuntu 22.04.1 LTS.
Any help will be highly appreciated.

pvs
  • 1
  • 2
  • On the 10th line of the script you have `mkdir Backup-Log` but it looks like that needs to be `mkdir "$backupDirPath/Backup-Log"` or your script needs ensure its working directory is in the right place with `cd "$backupDirPath"` before creating the `Backup-Log` directory. Probably just a typo in this question rather than in your script, but something to check. – Sotto Voce Nov 28 '22 at 21:26
  • The first error message says the job expects a config file at file `/root/.config/rclone/rclone.conf` but yours is in your users home directory. As a first step I would copy your `clone.conf` file to the corresponding location under `/root` and troubleshoot from there – PonJar Nov 29 '22 at 10:01
  • @PonJar, thanks it worked! – pvs Nov 29 '22 at 12:10

2 Answers2

0

I suspect you're running the script by hand as yourself, but when you put the script into /etc/cron.weekly/ you're setting it up to be run as root.

If you want to run the script as yourself then simply add it to your own crontab. Don't use su or sudo.

crontab -e    # Edit my own crontab
...

It's the standard six fields for min(0-59), hour(0-23), day(1-31), month(1-12), dow(0-6,0=Sun). Pick one of the Day Of Week values to ensure that it runs only weekly. For example, this will run at 2am every Sunday, writing its output to the log file backup.log in your home directory:

0 2 * * 0    Bash-Script/backup > backup.log 2>&1
roaima
  • 107,089
  • 14
  • 139
  • 261
0

The solution is to run sudo -i then copy rclone.conf from /home/paramv/.config/rclone to /root/.config/rclone

AdminBee
  • 21,637
  • 21
  • 47
  • 71
pvs
  • 1
  • 2