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 comment
Share on other sites

  • 1 year later...

Thanks for the information your article brings. I see the novelty of your writing, I will share it for everyone to read together. I look forward to reading many articles from you.

Edited by Ingolme
Removed advertising link
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...