Skip to content

Instantly share code, notes, and snippets.

@patmaddox
Created May 5, 2026 16:08
Show Gist options
  • Select an option

  • Save patmaddox/6112de75cfa21d5a45e217d774fef50e to your computer and use it in GitHub Desktop.

Select an option

Save patmaddox/6112de75cfa21d5a45e217d774fef50e to your computer and use it in GitHub Desktop.
syncbe.sh - sync users, groups, and files from one root to another
#!/bin/sh
set -e
sync_users()
{
pw usershow -n root | cut -f 2 -d ':' | pw -R ${root} usermod -n root -H 0
for u in $(cat ${config}/users); do
if pw -R ${root} usershow -n ${u} > /dev/null 2>&1; then
pw -R ${root} userdel -n ${u}
fi
pw usershow -n ${u} >> ${root}/etc/master.passwd
done
pwd_mkdb -p -d ${root}/etc ${root}/etc/master.passwd
}
sync_groups()
{
for g in $(cat ${config}/groups); do
if ! pw -R ${root} groupshow -n ${g} > /dev/null 2>&1; then
gid=$(pw groupshow -n ${g} | sed -E 's/^.*:.*:([[:digit:]]+):*/\1/')
pw -R ${root} groupadd -n ${g} -g ${gid}
fi
users=$(pw groupshow -n ${g} | sed 's/.*://')
pw -R ${root} groupmod -n ${g} -m ${users}
done
}
sync_keepfiles()
{
for f in $(cat ${config}/keepfiles); do
if [ -f ${f} ]; then cp ${f} ${root}${f}; fi
done
}
main() {
local root=$(dirname $(realpath $0))
local config=${root}/config
sync_users
sync_groups
sync_keepfiles
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment