Advertisement
Guest User

Migrate_users.sh

a guest
Jun 10th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.64 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Command reference.
  4. #/opt/splunk-cluster/splunk/bin/splunk cmd python ./migrate-users.py --username admin --password passwd /tmp/migration/users/
  5.  
  6. #global variables
  7. #List of users
  8. USER_LIST=`ls -1 /mnt/pool2/search_head_pool/etc/users`
  9.  
  10. #Users home dirs
  11. source="/mnt/pool2/search_head_pool/etc/users"
  12. #splunk admin password so script can run
  13. admin_password="admin_password_here"
  14.  
  15. #dirs to store a copy of users dirs before migration
  16. base_dir="/mnt/backup/cluster-work/migration"
  17. in_progress="${base_dir}/inprogress"
  18. skipped="${base_dir}/skipped"
  19. complete="${base_dir}/complete"
  20. logs="${base_dir}/logs"
  21.  
  22. ######
  23. #MAIN#
  24. ######
  25.  
  26. do_staged_only="0"
  27. #skip ldap check and dir inprogress creation.
  28.  
  29.  
  30. if [ $do_staged_only  = 0 ]; then
  31.  
  32. #Check account in ldap. If it exists then copy files to staging.
  33. count=0
  34. for user in $USER_LIST
  35. do
  36.         found=` ldapsearch  -h 10.1.1.1 -E pr=10000/noprompt -D "CN=ldaplookupaccount" -w ldappassword -b "OU=People,OU=eProfile,DC=core,DC=dir,DC=splunk,DC=com" -L -x "CN=$user" | grep memberOf | grep Splunk | sort -u | wc -l`
  37.         if [ $found = 0 ]; then
  38.                 echo "$count: $user $found. Skipping user"
  39.                 cp -rp ${source}/$user ${skipped}
  40.         else
  41.                 echo "Staging ${user}"
  42.                 mkdir ${in_progress}/${user}
  43.                 cp -rp ${source}/${user} ${in_progress}/${user}
  44.         fi
  45.         ((count++))
  46. done
  47. echo "doing ldap"
  48.  
  49. fi
  50.  
  51. #Process all files in staging area and try splunk import script against specific user dir (dir has a double dir name so we can call the script once per user dir).
  52.  
  53. staged_users=`ls -1 ${in_progress}`
  54.  
  55. for staged in $staged_users
  56. do
  57.         mig_status=$( { /opt/splunk-cluster/splunk/bin/splunk cmd python ./migrate-users.py --username admin --password ${admin_password} $in_progress/$staged > $logs/$staged.log; } 2>&1 )
  58.         stanza_found=`cat $logs/$staged.log | grep -c Found`
  59.         stanza_wrote=`cat $logs/$staged.log | grep -c Wrote`
  60.         date=`/bin/date`
  61.         if [[ $mig_status == *"User does not exist"* ]]
  62.         then
  63.                 echo "$date,username=$staged,status=fail,message=User_does_not_existi,log_file=$logs/missing_user/${staged}.log"
  64.                 mv $logs/$staged.log $logs/missing_user/${staged}.log
  65.         else
  66.                 echo "$date,username=$staged,status=success,Found=$stanza_found,wrote=$stanza_wrote,log_file=$logs/$staged.log"
  67.         fi
  68.         if [ $stanza_wrote != 0 ]; then
  69.                 echo "move to complete $in_progress/$staged"
  70.                 echo "$staged .$stanza_wrote."
  71.                 #mv -v $in_progress/$staged $complete
  72.         fi
  73. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement