Skip to content

Instantly share code, notes, and snippets.

@gustavonecore
Last active November 9, 2018 11:16
Show Gist options
  • Save gustavonecore/d09d1393838add3bfd625730f59c5a9d to your computer and use it in GitHub Desktop.
Save gustavonecore/d09d1393838add3bfd625730f59c5a9d to your computer and use it in GitHub Desktop.
Mysql backup to Google Drive
1.- First of all, install the gdrive binary
https://github.com/prasmussen/gdrive
2.- Execute gdrive list
This will try to run the command, but since you don't have any credential, will prompt in the console an url to login. Go to that url in your browser, confirm the process and copy the given token.
3.- Paste the token in the command line
4.- Add new cron jobs as you need. Replace the {database} and {folderId} with your database name and gdrive folder id, like:
0 3 * * * /path/to/your/mysql-gdrive.sh {database} {folderId}
e.g:
0 1 * * * /opt/mysql-gdrive.sh my_db1 AXC_324-exLcbidvXLxivqy0pIZg7oR
0 2 * * * /opt/mysql-gdrive.sh my_db2 XCVSD-2hHCYi2Bki1mej-lScYmF2enr
5.- Add the bash mysql-gdrive.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
user="{DB_USER}"
password="{DB_PASSWORD}"
dbname=$1
folderId=$2
date=$(date +"%Y-%m-%d")
dumpFile="/home/$dbname-$date.sql"
mysqldump -u$user -p$password $dbname > $dumpFile;
gdrive upload --parent $folderId $dumpFile > /opt/upload.log;
rm -f $dumpFile;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment