Detailed Guide to Setting Up Automatic VPS Backup to Google Drive and Dropbox with Simple Bash Script
A detailed guide to setting up automatic VPS data backup to Google Drive and Dropbox using a simple Bash script, suitable for DevOps and system administrators.
Image credit: TerriC · Pixabay Content License
Introduction
Automated data backup is essential for system administration, especially for VPS environments. With a simple Bash script, you can set up automatic backups of your VPS data to cloud storage services like Google Drive and Dropbox without installing complex software. This guide walks you through each step to set up an automated backup system, ensuring your data is always protected.
Prerequisites
- A Linux-based VPS (Ubuntu/Debian)
- Bash shell installed
- Registered Google Drive and Dropbox accounts
- Installed
curl,git, andrsyncon the VPS - Stable internet connection
Setting Up Backup with Google Drive
To back up data to Google Drive, we will use drive, a command-line tool that enables backups to Google Drive.
Install drive
Run the following commands on your VPS:
sudo apt update
sudo apt install -y git
git clone https://github.com/odeke-em/drive.git
cd drive
make
sudo cp drive /usr/local/bin/
Configure drive
Run the command to log in to Google Drive:
drive init
Follow the on-screen instructions to complete the login. After logging in, drive will store authentication information.
Create Backup Script
Create a file named backup_drive.sh:
#!/bin/bash
# Backup script for Google Drive
BACKUP_DIR="/home/backup"
GOOGLE_DRIVE_PATH="/backup"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="backup_$DATE.tar.gz"
cd $BACKUP_DIR
tar -czf $BACKUP_NAME /path/to/your/data
drive push $BACKUP_NAME $GOOGLE_DRIVE_PATH
echo "Backup completed: $BACKUP_NAME"
Run the following command to grant execution permissions:
chmod +x backup_drive.sh
Setting Up Backup with Dropbox
We will use Dropbox-Uploader to back up data to Dropbox.
Install Dropbox-Uploader
Run the following commands:
cd /home
git clone https://github.com/andreafabrizi/Dropbox-Uploader.git
cd Dropbox-Uploader
chmod +x dropbox_uploader.sh
Configure Dropbox
Run the command to create a Dropbox app and grant permissions:
./dropbox_uploader.sh setup
Select "Dropbox" and follow the instructions to complete the setup.
Create Backup Script
Create a file named backup_dropbox.sh:
#!/bin/bash
# Backup script for Dropbox
BACKUP_DIR="/home/backup"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="backup_$DATE.tar.gz"
cd $BACKUP_DIR
tar -czf $BACKUP_NAME /path/to/your/data
./dropbox_uploader.sh upload $BACKUP_NAME /backup/
echo "Backup completed: $BACKUP_NAME"
Grant execution permissions:
chmod +x backup_dropbox.sh
Automating Backups
To run backups automatically, use crontab:
crontab -e
Add the following lines to run backups daily at 2:00 AM:
0 2 * * * /home/backup_drive.sh
0 2 * * * /home/backup_dropbox.sh
Testing and Troubleshooting
Test Backups
Run the scripts manually to verify:
./backup_drive.sh
./backup_dropbox.sh
Ensure no errors occur and data is correctly backed up.
Troubleshooting
- Unable to log in to Google Drive: Verify authentication details.
- Unable to upload to Dropbox: Check Dropbox app permissions.
- Script not running: Check execution permissions and paths.
References
- drive - GitHub
- Dropbox-Uploader - GitHub
- Guide to Configuring a VPS as a Web3 Decentralized IPFS Pinning Service for DApp Projects
- Detailed Guide to Building a VPS-Based Global Ad Blocker with DNS over HTTPS to Block Ads and Trackers on All Devices
Related infrastructure & engineering guides: NVMe Cloud VPS for Docker, Nginx & Databases
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
