Baidu Netdisk offers free storage with decent upload/download speeds and stable access within China. For individual website owners or developers, it serves as a low-cost off-site backup destination for website files and databases.
This article introduces an automated Shell script that packages Nginx/Apache configuration files, website directories, and MySQL databases from a VPS and uploads them to Baidu Netdisk for backup.
Script Functionality and Workflow
The script performs the following steps:
- Define Variables: Set database credentials, backup directories, and paths for websites and configurations.
- Create Local Backup Directory: Ensure a temporary directory for backup files exists.
- Backup MySQL Databases: Export all user databases (excluding system databases) and compress them individually.
- Package Website Files and Configurations: Compress specified website directories and web server configuration files.
- Upload to Baidu Netdisk: Use an upload tool to transfer backup files to a designated directory on the cloud drive.
- Clean Up Local Temporary Files: Delete local backups after upload to free up disk space.
Prerequisites
Before running the script, complete these preparations:
- Create a directory on Baidu Netdisk for storing backups.
- Configure a Baidu Netdisk upload tool on your VPS (e.g., a third-party command-line client using the official API).
- Ensure the backup directories, website paths, database passwords, etc., in the script match your actual environment.
Backup Script (2024 Updated Version)
Below is the optimized and error-corrected script. Modify configurations as noted in the comments:
#!/bin/bash
# ==================== User Configuration ====================
# MySQL credentials
MYSQL_USER="root"
MYSQL_PASS="your_mysql_password" # Replace with your password
# Baidu Netdisk target directory (subfolder by date)
BAIDUPAN_BASE_DIR="/VPS_Backup"
BAIDUPAN_DIR="$BAIDUPAN_BASE_DIR/$(date +%Y-%m-%d)"
# Local temporary backup directory
LOCAL_BACKUP_DIR="/tmp/vps_backup_$(date +%Y%m%d)"
# Directories to backup (adjust paths as needed)
WEB_SERVER_CONFIG="/etc/nginx" # Nginx config
# WEB_SERVER_CONFIG="/etc/apache2" # Uncomment for Apache
WEBSITE_DIR="/var/www/html" # Website root
# Backup filenames (with date)
MYSQL_BACKUP_FILE="mysql_$(date +%Y%m%d).tar.gz"
WEBSITE_BACKUP_FILE="website_$(date +%Y%m%d).tar.gz"
CONFIG_BACKUP_FILE="config_$(date +%Y%m%d).tar.gz"
# Path to Baidu Netdisk upload tool
UPLOADER_SCRIPT="/path/to/your/bpcs_uploader.php"
# ==================== End Configuration ====================
mkdir -p "$LOCAL_BACKUP_DIR"
cd "$LOCAL_BACKUP_DIR" || exit 1
# 1. Backup MySQL databases
DATABASES=$(mysql -u"$MYSQL_USER" -p"$MYSQL_PASS" -B -N -e "SHOW DATABASES" | grep -Ev '^(information_schema|performance_schema|mysql|sys)$')
for DB in $DATABASES; do
echo "Backing up database: $DB"
mysqldump -u"$MYSQL_USER" -p"$MYSQL_PASS" --single-transaction --quick "$DB" | gzip -9 > "${DB}.sql.gz"
if [ $? -ne 0 ]; then
echo "Warning: Backup failed for $DB. Check permissions/connection."
fi
done
if ls *.sql.gz >/dev/null 2>&1; then
tar -zcvf "$MYSQL_BACKUP_FILE" *.sql.gz
echo "Database archive created: $MYSQL_BACKUP_FILE"
else
echo "No database backup files found. Skipping."
MYSQL_BACKUP_FILE=""
fi
# 2. Backup website files
if [ -d "$WEBSITE_DIR" ]; then
tar -zcvf "$WEBSITE_BACKUP_FILE" -C "$(dirname "$WEBSITE_DIR")" "$(basename "$WEBSITE_DIR")"
echo "Website archive created: $WEBSITE_BACKUP_FILE"
else
echo "Warning: Website directory $WEBSITE_DIR not found. Skipping."
WEBSITE_BACKUP_FILE=""
fi
# 3. Backup web server config
if [ -d "$WEB_SERVER_CONFIG" ]; then
tar -zcvf "$CONFIG_BACKUP_FILE" -C "$WEB_SERVER_CONFIG" ./*.conf 2>/dev/null
echo "Config archive created: $CONFIG_BACKUP_FILE"
else
echo "Warning: Config directory $WEB_SERVER_CONFIG not found. Skipping."
CONFIG_BACKUP_FILE=""
fi
# 4. Upload to Baidu Netdisk
if [ ! -x "$UPLOADER_SCRIPT" ]; then
echo "Error: Upload tool $UPLOADER_SCRIPT missing or not executable."
exit 1
fi
for BACKUP_FILE in "$MYSQL_BACKUP_FILE" "$WEBSITE_BACKUP_FILE" "$CONFIG_BACKUP_FILE"; do
if [ -n "$BACKUP_FILE" ] && [ -f "$BACKUP_FILE" ]; then
echo "Uploading $BACKUP_FILE to Baidu Netdisk..."
php "$UPLOADER_SCRIPT" upload "$BACKUP_FILE" "$BAIDUPAN_DIR/$BACKUP_FILE"
if [ $? -eq 0 ]; then
echo "Upload successful: $BACKUP_FILE"
else
echo "Upload failed: $BACKUP_FILE"
fi
fi
done
# 5. Clean up
rm -rf "$LOCAL_BACKUP_DIR"
echo "Backup process completed!"
exit 0
Usage and Automation
First Run
- Save the script to your VPS (e.g.,
/usr/local/bin/backup_to_baidu.sh). - Make it executable:
chmod +x /usr/local/bin/backup_to_baidu.sh. - Edit the script to update all configuration items (database password, directory paths, upload tool path).
- Run manually to test:
bash /usr/local/bin/backup_to_baidu.sh.
Schedule with Cron
To automate daily backups, add a cron job (e.g., run at 3 AM daily):
0 3 * * * /usr/local/bin/backup_to_baidu.sh >> /var/log/vps_backup.log 2>&1
This logs output and errors to /var/log/vps_backup.log for troubleshooting.
Notes and Recommendations
- Security: The script contains database passwords. Store sensitive data in environment variables or a secure config file.
- Backup Verification: Periodically check backup files on Baidu Netdisk for integrity and test restoration.
- Storage Limits: Be aware of any traffic or storage restrictions for free Baidu Netdisk accounts.
- Incremental Backups: The script performs full backups. For incremental backups, consider using
rsyncor similar tools to create local snapshots before uploading. - Error Handling: Basic error checks are included. For production use, enhance logging and add notification alerts (e.g., email).
This setup provides a low-cost, automated VPS data backup solution to mitigate data loss risks.