Steven SullivanSteven Sullivan - 24th April 2018

I recently joined RunCloud the other day and didn’t like their backup function, so I decided to create my own Backup Script that puts the files directly on Google Drive!

Install Google Drive to your Linux Server

First, you’ll need to install Google Drive to your Linux machine. This simply installs Drive – not the sync functions, etc.

wget -O drive 
mv drive /usr/sbin/drive 
chmod 755 /usr/sbin/drive

Next, you’ll need to start drive and then follow the instructions. Once you’ve done that and linked Google Drive to your linux server, we’re ready!


Install the Backup Script

Ok, so the script below basically backups all the web applications and databases into their own .tar.gz file. I run this below script once a day during the morning hours.


DATE=`date +%m-%d-%Y`
for dir in /home/*/webapps/*
  base=$(basename "$dir")
  tar -czf "/tmp/${base}-${DATE}.tar.gz" -C "$dir" .
  drive upload -p '<GOOGLE_DRIVE_FOLDER_ID>' -f /tmp/${base}-${DATE}.tar.gz
  rm -rf /tmp/${base}-${DATE}.tar.gz

databases=`mysql -u $USER -p$PASSWORD --batch --skip-column-names -e "SHOW DATABASES;" | grep -E -v "(information|performance)_schema"`

for db in $databases
  mysqldump -u $USER -p$PASSWORD --databases $db > /tmp/$db-`date +%m-%d-%Y`.sql
  tar -czf /tmp/$db-`date +%m-%d-%Y`.sql.tar.gz -C /tmp/ $db-`date +%m-%d-%Y`.sql
  drive upload -p '<GOOGLE_DRIVE_FOLDER_ID>' -f /tmp/$db-`date +%m-%d-%Y`.sql.tar.gz
  rm -rf /tmp/$db-`date +%m-%d-%Y`.sql
  rm -rf /tmp/$db-`date +%m-%d-%Y`.sql.tar.gz

How to make the backup script work

  1. Replace <GOOGLE_DRIVE_FOLDER_ID> with the Google Drive folder ID. This usually looks like random characters. To get it, just double click the backup folder and look in the address bar.
  2. Once you have set-up a server with RunCloud it provides you with the MySQL root password. Replace <MYSQL_ROOT_PASSWORD> with that password.

That’s it! You can now run the script to make sure it sends the files directly to your selected Google Drive folder.


Delete the files after X days

If you’re looking to delete files from google drive after X amount of days, then look no further because I created that script too.