Skip to content

Instantly share code, notes, and snippets.

@ravihara
Created November 10, 2017 10:04
Show Gist options
  • Save ravihara/8be8bd1a736770390e7d2fc2224ca3a1 to your computer and use it in GitHub Desktop.
Save ravihara/8be8bd1a736770390e7d2fc2224ca3a1 to your computer and use it in GitHub Desktop.
Backup user accounts from a linux system (non-system users)
#!/bin/bash -e
# Backup user accounts from a linux system (non-system users)
UGIDLIMIT=1000
MAXLIMIT=29999
# Create a directory to backup
MIGDIR="/tmp/backuinfo"
mkdir -p $MIGDIR && chmod 750 $MIGDIR
# Get password
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=$MAXLIMIT)' /etc/passwd > $MIGDIR/passwd.mig
# Get group
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=$MAXLIMIT)' /etc/group > $MIGDIR/group.mig
cp /etc/gshadow $MIGDIR/gshadow.mig
# Get password
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=$MAXLIMIT) {print $1}' /etc/passwd | tee - | egrep -f - /etc/shadow > $MIGDIR/shadow.mig
echo "Backed up user information to '$MIGDIR'. Please save it at a safe place."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment