Scripting‎ > ‎

Backup Samba USER sids

posted Oct 21, 2016, 6:22 AM by Chris Franklin   [ updated Oct 21, 2016, 6:25 AM ]
Simple bash script 

Requirements
  • Bash
  • sqlite3
  • samba

samba_backup_users_sids.bash

#!/bin/bash


PDBEDIT=$(which pdbedit)
SQLITE=$(which sqlite3)

DB=/etc/samba/backup_users_sid.sqlite

function _create_db() {
 echo "CREATE TABLE users (uid INT primary key,sid);" | $SQLITE "${DB}"
}

if [ ! -e "${DB}" ]; then
 _create_db
fi

function _sid_lookup() {
 USERN=$1
 $PDBEDIT -L -v "${USERN}" | while IFS=":" read KEY VALUE; do
  case "${KEY}" in
   "User SID") echo "${VALUE// /}";;
  esac
 done
}

$PDBEDIT -L | while IFS=":" read USERN USERUID TRASH; do
 if [ "${USERN}" == "${USERN/\$/}" ]; then
  SID=$(_sid_lookup "${USERN}")
  echo "INSERT OR IGNORE INTO users (uid,sid) VALUES ('${USERUID}','${SID}');" | $SQLITE "${DB}"
 fi
done


Example restore

Example Restore


  if [ -e /etc/samba/backup_users_sid.sqlite ]; then
   SID=$(/usr/bin/sqlite3 /etc/samba/backup_users_sid.sqlite "SELECT sid FROM users WHERE uid=${uid}")
  else
   SID=""
  fi

  if [ -z "${SID}" ]; then
   (echo "${password}"; echo "${password}";) | /usr/bin/pdbedit -t -a -u "${username}" > /dev/null 2>&1
  else
   (echo "${password}"; echo "${password}";) | /usr/bin/pdbedit -t -a -U ${SID} -u "${username}" > /dev/null 2>&1
  fi




Comments