Uncategorized

Rclone Backup Systems

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.

  1. Install rsync (if not installed): bashCollapseWrapRunCopysudo apt install rsync
  2. Script for incremental file backups: Create a script to back up only changed files: bashCollapseWrapRunCopynano ~/backup_incremental.sh Add the following, replacing /var/www/project with your project path: bashCollapseWrapRunCopy#!/bin/bash BACKUP_DIR=~/backups/incremental/files_$(date +%F_%H-%M-%S) mkdir -p $BACKUP_DIR rsync -av --link-dest=~/backups/full/project_full_$(date +%F).tar.gz /var/www/project $BACKUP_DIR/ rclone copy $BACKUP_DIR onedrive:Backups/Incremental
  3. Script for incremental database backups: Add to the same script:
    • For MySQL/MariaDB: bashCollapseWrapRunCopymysqldump -u dbuser -p'dbpass' dbname > $BACKUP_DIR/db_incremental_$(date +%F_%H-%M-%S).sql
    • For PostgreSQL: bashCollapseWrapRunCopypg_dump -U dbuser dbname > $BACKUP_DIR/db_incremental_$(date +%F_%H-%M-%S).sql
    • Add the upload command: bashCollapseWrapRunCopyrclone copy $BACKUP_DIR onedrive:Backups/Incremental
    The full script might look like this (for MySQL): bashCollapseWrapRunCopy#!/bin/bash BACKUP_DIR=~/backups/incremental/files_$(date +%F_%H-%M-%S) mkdir -p $BACKUP_DIR rsync -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).sql rclone copy $BACKUP_DIR onedrive:Backups/Incremental
  4. Make the script executable: bashCollapseWrapRunCopychmod +x ~/backup_incremental.sh
  5. Test the script: bashCollapseWrapRunCopy~/backup_incremental.sh
  6. Verify incremental backup in OneDrive: bashCollapseWrapRunCopyrclone ls onedrive:Backups/Incremental

Step 5: Automate Incremental Backups

  1. Schedule with cron: Edit the cron jobs: bashCollapseWrapRunCopycrontab -e Add a line to run the script daily at 2 AM: bashCollapseWrapRunCopy0 2 * * * /bin/bash ~/backup_incremental.sh
  2. Optional: Clean up old backups: To avoid filling your server, add a cleanup command to the script to keep only the last 7 days: bashCollapseWrapRunCopyfind ~/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: bashCollapseWrapRunCopyrclone copy onedrive:Backups/Full ~/restore
    • Extract files: bashCollapseWrapRunCopytar -xzf ~/restore/project_full_*.tar.gz -C ~/restore
    • Restore database (MySQL example): bashCollapseWrapRunCopymysql -u dbuser -p'dbpass' dbname < ~/restore/db_full_*.sql

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

Leave a Reply

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