forked from mathiasbynens/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from wbontrager/backup-and-restore
Add Backup and Restore Scripts
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |