Blog / Others/ Scheduled VPS Backup to Dropbox (Non-Web Upload)

Scheduled VPS Backup to Dropbox (Non-Web Upload)

使用DropBox定时进行VPS备份的方案(非网页上传)

How It Works

This solution uses the Linux cron scheduler to run a script that triggers the Dropbox client for file synchronization, enabling scheduled backups of VPS data. Compared to real-time syncing, scheduled syncing helps conserve server resources.

Note: The original post referenced a script for 'automatically backing up Linux VPS/server websites/databases and uploading to FTP,' which is a more general-purpose solution. This guide focuses specifically on using Dropbox for backup.

1. Install the Dropbox Client on Linux

First, download the Dropbox client appropriate for your system architecture.

For 32-bit Linux:

wget -O dropbox.tar.gz "https://www.dropbox.com/download?plat=lnx.x86"

For 64-bit Linux:

wget -O dropbox.tar.gz "https://www.dropbox.com/download?plat=lnx.x86_64"

After downloading, extract the archive:

tar xzvf dropbox.tar.gz

Run the Dropbox daemon for the first time to generate a host ID and link your account:

~/.dropbox-dist/dropboxd &

The terminal will output a link containing a host_id. Copy this link, open it in a browser, and log into your Dropbox account to link the machine.

2. Create Symbolic Links for Backup Directories

After linking, a ~/Dropbox folder will be created in your home directory. You can create symbolic links to directories you wish to back up inside this folder.

cd ~/Dropbox
ln -s /home/wwwroot/html
ln -s /home/wwwroot/htdocs
# Add links for other directories as needed

After creating links, start the Dropbox daemon to begin syncing:

~/.dropbox-dist/dropboxd &

3. Configure Scheduled Sync to Save Resources

To save server resources, you can schedule sync sessions using cron instead of running 24/7.

First, stop any running Dropbox process:

pkill dropbox

Then, create a control script, e.g., /root/backup.sh:

#!/bin/bash

DROPBOX_PATH="/root/.dropbox-dist/dropboxd"

start() {
    echo "[$(date)] Starting Dropbox sync..."
    $DROPBOX_PATH &
}

stop() {
    echo "[$(date)] Stopping Dropbox sync..."
    pkill -f "dropboxd"
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 2
        start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

exit 0

Make the script executable:

chmod +x /root/backup.sh

Finally, edit the crontab to set sync times. For example, to start sync at 4 AM and stop at 5 AM daily:

crontab -e

Add these two lines in the editor:

0 4 * * * /bin/bash /root/backup.sh start
0 5 * * * /bin/bash /root/backup.sh stop

Tip: Adjust the sync window based on daily data changes. For minor updates, 10-30 minutes may suffice.

4. MySQL Database Backup Script

In addition to website files, databases should be backed up regularly. Here is a sample script (/root/bakmysql.sh) using mysqldump to back up a MySQL database to the Dropbox directory:

#!/bin/bash

# Database Configuration
DB_NAME="your_database_name"
DB_USER="your_database_user"
DB_PASS="your_database_password"

# Path Configuration
BACKUP_DIR="/root/Dropbox/mysql_backup"
LOG_FILE="/var/log/mysql_backup.log"

mkdir -p $BACKUP_DIR
BACKUP_FILE="${BACKUP_DIR}/db_$(date +%Y%m%d).sql.gz"

echo "--- Backup Start [$(date +'%Y-%m-%d %H:%M:%S')] ---" >> $LOG_FILE

mysqldump -u$DB_USER -p$DB_PASS $DB_NAME | gzip > $BACKUP_FILE

if [ $? -eq 0 ]; then
    echo "Success: Database backed up to $BACKUP_FILE" >> $LOG_FILE
else
    echo "Error: Database backup failed!" >> $LOG_FILE
fi

# Optional: Delete backups older than 7 days
find $BACKUP_DIR -name "db_*.sql.gz" -mtime +7 -delete >> $LOG_FILE 2>&1

echo "--- Backup End [$(date +'%Y-%m-%d %H:%M:%S')] ---" >> $LOG_FILE
echo "" >> $LOG_FILE

Make the script executable and schedule it with cron, e.g., to run daily at 3 AM:

chmod +x /root/bakmysql.sh
# Add to crontab:
0 3 * * * /bin/bash /root/bakmysql.sh

5. Uninstall the Dropbox Client

To uninstall Dropbox, follow these steps:

# 1. Stop the Dropbox process
pkill dropbox

# 2. Remove related files and directories
rm -rf ~/.dropbox ~/.dropbox-dist ~/Dropbox dropbox.tar.gz
# Warning: Deleting ~/Dropbox will also delete files in the local sync folder. Ensure important data is backed up.

Post a Comment

Your email will not be published. Required fields are marked with *.