Install rclone (for OneDrive syncing):
sudo apt install rclone
Install tar (for compressing files, usually pre-installed):
sudo apt install tar
Step 2: Configure rclone for OneDrive
1. Set up rclone: Run the configuration command:
rclone config
2. Verify OneDrive connection: List directories in your OneDrive to confirm
rclone lsd onedrive:
Step 3: Perform Initial Full Backup
mkdir -p ~/backups/full
mkdir -p ~/backups/incremental
4. Back up project files: Assume your project files are in /var/www/project. Adjust the path as needed.
Compress the project directory:
tar -czf ~/backups/full/project_full_$(date +%F).tar.gz /var/www/project
Back up the database:
mysqldump -u dbuser -p’dbpass’ dbname > ~/backups/full/db_full_$(date +%F).sql
pg_dump -U dbuser dbname > ~/backups/full/db_full_$(date +%F).sql
Upload full backup to OneDrive: Create a Backups folder in OneDrive and upload:
rclone mkdir onedrive:Backups/Full
rclone copy ~/backups/full onedrive:Backups/Full
Verify the upload:
rclone ls onedrive:Backups/Full
Step 4: Set Up Incremental Backups
For incremental backups, we’ll use rsync for files and database dumps with rclone to sync only changes.
- Install rsync (if not installed): bashCollapseWrapRunCopy
sudo apt install rsync - Script for incremental file backups: Create a script to back up only changed files: bashCollapseWrapRunCopy
nano ~/backup_incremental.shAdd the following, replacing /var/www/project with your project path: bashCollapseWrapRunCopy#!/bin/bashBACKUP_DIR=~/backups/incremental/files_$(date +%F_%H-%M-%S)mkdir -p $BACKUP_DIRrsync -av --link-dest=~/backups/full/project_full_$(date +%F).tar.gz /var/www/project $BACKUP_DIR/rclone copy $BACKUP_DIR onedrive:Backups/Incremental - Script for incremental database backups: Add to the same script:
- For MySQL/MariaDB: bashCollapseWrapRunCopy
mysqldump -u dbuser -p'dbpass' dbname > $BACKUP_DIR/db_incremental_$(date +%F_%H-%M-%S).sql - For PostgreSQL: bashCollapseWrapRunCopy
pg_dump -U dbuser dbname > $BACKUP_DIR/db_incremental_$(date +%F_%H-%M-%S).sql - Add the upload command: bashCollapseWrapRunCopy
rclone copy $BACKUP_DIR onedrive:Backups/Incremental
#!/bin/bashBACKUP_DIR=~/backups/incremental/files_$(date +%F_%H-%M-%S)mkdir -p $BACKUP_DIRrsync -av --link-dest=~/backups/full/project_full_$(date +%F).tar.gz /var/www/project $BACKUP_DIR/mysqldump -u dbuser -p'dbpass' dbname > $BACKUP_DIR/db_incremental_$(date +%F_%H-%M-%S).sqlrclone copy $BACKUP_DIR onedrive:Backups/Incremental - For MySQL/MariaDB: bashCollapseWrapRunCopy
- Make the script executable: bashCollapseWrapRunCopy
chmod +x ~/backup_incremental.sh - Test the script: bashCollapseWrapRunCopy
~/backup_incremental.sh - Verify incremental backup in OneDrive: bashCollapseWrapRunCopy
rclone ls onedrive:Backups/Incremental
Step 5: Automate Incremental Backups
- Schedule with cron: Edit the cron jobs: bashCollapseWrapRunCopy
crontab -eAdd a line to run the script daily at 2 AM: bashCollapseWrapRunCopy0 2 * * * /bin/bash ~/backup_incremental.sh - Optional: Clean up old backups: To avoid filling your server, add a cleanup command to the script to keep only the last 7 days: bashCollapseWrapRunCopy
find ~/backups/incremental -type d -mtime +7 -exec rm -rf {} \;
Step 6: Monitor and Verify
- Check OneDrive: Log in to your OneDrive account via the Office 365 portal and verify that the Backups/Full and Backups/Incremental folders contain your files.
- Test restores (optional but recommended):
- Download a backup: bashCollapseWrapRunCopy
rclone copy onedrive:Backups/Full ~/restore - Extract files: bashCollapseWrapRunCopy
tar -xzf ~/restore/project_full_*.tar.gz -C ~/restore - Restore database (MySQL example): bashCollapseWrapRunCopy
mysql -u dbuser -p'dbpass' dbname < ~/restore/db_full_*.sql
- Download a backup: bashCollapseWrapRunCopy
Notes
Full Backup Frequency: Schedule full backups weekly/monthly by adding a cron job for the full backup commands.
Security: Store database credentials securely (e.g., in a .my.cnf file for MySQL or .pgpass for PostgreSQL) instead of hardcoding them.
OneDrive Limits: Education accounts typically have 1TB storage, but check your quota. Use rclone size onedrive: to monitor usage.
Error Handling: Add logging to the script for troubleshooting: bashCollapseWrapRunCopyecho "$(date): Backup completed" >> ~/backups/backup.log
Database Type: If you use a different database (e.g., MongoDB), let me know, and I’ll provide the appropriate dump command.
Final Working one