Jump to content

Crontab - Database Backup

Recommended Posts

Hi all,


I've created a script which will take backups of my databases and compress them together in a tar.gz file.


This runs perfectly fine on the command line, but I'm unable to get it working via crontab.


Here is the script that backs up the databases (which has permissions of 755)


# 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

Again, this runs perfectly on the command line. However, the following cron does not appear to do anything.

0 3 * * * /backup-db

The 'gdrive' command comes from the following Git and is installed at /usr/local/bin/gdrive




It is worth noting that the script is located at the highest level of the server (e.g. / )


Any advice on how to get this working would be very much appreciated.

Edited by Manny
Link to post
Share on other sites
  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...