Skip to content

Instantly share code, notes, and snippets.

@sirlancelot
Created March 20, 2011 23:45
Show Gist options
  • Save sirlancelot/878794 to your computer and use it in GitHub Desktop.
Save sirlancelot/878794 to your computer and use it in GitHub Desktop.
migrade user credentials
#!/bin/bash
#############################
# Migrate User Credentials
#
# @author Matthew Pietz
Mode=0
BackupPath="/tmp/user-mig.$$"
StorageFile="user-migrate.tar.gz"
StartID=1000
declare -i StartID
function Startup() {
if [ $(id -u) -ne 0 ]; then
echo "must be root." >&2
exit 1
fi
mkdir -p "$BackupPath"
}
function Cleanup() {
rm -rf "$BackupPath"
}
function CreateArchive() {
awk -v LIMIT=$StartID -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd > "$BackupPath/passwd.mig"
awk -v LIMIT=$StartID -F: '($3>=LIMIT) && ($3!=65534)' /etc/group > "$BackupPath/group.mig"
awk -v LIMIT=$StartID -F: '{print $1}' "$BackupPath/passwd.mig" | egrep -f - /etc/shadow > "$BackupPath/shadow.mig"
cp /etc/gshadow "$BackupPath/gshadow.mig"
# use ls so that we don't get `./` in the archive
ls "$BackupPath" | tar -C "$BackupPath/" -czpf "$(pwd)/$StorageFile" -T -
if [ $? -ne 0 ]; then
echo "errors while creating file..."
rm "$(pwd)/$StorageFile"
Cleanup
exit 2
else
echo "created file: $StorageFile"
Cleanup
fi
}
function ApplyArchive() {
echo "TODO: Apply archive on new computer" >&2
exit 1
}
function Usage() {
sed -e 's/^ //' <<EndUsage
Usage: ${0##*/} [-i LIMIT] -s [FILE]
or: ${0##*/} -a [FILE]
-i LIMIT Set the start index for UID and GID
Default: 1000
-s Store credentials in [FILE]
-a Read and apply credentials from [FILE]
EndUsage
}
function CheckMode() {
if [ $Mode -gt 0 ]; then
echo "Only one of -s or -a can be specified" >&2
exit 1
fi
Mode=$1
}
#################################################
Startup
while getopts ":i:sa" Opt; do case $Opt in
i) StartID=$OPTARG;;
s) CheckMode 1;;
a) CheckMode 2;;
\?)
echo "Invalid option -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument" >&2
exit 1
;;
esac; done
shift $((OPTIND-1))
# Set the storage file to use
if [ "$1" ]; then StorageFile="$1"; fi
case $Mode in
1) CreateArchive;;
2) ApplyArchive;;
*) Usage;;
esac
Cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment