How to use Google Drive to back up and restore data

This article describes how to back up data on your A2 Hosting account to a Google Drive account, as well as how to restore data from Google Drive to your account. Using this configuration, your data is backed up securely to an off-site storage location.

If you have not already set up a Google Drive account, you must do so before following the procedures below. To set up a Google Drive account, please visit https://www.google.com/drive.

Installing and configuring the gdrive program

The gdrive program enables you to access and manipulate Google Drive files and directories from the command line.

To install and configure the gdrive program for your account, follow these steps:

  1. Log in to your account using SSH.
  2. At the command prompt, type the following commands to download and extract the gdrive program:
    cd ~
    wget https://github.com/prasmussen/gdrive/releases/download/2.1.1/gdrive_2.1.1_linux_386.tar.gz
    tar xvzf gdrive_2.1.1_linux_386.tar.gz
    
  3. To make the gdrive program accessible from any directory in your account, type the following commands:

    mkdir ~/bin
    mv ~/gdrive ~/bin
  4. To configure gdrive to access your Google Drive account, type the following command:

    gdrive about
  5. The Authentication needed message appears. Copy the URL, and then paste it into your web browser. (Log in to your Google Drive account if necessary.)
  6. Click Allow:

    Google Drive - Verification step 1

  7. Click the clipboard icon to copy the verification code:

    Google Drive - Verification step 2

  8. At the command prompt, paste the verification code that you obtained in step 7, and then press Enter. You should receive the following message (the actual names and amounts will differ):

    User: Kelly Koe, [email protected]
    Used: 181.6 MB
    Free: 32.1 GB
    Total: 32.2 GB
    Max upload size: 5.2 TB
    

    gdrive is now configured to use your Google Drive account.

  9. To list your Google Drive files and directories, type the following command:

    gdrive list

    This command displays the ID number, name, type, size, and creation date for all of the files and directories in your Google Drive account.

    • To view all of the gdrive commands, type the following command:
      gdrive help
    • The gdrive program saves its configuration settings in the /home/username/.gdrive directory, where username represents your account username. You should not normally have to edit the configuration manually, however.

Backing up data to Google Drive

After you install and configure the gdrive program, and have verified that gdrive can connect to Google Drive, you are ready to:

  • Create a directory on Google Drive to contain your backup files.
  • Create a cron job on your hosting account to do the actual backup.

To do this, follow these steps:

  1. To create a directory on Google Drive in which to store your backup files, type the following command. You can replace backups with any name you want to use:
    gdrive mkdir backups
    
  2. To verify that the directory was created successfully, type the following command:

    gdrive list

    Note the ID for the directory, as you will need this information below.

  3. After you verify that the Google Drive directory was created successfully, you are ready to set up a cron job that backs up files to the directory automatically. To do this, the cron command must run the gdrive program with the correct ID of the destination directory.

    For example, the following command demonstrates how to back up the entire public_html directory to the Google Drive backups directory daily at 2:30 AM. You should replace username with your own A2 Hosting account username, and replace ID with the ID of the backups directory:

    30 2 * * * /home/username/bin/gdrive upload --parent ID --recursive /home/username/public_html

    You can also back up individual files. The following cron job shows how to do this with a file named backup.tar.gz:

    30 2 * * * /home/username/bin/gdrive upload --parent ID /home/username/backup.tar.gz
    • You can set up the cron job using the crontab command line program, or by using cPanel (if your account includes cPanel access). For information about how to set up a cron job in cPanel, please see this article.
    • The gdrive commands shown above use the upload option, which transfers all files regardless of whether or not there are newer or older versions already stored on Google Drive. As an alternative, you may want to use the sync options to transfer files more efficiently. For more information about the sync options, see the gdrive documentation.

Restoring data from Google Drive

To restore data from Google Drive to your A2 Hosting account, follow this step:

  1. Use the gdrive download commands:
    • To restore an individual file, type the following command. Replace ID with the ID of the file you want to restore:
      gdrive download ID
    • To restore an entire directory, type the following command. Replace ID with the ID of the directory you want to restore:

      gdrive download --recursive ID

More Information

Article Details

  • Level: Intermediate

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.