Skip to content

Commit

Permalink
Merge pull request #4 from wbontrager/backup-and-restore
Browse files Browse the repository at this point in the history
Add Backup and Restore Scripts
  • Loading branch information
wbontrager authored Jun 6, 2019
2 parents 7f234a0 + 5b0bb25 commit b27e5b1
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
50 changes: 50 additions & 0 deletions backup/macos_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

# Source: https://github.com/mathiasbynens/dotfiles/pull/58

function process () {
# Create a backup file if one doesn't exists
if [[ ! -e '.macos_settings_backup' ]]; then
echo "Backup Settings" > .macos_settings_backup
else
# If a backup already exists, the script exits to prevent
# the backup being overwrittern with the modified values
echo "Backup already created. Cancelling operation..."
exit
fi

echo "Creating backup..."

# extract the settings being modified by .macos
settings=`egrep "^defaults (-currentHost )?write [[:alnum:][:punct:]]+ [[:alnum:][:punct:]]+ (-array |-bool |-int |-string |-dict-add |-array-add )?.*" ../.macos | sed "s/write/read/" | sed "s/\(defaults\)\(.*\)\(read [a-zA-Z[:punct:] ]*\) -.*/\1\2\3/"`

# Bash workaround to enable iteration over array values that have whitespace
oldifs=$IFS
IFS=$(echo -en "\n\b")

# Iterate over the settings array - storing the current value in .macos_settings_backup
for s in ${settings[*]}; do
t=`eval "$s 2>/dev/null"`

# replace the action with a placeholder
# to easily swap it out during settings restore
c=`echo $s | sed "s/ read / :action: /"`
if [[ $t ]]; then
echo $c ":" $t >> .macos_settings_backup
else
echo $c ": default" >> .macos_settings_backup
fi
done
IFS=$oldifs

# Backup Launchpad DB
for file in ~/Library/Application\ Support/Dock/*.db; do
cp "$file" "$file.bak";
done

echo "Settings backed up to .macos_settings_backup"
}

process

exit
77 changes: 77 additions & 0 deletions backup/macos_restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

# Source: https://github.com/mathiasbynens/dotfiles/pull/58

function restore () {
# Check that there are backup settings to restore from
if [[ ! -e ".macos_settings_backup" ]]; then
echo "Cannot find backup. Exiting..."
exit
fi

echo "Restoring backup..."

# extract the backup settings from .macos_settings_backup
settings=`egrep "^defaults (-currentHost )?:action: [[:alnum:][:punct:]]+ [[:alnum:][:punct:]]+" .macos_settings_backup`

# Bash workaround to enable iteration over array values that have whitespace
oldifs=$IFS
IFS=$(echo -en "\n\b")

# Iterate over the settings array - storing the current value in .macos_settings_backup
for s in ${settings[*]}; do

# get the value
value=`echo $s | sed "s/.* : //"`

# get the command
com=`echo $s | sed "s/ : .*//"`

# if the backed up setting is 'default', the modified setting is removed which
# causes macOS to revert to the default settings
if [[ $value = 'default' ]]; then
com=`echo $com | sed "s/ :action: / delete /"`
eval "$com"
else
com=`echo $com | sed "s/ :action: / write /"`
eval "$com '$value'"
fi
unset value
done
IFS=$oldifs
unset oldifs

# Don't show item info below desktop icons
/usr/libexec/PlistBuddy -c "Set :DesktopViewSettings:IconViewSettings:showItemInfo false" ~/Library/Preferences/com.apple.finder.plist

# Disable snap-to-grid for desktop icons
/usr/libexec/PlistBuddy -c "Set :DesktopViewSettings:IconViewSettings:arrangeBy kind" ~/Library/Preferences/com.apple.finder.plist

# Hide the ~/Library folder
chflags hidden ~/Library

# Enable local Time Machine backups
echo "Enabling Time Machine... (may require password or ctrl + c to skip)"
sudo tmutil enablelocal

# Restore Dropbox’s green checkmark icons in Finder
file=/Applications/Dropbox.app/Contents/Resources/check.icns
[ -e "$file.bak" ] && mv -f "$file.bak" "$file" $$ rm -f "$file.bak"
unset file

# Backup Launchpad DB
for file in ~/Library/Application\ Support/Dock/*.db.bak; do
# copy the backup andremove .bak
cp "$file" "${file//.bak/}"
rm -f "$file"
done

# Kill affected applications
for app in Finder Dock Mail Safari iTunes iCal Address\ Book SystemUIServer; do killall "$app" > /dev/null 2>&1; done

echo "Backup restored."
}

restore

exit

0 comments on commit b27e5b1

Please sign in to comment.