# Using Google Drive to back up and restore data

> Set up Google Drive as an offsite backup destination for your hosting account and restore files from it.

Source: https://helpcenter.ruachost.com/cpanel/files/using-google-drive-to-backup-and-restore-data-in-ruachost/

Ruachost allows you to configure **Google Drive** as an offsite backup destination for your hosting account. This ensures your website files and databases are securely stored in the cloud and can be restored if needed.

## Why Use Google Drive for Backups?

- **Offsite protection**: Keeps your data safe even if your hosting server is compromised.
- **Easy access**: Manage and download backups directly from your Google Drive account.
- **Automation**: Schedule backups to run automatically via cron jobs.
- **Scalability**: Use your existing Google Drive storage without additional infrastructure.

## Steps to Configure Google Drive Backups

### Step 1: Create Google API Credentials

1. Log in to the Google Cloud Console.
2. Create a new project (e.g. ruachost-backup).
3. Enable the **Google Drive API**.
4. Configure the **OAuth consent screen**.
5. Create **OAuth client credentials** (Desktop app).
6. Copy the **Client ID** and **Client Secret** (you’ll need these later.)

### Step 2: Install the gdrive Utility

The `gdrive` command-line tool is used to interact with Google Drive.

- **On your local computer**:

  - Download the latest release from GitHub (gdrive).
  - Extract the file and verify installation by running:

    ```bash
    ./gdrive version
    ```
- **On your Ruachost hosting account**:

  - Upload the `gdrive` binary to your account (e.g., into `~/bin/`).
  - Make it executable:

    ```bash
    chmod +x ~/bin/gdrive
    ```

### Step 3: Authenticate Google Drive

1. Run:

   ```bash
   gdrive account add
   ```
2. Enter your **Client ID** and **Client Secret**.
3. Open the provided URL in your browser, log in to Google, and approve access.
4. Export the configuration and upload it to your Ruachost account if needed.

### Step 4: Create a Backup Directory in Google Drive

```bash
gdrive files mkdir backups
```

- Note the **directory ID** returned, you’ll use this in your cron jobs.

### Step 5: Automate Backups with Cron

Set up a cron job in cPanel or via SSH to back up your site automatically.

Example: Back up your `public_html` folder daily at 2:30 AM:

```bash
30 2 * * * /home/username/bin/gdrive files upload --parent DIRECTORY_ID --recursive /home/username/public_html
```

Example: Back up a single archive file:

```bash
30 2 * * * /home/username/bin/gdrive files upload --parent DIRECTORY_ID /home/username/backup.tar.gz
```

### Step 6: Restoring Data from Google Drive

- To restore a single file:

  ```
  gdrive files download FILE_ID
  ```
- To restore an entire directory:

```bash
gdrive files download --recursive DIRECTORY_ID
```

##
