namil-son namil-son - 9 days ago 4
Linux Question

uploading file to google-drive using gdrive is not working on crontab

I wrote backup script for my computer. The backup scenario is like this:

Whole directories under root are bound into tar.gz twice a day(3AM, and 12AM), and this archive is going to be uploaded to google-drive using gdrive app. every 3AM.

and here is the script

#Program: arklab backup script version 2.0
#Author: namil son
#Last modified date: 160508
#It should be executed as a super user

export LANG=en

MD=`date +%m%d`
TIME=`date +%y%m%d_%a_%H`
filename=`date +%y%m%d_%a_%H`.tar.gz
backup=`cat $backuproot/backup.conf`
gdriveID="blablabla" #This argument should be manually substituted to google-drive directory ID for each server.

#Start a new backup period at January first and June first.
if [ $MD = '0101' -o $MD = '0601' ]; then
mkdir $backuproot/`date +%y%m`
rm -rf $backuproot/`date --date '1 year ago' +%y%m`
echo $backuproot/`date +%y%m` > $backuproot/backup.conf #Save directory name for this period in backup.conf
backup=`cat $backuproot/backup.conf`
gdrive mkdir -p $gdriveID `date +%y%m` > $backup/dir
awk '{print $2}' $backup/dir > dirID
rm -f $backup/dir

#make tar ball
tar -g $backup/snapshot -czpf $backup/$filename / --exclude=/tmp/* --exclude=/mnt/* --exclude=/media/* --exclude=/proc/* --exclude=/lost+found/* --exclude=/sys/* --exclude=/local_share/backup/* --exclude=/home/* \

#upload backup file using gdrive under the path written in dirID
if [ `date +%H` = '03' ]; then

gdrive upload -p `cat $backup/dirID` $backup/$filename
gdrive upload -p `cat $backup/dirID` $backup/`date --date '15 hour ago' +%y%m%d_%a_%H`.tar.gz

Here is the problem!

When run this script on crontab, it works pretty well except for uploading tar ball to google-drive, though whole script works perfectly when run the script manually. Only the uploading process is not working when it is runned on crontab!

Can anybody help me?

Crontab entry is like this:

0 3,12 * * * sh /local_share/backup/ &>> /local_share/backup/


I had the exact same issue with minor differences. I'm using gdrive on a CentOS system. Setup was fine. As root, I set up gdrive. From the command line, 'drive list' worked fine. I used the following blog post to set up gdrive:

I wrote a PHP script to do a backup of some directories. When I ran the PHP script as root from the command line, everything worked and uploaded to Google Drive just fine.

So I threw:

1 1 * * * php /root/my_backup_script.php

Into root's crontab. The script executed fine, but the upload to Google Drive wasn't working. I did some debugging, the line:

drive upload --file /root/myfile.bz2

Just wasn't working. The only command-line return was a null string. Very confusing. I'm no unix expert, but I thought when crontab runs as a user, it runs as a user (in this case root). To test, I did the following, and this is very insecure and not recommended:

I created a file with the root password at /root/.rootpassword

chmod 500 .rootpassword

Changed the crontab line to:

1 1 * * * cat /root/.rootpassword | sudo -kS php /root/my_backup_script.php

And now it works, but this is a horrible solution, as the root password is stored in a plain text file on the system. The file is readable only by root, but it is still a very bad solution.

I don't know why (again, no unix expert) I have to have root crontab run a command as sudo to make this work. I know the issue is with the gdrive token generated during gdrive setup. When crontab runs the token is not matching and the upload fails. But when you have crontab sudo as root and run the php script, it works.

I have thought of a possible solution that doesn't require storing the root password in a text file on the system. I am tired right now and haven't tried it. I have been working on this issue for about 4 days, trying various Google Drive backup solutions... all failing. It basically goes like this:

Run the gdrive setup all within the PHP/Apache interpreter. This will (perhaps) set the gdrive token to apache. For example:

Create a PHP script at /home/public_html/gdrive_setup.php. This file needs to step through the entire gdrive and token setup.

Run the script in a browser, get gdrive and the token all set up.

Test gdrive, write a PHP script something like:

$cmd = exec("drive list"); 
echo $cmd;

Save as gdrive_test.php and run in a browser. If it outputs your google drive files, it's working.

Write up your backup script in php. Put it in a non-indexable web directory and call it something random like 2DJAj23DAJE123.php

Now whenever you pull up 2DJAj23DAJE123.php in a web browser your backup should run.

Finally, edit crontab for root and add:

1 1 * * * wget >/dev/null 2>&1

In theory this should work. No passwords are stored. The only security hole is someone else might be able to run your backup if they executed 2DJAj23DAJE123.php.

Further checks could be added, like checking the system time at the start of 2DJAj23DAJE123.php and make sure it matches the crontab run time, before executing. If the times don't match, just exit the script and do nothing.

The above is all theory and not tested. I think it should work, but I am very tired from this issue.

I hope this was helpful and not overly complicated, but Google Drive IS complicated since their switch over in authentication method earlier this year. Many of the posts/blog posts you find online will just not work.