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 https://drive.google.com/uc?id=0B3X9GlR6EmbnMHBMVWtKaEZXdDg 
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!

drive

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.

#!/bin/bash

DATE=`date +%m-%d-%Y`
for dir in /home/*/webapps/*
do
  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
done


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

for db in $databases
do
  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
done

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.

8 thoughts...

  1. Thank you all working. Could you tell me how to have different folders for each websites files and database as I am currently backing up 12 websites and having 7 days worth of data in 1 folder is overwhelming.
    Many thanks

    1. Hi Philip,

      Unfortunately it will require re-coding the script. Google Drive runs using folder ID’s, not by “free balling” and creating folders as-and-when. Because of this, you can only upload to one folder. In order to upload to multiple folders you’d have to rewrite the script so that it checks a username and/or name, then match that to a Google folder ID and then use that ID for the upload. More effort than it’s worth.

  2. When i try to run the script from root using “./googlebackup.sh” it says the following:
    -bash: ./googlebackup.sh: /bin/bash^M: bad interpreter: No such file or directory
    Iv tried running it using the cron in runcloud but no backups? Also in the “command” line under add new cron job in runcloud do i just put googlebackup.sh or /googlebackup.sh (file is in root directory)

    1. This means you’re using Windows and incorrectly coding the document. If you’re using NotePad++ in the lower right corner, double click Windows (CR LF) and then click on Unix (LF), then save the document again.

      https://uploads.disquscdn.com/images/d58bb6e28017e53dd73cc4200538a0e7886f05c2d0fe99a59fd02f969c5fc2bb.jpg

      If you’re not using Notepad++ then you’ll have to search for the error and use other techniques.

  3. what directory do you put the backup script into and what do you name the script?

    1. I just named it GoogleBackup.sh and placed it in the /root directory. You can name it whatever you like and also place it wherever you like.

  4. King In The Castle says:

    Do you know this: https://rclone.org/drive/ ?

  5. King In The Castle says:

    🙂 WOW! I will try it. Thanks Steven.

Leave a Reply

Your email address will not be published. Required fields are marked *