Skip to content

Instantly share code, notes, and snippets.

@huyz
Forked from Vartkat/buildresticexcludes.sh
Last active March 6, 2022 03:43
Show Gist options
  • Select an option

  • Save huyz/a3711154a5ac476f06240e13a148fcf6 to your computer and use it in GitHub Desktop.

Select an option

Save huyz/a3711154a5ac476f06240e13a148fcf6 to your computer and use it in GitHub Desktop.
Bash script to build a restic exclude list that mimics Apple TimeMachine exclude list
#!/bin/bash
# This script intend to mimic TimeMachine exclude list.
# As the exclude list can evolve between backups it has to be rebuilt before every backup
# Apple uses 5 types of excludes, four from the /System/Library/CoreServices/backupd.bundle/Contents/Resources/StdExclusions.plist file
# and files from applications where metadata says to not backup, these can be found usinf
# sudo mdfind "com_apple_backup_excludeItem = 'com.apple.backupd'"
SYSLOG=/usr/bin/syslog;
TEMPFILE=$1;
PLISTBUDDY=/usr/libexec/PlistBuddy;
MDFIND=/usr/bin/mdfind;
FIND=/usr/bin/find;
RESTIC=/usr/local/bin/restic;
ECHO=/bin/echo;
# Verify if PlistBuddy is installed if not exit
if [ ! -f $PLISTBUDDY ]; then
$SYSLOG -s -k Facility com.apple.console Level Error Sender ResticBackupUserScript Message "Can't find PlistBuddy"
exit 1
fi
# Excludes to keep directory structure without files
$FIND /private/var/log -type f > $TEMPFILE
$FIND /private/var/spool/cups -type f >> $TEMPFILE
$FIND /private/var/spool/fax -type f >> $TEMPFILE
$FIND /private/var/spool/uucp -type f >> $TEMPFILE
$ECHO -e "# PathsExcluded\n#" >> $TEMPFILE
$PLISTBUDDY /System/Library/CoreServices/backupd.bundle/Contents/Resources/StdExclusions.plist -c "Print PathsExcluded" | grep -v '{' |grep -v '}'| sed -e 's/^[[:space:]]*//' >> $TEMPFILE
$ECHO -e "#\n# ContentsExcluded\n#" >> $TEMPFILE
$PLISTBUDDY /System/Library/CoreServices/backupd.bundle/Contents/Resources/StdExclusions.plist -c "Print ContentsExcluded" | grep -v '{' |grep -v '}'| sed -e 's/^[[:space:]]*//' | sed -e 's/$/\/\*/' >> $TEMPFILE
$ECHO -e "#\n# UserPathsExcluded\n#" >> $TEMPFILE
$PLISTBUDDY /System/Library/CoreServices/backupd.bundle/Contents/Resources/StdExclusions.plist -c "Print UserPathsExcluded" | grep -v '{' |grep -v '}'| sed -e 's/^[[:space:]]*/\/Users\/\*\//' >> $TEMPFILE
$ECHO -e "#\n# Applications files excluded\n#" >> $TEMPFILE
$MDFIND "com_apple_backup_excludeItem = 'com.apple.backupd'" >> $TEMPFILE
# Exclude DocumentRevision files
$ECHO "/.DocumentRevisions-V100/" >> $TEMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment