Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripts to backup and restore settings modified by .osx #58

Closed
wants to merge 9 commits into from
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ Thumbs.db

# Files that might appear on external disks
.Spotlight-V100
.Trashes
.Trashes

# OSX system settings backup file
.osx_settings_backup
48 changes: 48 additions & 0 deletions osx_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

function process () {
# Create a backup file if one doesn't exists
if [[ ! -e '.osx_settings_backup' ]]; then
echo "Backup Settings" > .osx_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 .osx
settings=`egrep "^defaults (-currentHost )?write [[:alnum:][:punct:]]+ [[:alnum:][:punct:]]+ (-array |-bool |-int |-string |-dict-add |-array-add )?.*" .osx | 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 OSX settings array - storing the current value in .osx_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 >> .osx_settings_backup
else
echo $c ": default" >> .osx_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 .osx_settings_backup"
}

process

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

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

echo "Restoring backup..."

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

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

# Iterate over the OSX settings array - storing the current value in .osx_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 OSX 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