Backing Up with Rsync

I have a deep appreciation for automation, templates, and shortcuts—anything that reduces repetitive tasks. Automation liberates people from tedious manual work, enabling us to focus on more meaningful and creative endeavors. It doesn’t inherently foster laziness or eliminate jobs; instead, it creates opportunities for humans to engage in tasks that require distinctly human skills, leaving the mechanical work to machines. Rsync is an example of such automation.

Rsync

If you don’t know what rsync is, I highly recommend you learn about it. Rsync is an open-source tool that quickly updates files between two places by only transferring the parts of the files that have changed. It doesn’t need both sets of files to be in the same location to figure out the changes, which might seem surprising at first. It was initially released on June 19, 1996 and is available for any Linux distribution and on GitHub.

A Simple Backup Script

I have encountered a few times when I needed to rebuild my computer or migrate to another device and wanted to take some critical files with me. While I generally use Nextcloud these days for many things, It takes some time to set up and sync on a new device. It also depends on connectivity to a central server.

As I learned more about using SSH I came across rsync and realized its value immediately. I leveraged rsync in a script that will back up my critical files (i.e. anything I deem necessary) to an external 16GB USB drive that stays connected to my desktop PC. The script resides on the USB and is executed via a cron job daily. The majority of the script will backup select files in my home directory, including some hidden folders such as `.ssh`, my local email client directory, and more. Alternatively I can run this script to backup the same files to another location on my network.

In addition, the script will export a list of all applications currently installed on my desktop at the time of execution. This is helpful if I need to rebuild as it can be difficult to remember those one-off applications that are very helpful but used infrequently. Finally, the script will also export a list of cron jobs from the crontab file. This is important as these jobs are easily forgotten in their “set it and forget it nature.”

Note: Additional security can be incorporated by building out the script to compress the resulting files into a password protected archive.

Documentation for a Simple Backup Script

Purpose

This script automates the process of backing up essential configuration files, personal data, and system settings to an external USB drive and web server. It is designed to ensure that critical files are preserved and organized in case of system failure or data loss.


Detailed Explanation

  1. Backup User-Specific Configuration and Data Files
    • The script uses rsync to back up essential files and directories with the following options:
      • -r: Recursively copies directories.
      • -t: Preserves modification times.
      • -v: Enables verbose mode.
      • -z: Compresses data during the transfer.
      • -P: Shows progress and handles partial transfers.
    • Files and Directories Backed Up:
      • ~/lynx_bookmarks.html: Lynx web browser bookmarks.
      • ~/.bashrc: Bash configuration file.
      • ~/.bash_aliases: Aliases for custom Bash commands.
      • ~/Desktop: Files and directories on the desktop.
      • ~/.muttrc: Configuration file for the Mutt email client.
      • ~/.ssh: SSH keys and configurations.
      • ~/.thunderbird: Thunderbird email client data.
      • ~/.newsboat/urls and ~/.newsboat/newsboat_config: Newsboat RSS feed URLs and configuration.
      • ~/bin: Custom scripts and binaries created by the user.
      • /etc/hosts and /etc/fstab: System-level configuration files (networking and filesystem table).
  2. Export Configuration to a Web Server
    • Copies ~/.bash_aliases to a web server directory for remote access.
  3. Export of Cron Jobs
    • The crontab -l command outputs the list of scheduled cron jobs.
    • The output is saved in /media/username/backups/crontab/contab.txt.
  4. Export List of Installed Applications
    • The dpkg --get-selections command generates a list of all installed packages.
    • The list is saved as apps.installed.txt.

Usage

  1. Make the script executable: chmod +x backup_script.sh
  2. Run the script: ./backup_script.sh

Notes

  • Custom Paths: Update the paths (e.g., /media/username/backups/) to match your system’s directory structure.
  • Additions: To back up additional files, append new rsync commands.
  • History: The NOTES section is reserved for logging changes with timestamps (e.g., YYYY-MM-DD: Added line to back up xyz file).

Example Output

  • Backup progress and file names will be displayed on the terminal.
  • Logs of exported files will be saved in specified locations.

This script ensures an organized and efficient backup process, making restoration straightforward in case of data loss. As I mentioned earlier, security of the exported files can be incorporated but that is not in the scope of this current iteration. I encourage you to explore rsync (or alternatives) to protect against data loss with limited effort.

Scroll to Top