

Backing up your iCloud Drive files using rsync
source link: https://www.jessesquires.com/blog/2019/09/27/icloud-backup-using-rsync/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

27 Sep 2019
software-dev apple, icloud, ios, macos
Unfortunately, iCloud does not have a good reputation for being reliable, especially during beta releases of iOS and macOS. Yet a lot people still use it, often without any problems. I still use it, despite a few bad experiences in the past, because the best alternatives are questionable for other reasons. I’ve had good luck with iCloud Drive for the past few years, but I am terrified and paranoid of getting caught in the middle of an iCloud clusterfuck, so I backup what I have in iCloud periodically using rsync
.
Exploring iCloud Drive
The iCloud Drive folder on your Mac is tricky. The files are not what they seem. iCloud Drive displays as a special directory in the sidebar of Finder. Its actual location on disk is at ~/Library/Mobile Documents/
. If you cd
there and ls
, you will see its entire contents, most of which is not viewable via Finder.
There appear to be three general types of directories present. After some digging around, I could not discern a difference between them. They have the following formats:
- Developer ID or “App ID Prefix” (I think?) followed by bundle identifier. Examples:
8Z3V4F58RK~com~ustwo~monumentvalley/
F3LWYJ7GM7~com~apple~mobilegarageband/
F3LWYJ7GM7~com~apple~musicmemos~ideas/
- Apple first-party apps with specific iCloud support, prefixed with
com~apple~
. These correspond to the “special” app-specific folders that you see in Finder’s view of iCloud Drive. However, not all of these are present in Finder, like Notes and Mail. Examples:com~apple~Automator/
com~apple~Keynote/
com~apple~Preview/
com~apple~TextEdit/
com~apple~Notes/
com~apple~mail/
- Bundle identifiers prefixed with
iCloud~com~
. Examples:iCloud~com~agilebits~onepassword-ios/
iCloud~com~apple~iBooks/
iCloud~com~ustwo~monumentvalley2/
iCloud~com~apple~mobilesafari/
Aside from the com~apple~
-prefixed directories, both first-party and third-party apps appear in both formats. My guess is that the difference is legacy vs modern naming conventions, especially considering MomumentValley (2014) and MonumentValley2 (2017).
Backing up your documents
Now that we sort of understand the layout of ~/Library/Mobile Documents/
, where the hell are our iCloud Drive documents stored? Those live in com~apple~CloudDocs/
. If you cd
there, you should see all of the “custom”, non-app-specific files that you’ve stored in iCloud Drive. These should match what is viewable in Finder.
This is the directory that we want to backup. We can use rsync
to do that. (Side note: the way that rsync
handles paths is a bit odd. It doesn’t like relative paths, or ~
, or escaping spaces in directory names. Thus, this script uses absolute paths with spaces.) You just need to fill-in the USER
and DEST
variables.
#!/bin/bash
USER="<user>"
DEST="<destination dir>"
SRC="/Users/$USER/Library/Mobile Documents/com~apple~CloudDocs/"
rsync --verbose --recursive --delete-before --whole-file --times --exclude=".DS_Store" --exclude=".Trash/" "$SRC" "$DEST"
Explanation of the options:
--verbose
: increase verbosity--recursive
: recurse into directories--delete-before
: receiver deletes before transfer--whole-file
: copy files whole (without rsync algorithm)--times
: transfer modification times along with the files and update them on the remote system--exclude
: exclude files matching PATTERN
You can use the --dry-run
option to preview what would be transfered without actually executing rsync
.
This will backup only your iCloud Drive files. Any app-specific files (like Pages documents in com~apple~Pages/
) will not be included. If you want to backup any of those files, you will need to write a similar script with those target directories as the source. If you want to backup everything, specify the root ~/Library/Mobile Documents/
directory — but beware, this might be a lot of data. For example, iCloud~com~apple~iBooks/
contains all of your synced iBooks that were not purchased from the iBook Store. I have a lot of those.
That’s it! You can read the rsync
docs (man rsync
) for more details and options, but this should get you started.
Recommend
-
11
Backing Mac to Mac via scp / rsync Jul 22, 2019 I found myself turning my old Macbook Air over to my wife to use when the cursed butterfly keyboard on her new MacBook died. And even tho I had theoretically us...
-
11
Linux Tip Of The Day - Using rsync for Backup Feb 3, 2017 Once upon a time when I had complex file copying to handle, I used xcopy. It has been a lot of years since DOS and xcopy and as of la...
-
11
-
18
Note: The entire approach highlighted in this blog about using the rsync command with Ruby is only applicable on Linux-based systems and won't be available on Windows. In this article, we will ex...
-
12
Create bootable USB drive for ISO/WIM/IMG/VHD(x)/EFI files using VentoyVentoy is a free and open-source tool used to create bootable USB disks. There are many other applications that can create bootable disks but Ventoy comes with its sets of...
-
6
<?xml encoding="utf-8" ??>Introduction When using commands like scp or rsync with the public IP address of your server, you will automatically transfer the files over t...
-
5
Invisible files and Apple's Finder, tar, rsync, and GNU tar solouki macrumors 6502 Original poster
-
5
Siv Scripts Solving Problems Using Code I recently purchased a new computer and needed to tr...
-
5
In this post I describe how I used the azcopy command-line tool to backup some files to Azure blob storage. This is somewhat outside my comfort-zone, so I'm most posting it in the hope people will drop suggestions/advice in the commen...
-
5
Backing up to an external HDD using rsync I have a Linux-bas...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK