Backup Script to Google Drive (RunCloud)

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 "${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.

12 thoughts...

  1. lance says:

    The sign in with this app has been temporarily disabled. Any help?

    1. Hi Lance,

      Unfortunately not. I’ve had to switch to rclone – which seems better in my opinion.

      I followed a tutorial (link below), with the exception of the “use auto config” part – select No and it will provide you a Google authentication link which you can copy/paste.

      I will be updating this post soon with more details.

  2. Paul says:

    Which command for cronta -e do you use? I put into root directory but when i set into crontab -e
    * 4 * * * /bin/bash / I give error

  3. Paul Nita says:

    which command do you use in contab -e? I have error :((

  4. 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.

  5. When i try to run the script from root using “./” it says the following:
    -bash: ./ /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 or / (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.

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

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

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

  7. King In The Castle says:

    Do you know this: ?

  8. 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 *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>