- Published on

Automating Backups rsync macOS: Some Simple Steps to Secure Your Data
How to automate backups using `rsync` on macOS, ensuring your data is safe and sound with minimal effort
Data is everything, safeguarding your important files is crucial. Whether you're a developer, a student, or someone who loves digital organization, having an automated backup system helps you secure your data and offers peace of mind.
rsync is a powerful tool that has gained immense popularity for backup solutions. Here's why:
- Efficiency:
rsynconly transfers the changes made to files rather than copying everything each time. This approach not only saves time but also conserves bandwidth, which is essential for those with limited internet access. - Flexibility: It allows users to exclude certain files or directories, such as temporary files or version control systems, using very simple commands.
- Comprehensive Logging: If something goes wrong during the backup process,
rsyncprovides detailed logs that help identify errors or issues, simplifying troubleshooting.
Step 1: Creating Your Backup Script
The first step is creating a backup Bash script. Here's a simple example:
#!/bin/bash
# Define common exclusions
EXCLUDES=(
'*.log'
'*/tmp'
'*/__pycache__'
'*/node_modules'
'*/.yarn'
'*/.venv'
'*/venv'
'*/.terraform'
'*/.terragrunt'
'*/.git'
'*/.next'
'*/build'
'*/vendor'
'.DS_Store'
'.localized'
'.Spotlight-V100'
'.Trashes'
'.fseventsd'
)
# Convert exclusions to rsync format
RSYNC_EXCLUDES=""
for EXCLUDE in "${EXCLUDES[@]}"; do
RSYNC_EXCLUDES+="--exclude='$EXCLUDE' "
done
# Define source and destination paths
SOURCES=(
"/Users/user/workspace/work/*"
"/Users/user/workspace/personal/*"
)
DESTINATIONS=(
"/Volumes/Backup/ws-work"
"/Volumes/Backup/ws-personal"
)
# Perform rsync operations
for i in "${!SOURCES[@]}"; do
echo "Syncing ${SOURCES[$i]} to ${DESTINATIONS[$i]} ..."
echo "rsync -avh $RSYNC_EXCLUDES ${SOURCES[$i]} ${DESTINATIONS[$i]}"
# The eval command is used to correctly interpret and execute the constructed rsync command string with the dynamically generated exclusion patterns.
eval rsync -avh $RSYNC_EXCLUDES "${SOURCES[$i]}" "${DESTINATIONS[$i]}"
done
How This Works
The script initializes arrays for SOURCES (the files you want to backup) and DESTINATION (where to store the backups).
- You can customize the EXCLUDES list to skip unnecessary directories or files.
- Utilizing
avhinrsyncensures that all arguments will operate efficiently; this stands for archive mode, verbose output, and human-readable sizes. Step 2: Making the Script Executable
You’ll need to make your script executable. Simply run the following command in your Terminal:
chmod +x /path/to/backup.sh
Step 3: Automating Backups with **cron**
Now that the script is ready, the next phase is to set up a schedule for your backups. In macOS, you can use cron or launchctl.
Using **cron**
- Open the Terminal and enter:
crontab -e
- Add this line to schedule your backup every day at 3 AM:
0 3 * * * /path/to/backup.sh >> /path/to/backup.log 2>&1
Using **launchctl**
Alternatively, you can use launchctl by creating a property list file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.backup</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/backup.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>3</integer> <!-- 3 AM -->
<key>Minute</key>
<integer>0</integer> <!-- 0 minutes -->
</dict>
<!-- Log output paths -->
<key>StandardOutPath</key>
<string>/path/to/backup_stdout.log</string>
<key>StandardErrorPath</key>
<string>/path/to/backup_stderr.log</string>
</dict>
</plist>
Load the job with:
launchctl load ~/Library/LaunchAgents/com.user.backup.plist
Your Backup, Your Safety
By automating your backups with rsync on macOS, you're taking a proactive measure to protect your data. Now, every day at 3 AM, your files will sync to your external hard drive, allowing you to enjoy life without worrying about losing anything important.
Takeaway Points
- Automate Your Backups: Schedule backups daily or weekly to ensure data is consistently updated and protected.
- Use **
rsync** for Efficiency: The tool only copies changes, saving time and space. - Customize Exclusions: Tailor your backup to ignore unnecessary files.
- Error Logging: Catch and address potential issues with detailed logs provided by
rsync. - Choose Your Scheduler: Use
cronfor simplicity orlaunchctlfor advanced scheduling on macOS. - Regular Checks: Periodically verify that your backups are working correctly.