How can I restore iBooks stored in iCloud from a Time Machine backup? I found that many of my books and PDFs had been removed from iBooks. 

I'm not sure exactly how it happened but recently I had both used the Storage Management app (to remove a few specific books) and turned on syncing on a new Mac that already had some books stored locally in iBooks. 

Anyway, I can see that my remaining books are stored in /Users/USERNAME/Library/Mobile Documents/iCloud~com~apple~iBooks/.

If I run open /Users/USERNAME/Library/Mobile Documents/iCloud~com~apple~iBooks/ and then try to open Time Machine, I'm just brought to a Time Machine window for iCloud Drive and it appears empty, with no iBooks entry.

If I try to go to the specific folder in Time Machine (with cmd+shift+g) I'm just dropped back at the iCloud Drive folder again.

So how can I (1) check if my books were backed up by Time Machine and (2) restore them?
I found this was possible use the command line tmutil.

First, I checked that the files were in my backup:

$ tmutil listbackups | tail -n 3
/Volumes/Time Machine Backups/Backups.backupdb/macbook/2017-12-31-123506
/Volumes/Time Machine Backups/Backups.backupdb/macbook/2017-12-31-212807
/Volumes/Time Machine Backups/Backups.backupdb/macbook/2017-12-31-224355


Check the iBooks folder was backed up

$ ls '/Volumes/Time Machine Backups/Backups.backupdb/macbook/2017-12-31-224355/Macintosh HD/Users/USERNAME/Library/Mobile Documents/iCloud~com~apple~iBooks'
Data            Documents       metadata.nosync
$ ls '/Volumes/Time Machine Backups/Backups.backupdb/macbook/2017-12-31-224355/Macintosh HD/Users/USERNAME/Library/Mobile Documents/iCloud~com~apple~iBooks/Documents' | wc -l
402
$ ls '/Volumes/Time Machine Backups/Backups.backupdb/macbook/2017-12-31-224355/Macintosh HD/Users/USERNAME/Library/Mobile Documents/iCloud~com~apple~iBooks/Documents' | tail -n 3
ruby.epub
wp169en.pdf
wp196en.pdf


Restore to a temporary folder:

$ mkdir /tmp/ibooks/
$ tmutil restore '/Volumes/Time Machine Backups/Backups.backupdb/macbook/2017-12-31-224355/Macintosh HD/Users/USERNAME/Library/Mobile Documents/iCloud~com~apple~iBooks' /tmp/ibooks/
Total copied: 1518.18 MB (1591923972 bytes)
Items copied: 10006


I then made sure iBooks.app was closed (and on iOS devices too) and synchronised the restored files into the right place:

$ cd /tmp/ibooks/
$ rsync -av ./ '/Users/USERNAME/Library/Mobile Documents/iCloud~com~apple~iBooks/'
building file list ... done
[...]
sent 1539459615 bytes  received 196218 bytes  19366740.04 bytes/sec
total size is 1591923972  speedup is 1.03


I then opened iBooks.app and my books were back! iBooks spent 30 mins or so uploading to iCloud, and the books are also once again available on my iCloud devices.

Note, I attempted to directly restore from the backup to the target directory, but it failed, hence use of the temp directory and rsync.

$ tmutil restore '/Volumes/Time Machine Backups/Backups.backupdb/macbook/2017-12-31-224355/Macintosh HD/Users/USERNAME/Library/Mobile Documents/iCloud~com~apple~iBooks' '/Users/USERNAME/Library/Mobile Documents/'
/Users/USERNAME/Library/Mobile Documents/iCloud~com~apple~iBooks: The operation couldn’t be completed. File exists (error 17)