Here’s a scripted version of a MySQL backup and automatic upload to Google Drive, using the gdrive CLI (not the complicated Google Drive API).
This is the simplest and most commonly used on Linux servers.
STEP 1 — Install gdrive CLI on the server (just once)
wget -O /usr/local/bin/gdrive https://github.com/prasmussen/gdrive/releases/download/2.1.1/gdrive-linux-x64
chmod +x /usr/local/bin/gdrive
Then initialize:
gdrive about
A link will appear => Log in to Google => Copy the code => Paste it back into the terminal.
After that, GDrive is ready to use.
STEP 2 — Get the Google Drive folder ID for the storage location
Create a folder in Google Drive, for example: MYSQL BACKUP
View the folder ID:
https://drive.google.com/drive/folders/1ABCdEfGhijkLmNoPq
^ folder ID
STEP 3 — Backup + upload + delete old files script
Please replace:
gdrive_folder_id=”1ABCdEfGhijkLmNoPq”
Complete script:
#!/bin/sh
# Script backup mysql + upload ke Google Drive
# Configuration value
mysql_host=”localhost”
mysql_database=”ruko”
mysql_username=”ruko”
mysql_password=’123′
backup_path=/home/ruko/public_html/backup
expired=60 # delete backup files older than 60 days
# ID folder Google Drive (ubah sendiri)
gdrive_folder_id=”1ABCdEfGhijkLmNoPq”
today=$(date +%Y-%m-%d)
timestamp=$(date +_%Y-%m-%d_%H-%M)
sql_file=$backup_path/${mysql_database}${timestamp}.sql
gz_file=${sql_file}.gz
# Create backup directory if not exists
if [ ! -d “$backup_path” ]; then
mkdir -p “$backup_path”
fi
# Create MySQL dump
/usr/bin/mysqldump -h “$mysql_host” -u “$mysql_username” -p”$mysql_password” “$mysql_database” > “$sql_file”
# Compress
gzip “$sql_file”
# Upload to Google Drive
/usr/local/bin/gdrive upload –parent “$gdrive_folder_id” “$gz_file”
# Remove local backup older than 60 days
find “$backup_path” -type f -name “*.gz” -mtime +$expired -exec rm -f {} \;
How it works
Backups are created every time the script is run (usually via cron).
Files are compressed to .gz.
Uploaded to a specific Google Drive folder.
Local backups older than 60 days are automatically deleted.