postprefix
9/7/2013 - 5:23 PM

DeployStudio's "Freeze home directories content (restore at reboot)". The script that runs at first boot checks for any existing backups. If

DeployStudio's "Freeze home directories content (restore at reboot)". The script that runs at first boot checks for any existing backups. If none are found, it uses ditto (ditto --rsrc ) to create a copy of each home directory from /Users to /private/dss_homedirs_ref/. If there is an existing backup of a user in /private/dss_homedirs_ref/, the script uses rsync ( rsync -av --delete ) to restore the home folder.

(A word of caution here; If there is an existing backup and changes have been made to the user's home directory, the rsync command will delete the files that don't exist in the backup after a reboot.) By default, the Shared folder is not included in the backup and restore process.

#!/bin/sh

SCRIPT_NAME=`/usr/bin/basename "${0}"`

echo "${SCRIPT_NAME} - v1.0 ("`date`")"

# Defaults
BACKUP_FOLDER=/private/dss_homedirs_ref
INCLUDE_SHARED_FOLDER="NO"

# Functions
am_i_root() {
  if [ ${UID} != 0 ]
  then
    echo "  Sorry, only root can run this script !"
    exit 1
  fi
}

restore_user_homedirectory() {
  USER_SHORTNAME=${1}

  if [ -e "${BACKUP_FOLDER}/${USER_SHORTNAME}" ]
  then
  echo "  Restoring user home directory \"/Users/${USER_SHORTNAME}\""
    rsync -av --delete "${BACKUP_FOLDER}/${USER_SHORTNAME}" "/Users/"
    echo "  Restore complete"
  else
    if [ ! -e "${BACKUP_FOLDER}" ]
	then
      echo "  Creating backup folder \"${BACKUP_FOLDER}\""
      mkdir -p "${BACKUP_FOLDER}"
      chown root:wheel "${BACKUP_FOLDER}"
      chmod 755 "${BACKUP_FOLDER}"
    fi
    if [ -e "/Users/${USER_SHORTNAME}" ]
	then
	  echo "  Backing up user home directory \"/Users/${USER_SHORTNAME}\""
      ditto --rsrc "/Users/${USER_SHORTNAME}" "${BACKUP_FOLDER}/${USER_SHORTNAME}"
	  echo "  Backup complete"
    fi
  fi	
}

# main
am_i_root

if [ "${1}" == "--include-shared-folder" ]
then
  INCLUDE_SHARED_FOLDER="YES"
fi

for HOME_DIRECTORY_PATH in /Users/*
do
  HOME_DIRECTORY=`basename "${HOME_DIRECTORY_PATH}"`
  if [ "${HOME_DIRECTORY}" != "Shared" ] || [ "${INCLUDE_SHARED_FOLDER}" == "YES" ]
  then
    restore_user_homedirectory "${HOME_DIRECTORY}"
  fi
done

exit 0