Command Line Backups in OS X: A Practical Guide
Command line backups in OS X let you back up files and folders using Terminal commands like rsync, ditto, and tar, giving you precise control over what, where, and how data is copied. You enter a command, specify source and destination paths, and the tool copies your data—often with options for incremental updates, permissions preservation, and remote transfers. This guide explains the core commands, how to choose between them, common pitfalls, and how to automate backups with launchd or cron.
Why Use the Command Line for Backups?
Graphical backup tools like Time Machine are excellent for whole-system restores, but they hide the details. Command line backups offer several advantages:
- Selective control: You choose exactly which folders to back up, excluding caches, logs, or large media files.
- Scriptability: You can write a script that runs the same backup every time, with consistent options.
- Remote backups:
rsyncover SSH can back up to a remote server or another Mac securely. - Speed for large data: Incremental
rsynconly copies changed parts of files, which can be much faster than full copies. - No proprietary format: Files are copied as regular files, so you can access them directly without special software.
However, command line backups require care: a mistyped path or a missing flag can overwrite or delete data. Always test commands on a small sample first.
The Core Tools: rsync, ditto, and tar
OS X includes three main command line tools for backups. Each has different strengths.
rsync
rsync is the most flexible and widely used. It synchronizes files and directories between two locations, copying only the differences. Basic syntax:
rsync -av --delete /path/to/source/ /path/to/destination/
-a(archive) preserves permissions, timestamps, symlinks, and other metadata.-v(verbose) shows what is being copied.--deleteremoves files in the destination that no longer exist in the source, making the destination an exact mirror.
To back up over a network, add -e ssh and use a remote path like user@server:/path. For example:
rsync -av -e ssh /Users/me/Documents/ me@backup-server:/backups/Documents/
Important: a trailing slash on the source path means “copy the contents of the folder,” while no trailing slash means “copy the folder itself.” This is a common source of confusion.
ditto
ditto is a native macOS tool that preserves resource forks and extended attributes, which is important for older Mac files. It is simpler than rsync and works well for local copies.
ditto /path/to/source /path/to/destination
Unlike rsync, ditto does not have a --delete option, so it will not remove files that were deleted from the source. It is a good choice for one-time copies or when you need to preserve all metadata.
tar
tar creates archive files (like .tar or .tar.gz) that bundle many files into one. This is useful for compressing backups or for transferring a folder as a single file.
tar -czvf backup.tar.gz /path/to/source
-ccreate archive-zcompress with gzip-vverbose-fspecify filename
To restore, use tar -xzvf backup.tar.gz. Note that tar does not preserve resource forks by default; use --apple-double or --mac-metadata flags to include them, but this is rarely needed for modern files.
Choosing the Right Tool for Your Backup
Your choice depends on what you are backing up and where it is going.
- Local external drive, need an exact mirror: Use
rsync -av --delete. - Local external drive, want a simple copy without deletion: Use
ditto. - Remote server over SSH: Use
rsync -av -e ssh. - Compressed archive for storage or transfer: Use
tar -czvf. - Full system backup: Time Machine is still the best choice, as it handles system files, snapshots, and restore to a new Mac.
For most personal file backups, rsync is the recommended default because of its flexibility and incremental nature.
Automating Backups with launchd and cron
Manual backups are easy to forget. You can automate command line backups using macOS’s built-in schedulers.
Using cron
Edit your crontab with crontab -e and add a line like:
0 2 * * * /usr/bin/rsync -av --delete /Users/me/Documents/ /Volumes/Backup/Documents/
This runs the backup every day at 2:00 AM. The format is: minute hour day-of-month month day-of-week. Note that cron jobs may not run if the Mac is asleep; use pmset to schedule wake, or use launchd instead.
Using launchd
launchd is the modern macOS scheduler. Create a property list file in ~/Library/LaunchAgents (for user jobs) or /Library/LaunchDaemons (for system jobs). Example com.example.backup.plist:
<?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.example.backup</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/rsync</string>
<string>-av</string>
<string>--delete</string>
<string>/Users/me/Documents/</string>
<string>/Volumes/Backup/Documents/</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>2</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
</dict>
</plist>
Load it with launchctl load ~/Library/LaunchAgents/com.example.backup.plist. launchd can also run jobs when a drive is attached or at login, which is more reliable than cron for laptops.
Common Problems and How to Avoid Them
Even simple commands can cause trouble. Here are the most frequent issues:
- Trailing slash mistakes:
rsync /source /destcopies the source folder intodest, whilersync /source/ /destcopies the contents of source intodest. Test with-n(dry run) first. - Deleting data unintentionally:
--deleteis powerful. If the source is accidentally empty or unmounted,rsyncwill delete everything in the destination. Always mount the source and check it withlsbefore running. - Permissions issues: If you get “Operation not permitted”, you may need to run the command with
sudo(but be careful) or grant Terminal Full Disk Access in System Settings > Privacy & Security. - Backup drive not mounted: Automate a check:
if [ -d /Volumes/Backup ]; then rsync ...; else echo "Drive not mounted"; fi - Symbolic links and hard links:
rsync -apreserves symlinks, but if you want to copy the files they point to, use-Linstead.
Safety and Best Practices
Command line backups are only safe if you follow a few rules:
- Always test with
--dry-runor-nfirst. This shows what would happen without making changes. - Keep a backup of your backup script. Store the command in a text file so you can review it before running.
- Verify the backup. After a backup, run
rsync -av --checksum source/ dest/(without--delete) to confirm no differences. - Do not back up to the same drive as the original. If the drive fails, you lose both copies.
- Use versioned backups.
rsyncwith--backup-dircan keep old versions of changed files, protecting against accidental edits. - Be cautious with
sudo. Running as root can overwrite system files. Only use it when necessary.
Also, remember that command line tools do not handle open files (like a database in use) perfectly. For such files, consider using a tool designed for that, or stop the service before backup.
Alternatives to Command Line Backups
If the command line feels too risky, there are built-in and third-party alternatives:
- Time Machine: The easiest, most comprehensive backup for macOS. It automatically backs up everything, including system files, and allows easy restores.
- Disk Utility: Can create disk images (.dmg) of folders or drives, but not incremental.
- iCloud: Syncs files to the cloud, but not a true backup (changes propagate both ways).
- Third-party tools: Carbon Copy Cloner and SuperDuper provide a GUI for
rsync-like cloning, with scheduling and safety checks.
For most users, Time Machine is the recommended primary backup. Command line backups are best for specific folders, remote servers, or when you need automation and control.
Frequently Asked Questions
What is the difference between rsync and Time Machine?
Time Machine creates scheduled, versioned snapshots of your entire system, storing hourly, daily, and weekly backups. rsync is a file-synchronization tool that copies files to a destination, optionally deleting files that were removed from the source. Time Machine is better for full system restore; rsync is better for selective folder backups or mirroring.
How do I exclude files from an rsync backup?
Use the --exclude option. For example, rsync -av --exclude='*.tmp' --exclude='Cache/' /source/ /dest/. You can also put a list of patterns in a file and use --exclude-from=file.txt.
Does rsync preserve macOS metadata like Finder tags?
Yes, with the -a (archive) flag, rsync preserves permissions, timestamps, symlinks, and extended attributes, which include Finder tags and other metadata. However, some macOS-specific metadata (like resource forks) may not be fully preserved on non-HFS+ filesystems; ditto is better for that.
Can I back up to a network drive using command line?
Yes. You can mount the network drive (e.g., SMB or NFS) and then use rsync as usual, or use rsync over SSH to a remote server. For SMB, mount it first with mount_smbfs or via Finder, then reference the mounted volume path.
Why does my rsync backup fail with “Operation not permitted”?
This often happens when Terminal does not have Full Disk Access. Go to System Settings > Privacy & Security > Full Disk Access and enable Terminal. Also, if you are backing up files from protected locations like Mail or Photos, you may need to grant access to those folders.
How do I restore a file from an rsync backup?
Simply reverse the command: rsync -av /path/to/backup/ /path/to/original/. This copies the backup back to the original location. Be careful not to use --delete unless you intend to remove files in the original that are not in the backup.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.