Gdrive: Crontab - Upload to Google Drive

Created on 22 May 2016  路  5Comments  路  Source: prasmussen/gdrive

I noticed previous dialogue regarding crontabs but couldn't find a solution to my problem.

I have created a script that will take backups of MySQL databases and upload a compressed tar.gz file to Google Drive. This works fine on the command line, but the upload doesn't happen when called via crontab. It is worth noting here that the cron does create a file, so is set up correctly.

Here's the code (feel free to use it yourselves):
`
#!/bin/sh
# Create a backup of all MySQL databases

# Create a temporary directory to store the MySQL databases
mkdir /$( date '+%Y-%m-%d' )

# Enter the temporary directory created above
cd /$( date '+%Y-%m-%d' )

# Create a dump of each database
mysql -u user -ppass -e 'show databases' | while read dbname; do mysqldump -u user -ppass -h localhost "$dbname" > "$dbname".sql; done

# Create a tar.gz archive, consisting of all database backups
tar -zcf /$( date '+%Y-%m-%d' ).tar.gz /$( date '+%Y-%m-%d' )

# Transfer the tar.gz archive to Google Drive
gdrive upload --parent XXXXX /$( date '+%Y-%m-%d' ).tar.gz

# Remove the temporary directory created at the top of the script
rm -rf /$( date '+%Y-%m-%d' )

# Finally, remove the tar.gz archive from the server
rm -rf /$( date '+%Y-%m-%d' ).tar.gz
`

Somewhere, somehow, I get the feeling the cron doesn't recognise the gdrive command. Everything else works flawlessly, so what would people recommend in order for the upload to work automatically.

Most helpful comment

Awesome. A quick response and one that worked first time.

The top of my script now reads as follows:

#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Thank you.

All 5 comments

gdrive is probably not in path.
You can set the path manually at the top of your crontab by copying the output of echo $PATH. Mine looks like this:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

or you could use the full path to the gdrive binary in your script.

Awesome. A quick response and one that worked first time.

The top of my script now reads as follows:

#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Thank you.

i have the same issue i try to insert
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
but still crontab didn't execute the gdrive command in bash
any one have solution?

@abdo0 you can found the soulution in here

I've been with this same problem. Do you test all commands manually without in crontab? I imagine that you install gdrive with linuxbrew. The good way to install it's downloading the executable, change the name to "gdrive" and copying it to /usr/bin.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uzvermode picture uzvermode  路  3Comments

dominion66 picture dominion66  路  4Comments

aservet1 picture aservet1  路  5Comments

4getit picture 4getit  路  4Comments

devova picture devova  路  7Comments