Automating Daily Database Backups to Google Drive Free of Cost

Share

In today’s data-driven world, ensuring the safety and availability of your database is crucial. One way to safeguard your data is by creating regular backups. In this article, we will walk you through the process of automating daily database backups to Google Drive, using a combination of bash and Python scripts. This method is not only effective but also cost-efficient as it leverages a free Google Cloud service account.

Automating Daily Database Backups to Google Drive: Step-by-Step Guide

Prerequisites

Before we dive into the details, here’s what you’ll need to get started:

  1. Google Cloud Service Account: Create a service account on the Google Cloud Console. This account will be used to interact with Google Drive. Make sure to download the JSON key file for this service account.
  2. Google Drive Account: Access to a Google Drive account where you want to store your database backups. You will create a dedicated folder for these backups.
  3. MySQL Database: Ensure you have a MySQL database that you want to back up.
  4. Python: Python should be installed on your system to run the Python script.

Setting Up Automated Backups

Let’s break down the process into manageable steps:

1. Create a Google Drive Folder

The first step is to log in to your Google Drive account and create a new folder for your database backups. You can name it “DatabaseBackups” or choose a name that suits your preference.

2. Share the Folder with the Service Account

To enable your service account to access this folder, follow these steps:

  1. Right-click on the “DatabaseBackups” folder and select “Share.”
  2. In the sharing dialog, enter the email address associated with your Google Cloud service account.
  3. Set the service account’s access level to “Editor” or “Viewer,” depending on your preference.
  4. Click “Send” to send an invitation to the service account’s email address.

3. Clone or Download the Scripts

Next, clone the repository containing the necessary scripts or download them individually:

  • backup_script.sh (bash script)
  • upload_to_drive.py (Python script)

Github: https://github.com/Gurinder-Batth/auto-database-backup-google-drive

4. Configure the Scripts

Open backup_script.sh and set the MySQL database credentials (username and password) and adjust the backup file name and location as needed:

DB_USER="your_db_username" DB_PASS="your_db_password" BACKUP_DIR="/path/to/backup/directory"

Then, open upload_to_drive.py and configure the following variables:

SERVICE_ACCOUNT_KEY_FILE = 'path/to/your/service-account-key.json' DRIVE_FOLDER_ID = 'your-drive-folder-id' BACKUP_DIR = '/path/to/backup/directory'

5. Test the Scripts

Before setting up automation, it’s essential to verify that the scripts work correctly. You can test them manually:

./backup_script.sh python upload_to_drive.py

6. Set Up a Cron Job

Now, let’s automate the backup process. To do this, set up a cron job to schedule the backup and upload tasks to run every 24 hours. Open your terminal and type:

crontab -e

Then, add the following lines to schedule the tasks:

0 0 * * * /path/to/database-backup/backup_script.sh 30 0 * * * /usr/bin/python /path/to/database-backup/upload_to_drive.py

These lines will execute the bash script to create a database backup at midnight and run the Python script to upload it to Google Drive at 12:30 AM every day.

7. Save and Exit

Save the changes to the cron job file and exit the text editor.

Automation in Action

With these steps completed, your database will be automatically backed up to your Google Drive folder every day. You can check the Google Drive “DatabaseBackups” folder for the backups.

It’s important to note that you should monitor your backups and adjust the retention policy as needed to manage storage space efficiently.

Let’s Wrap It Up!

Automating your database backups to Google Drive is a reliable and cost-effective way to ensure the safety of your data. By following this step-by-step guide, you can set up this automation quickly and efficiently. So, don’t wait; start protecting your valuable data today!

LN Webworks expert team is ready to tailor solutions that meet your every need. Reach out to us now for Drupal project excellence and discover how we can help you with your web development projects.

Read More Blogs

Illustration comparing a custom CRM and an off-the-shelf CRM, showing a business professional choosing between a tailored CRM solution and a packaged CRM software. Other, SaaS Development

SaaS CRMs win on speed. Custom wins on fit and ownership. This isn’t a pitch for either one. Every early-stage SaaS company hits this decision within the first year, usually right after the founder realizes a spreadsheet isn’t a CRM.  The default answer is almost always a SaaS CRM: HubSpot, Salesforce, Zoho, sign up in […]

Illustration of a modernization readiness assessment highlighting business alignment, technology assessment, process optimization, data integration, people and skills, risk and compliance, and a roadmap for successful digital transformation Other

Most “audits” and “assessments” promise clarity but deliver a sales deck with language that lacks clarity. You book the call expecting evidence about your specific platform, and what shows up is a generic pitch for whichever package is easiest for the firm to sell.  That’s the actual reason process transparency matters here, not because a […]

LN Webworks illustration of the BAT-BMS Bluetooth exploit, app ban, and access-control review process Other

In late June and early July, videos started circulating on Instagram of people walking up to moving e-rickshaws, tapping a phone, and watching the vehicle lose power mid-ride.  It spread as a prank trend before anyone fully understood the mechanism. Drivers in Patna (India) reported it first, assuming mechanical failure, until investigators traced it to […]

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.