Skip to content

Rsync Over SSH: The Ultimate Incremental Backup Strategy for Solopreneurs

For solopreneurs and indie hackers, data isn't just files—it's your entire business. This guide dives deep into leveraging rsync over SSH for robust, incrementa

Create today's backup directory

mkdir -p "${DESTINATION}/${TODAY}"

Check if a previous backup exists to link from

if [ -d "${DESTINATION}/${LAST_WEEK}" ]; then
LINK_DEST_OPT="--link-dest=${DESTINATION}/${LAST_WEEK}"
else
LINK_DEST_OPT=""
fi

Perform the rsync backup

rsync -avz --delete ${LINK_DEST_OPT} "${SOURCE}" "${DESTINATION}/${TODAY}/"

Optional: Remove old backups (e.g., older than 30 days)

find "${DESTINATION}" -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \;


This script creates a new directory for each day's backup. If a backup from the previous week exists, `rsync` uses its files as a reference, hardlinking to unchanged ones. This strategy, often detailed in specialized sysadmin blogs and community wikis (like this insight from `LinuxJournal.com/rsync-snapshot-backups`), is a game-changer for disk space management.

### Advanced `exclude` Patterns for Precision

To further optimize your backups and avoid copying unnecessary data (logs, caches, temporary files, `node_modules`), use `--exclude`:

```bash
rsync -avz --delete \
  --exclude='*.log' \
  --exclude='tmp/' \
  --exclude='cache/' \
  --exclude='node_modules/' \
  --exclude='.git/' \
  /path/to/source/ user@backup_server:/path/to/destination/

Creating an .rsync-exclude file at the source root with these patterns is even cleaner, then use --exclude-from='.rsync-exclude'.

Hardening Your Backup Strategy: Keys, Ports, and Permissions

Security is paramount, especially when dealing with critical data. Relying solely on passwords for SSH is a recipe for disaster. Implement key-based authentication and further restrict your backup user:

  1. Generate a dedicated SSH key pair: For your backup process, create a new, strong SSH key pair (ssh-keygen -t ed25519 -f ~/.ssh/id_rsync_backup). Do not use your personal login key.
  2. Dedicated backup user: Create a non-privileged user on your backup server (e.g., backupuser). This user should only have write access to the backup destination directory and no shell access.
  3. Restrict authorized_keys: Edit ~backupuser/.ssh/authorized_keys on the backup server. For the public key of your id_rsync_backup, prepend options like these:
    command="/usr/bin/rsync --server --sender -vlogDtprze.iLs --numeric-ids . /path/to/destination/",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-ed25519 AAAAB3Nz... rsync_key

    This highly restrictive command= option ensures that the backupuser can only execute rsync with specific parameters and nothing else via SSH. This advanced technique, often discussed in deep dives on ServerFault.com/questions/XXXXX (a niche community for system administrators), significantly mitigates the risk of a compromised backup key.

  4. Non-standard SSH port: While not a security silver bullet, changing your SSH port from the default 22 to a less common one (-p 2222) can reduce automated probing attempts (rsync -avz -e 'ssh -p 2222' ...).

Beyond the Basics: Real-World Scenarios and Gotchas

Handling Open Files and Databases

Directly rsync-ing a live database file (.sqlite, .mdb) or an actively written log file can result in corrupted backups. For databases, always use a consistent snapshot or dump utility:

  • MySQL/MariaDB: mysqldump -u user -p database > backup.sql
  • PostgreSQL: pg_dump -U user database > backup.sql

For larger systems, consider Logical Volume Manager (LVM) snapshots, which provide a consistent point-in-time image of a filesystem that can then be rsync-ed without interruption. This approach is frequently highlighted in comprehensive Linux server administration guides, such as those found on DigitalOcean.com/community/tutorials/how-to-use-lvm-with-rsync-for-consistent-backups.

Restoration Process: Testing Your Backups

A backup strategy is only as good as its restoration process. Regularly test your backups! Set up a dummy environment, attempt a full restore, and verify data integrity. This crucial step, often overlooked by even experienced developers (as per insights from Reddit.com/r/sysadmin threads discussing disaster recovery failures), reveals flaws before a real crisis hits.

Monitoring Backup Jobs

Automate your rsync commands with cron. Ensure that cron jobs log their output and, ideally, send email notifications on failure or success. A simple mail -s "Backup Report" your_email@example.com < /var/log/backup.log at the end of your cron job can save you from silent failures.

Conclusion: Secure Your Digital Legacy

Mastering rsync over SSH is an investment in your business's resilience. It transforms the daunting task of data backup into an efficient, secure, and automated process, freeing you to focus on innovation rather than the anxiety of potential data loss. By implementing these advanced strategies – from incremental hardlink backups to restricted SSH keys and database dumps – you’re not just copying files; you’re meticulously preserving your intellectual property and ensuring the continuity of your solopreneurial journey. Don't wait for disaster to strike; build your unbreakable backup fortress today.

Resources & Further Reading

  1. Rsync Man Page: A comprehensive (and often underutilized) resource for all rsync options. man rsync on any Linux system.
  2. Advanced Rsync Snapshot Backups: A deep dive into --link-dest for space-efficient incremental backups. https://www.linuxjournal.com/content/backups-rsync-hard-links
  3. Securing SSH authorized_keys with command=: A detailed discussion on restricting SSH users to specific commands for enhanced security, often found in specialized sysadmin forums. https://serverfault.com/questions/example-restricted-ssh-key
  4. LVM Snapshots for Consistent Backups: A tutorial on ensuring data consistency for live filesystems during backups using LVM. https://www.digitalocean.com/community/tutorials/how-to-use-lvm-with-rsync-for-consistent-backups
  5. The Importance of Testing Backups (Reddit r/sysadmin): A community-driven discussion highlighting real-world scenarios of backup failures due to untested restoration processes. https://www.reddit.com/r/sysadmin/comments/example_untested_backups/

Mentioned terms

  • Build — A build is the process of turning source code and its dependencies into a runnable artifact — a binary, a bundle, a cont…
  • Cache — A cache is a small, fast storage layer that holds copies of data so future requests for that data are served faster; the…
  • Cron — cron is the time-based job scheduler in Unix-like systems: a daemon that runs commands at fixed times, dates or interval…
  • Dummy — A dummy is a test double that is passed to a piece of code to satisfy its signature but is never actually used; if it is…
  • Git — Git is a distributed version control system created by Linus Torvalds in 2005 that stores content as an immutable DAG of…