-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathuserMigration.sh
More file actions
executable file
·124 lines (99 loc) · 3.19 KB
/
userMigration.sh
File metadata and controls
executable file
·124 lines (99 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
CREDS='admin:admin'
HOST=localhost
PORT=4502
QUERYURL=/bin/querybuilder.json
DATE=$(date +%Y%m%d%H%M%S)
PACKAGENAMEUSERS="users-${DATE}"
PACKAGENAMEGROUPS="groups-${DATE}"
PARAMSUSERS='path=/home/users&property=jcr%3aprimaryType&property.value=rep%3aUser&p.limit=-1&p.hits=full'
PARAMSGROUP='path=/home/groups&property=jcr%3aprimaryType&property.value=rep%3aGroup&p.limit=-1&p.hits=full'
usage ()
{
echo "Usage: add type of Package you want to Export : user or group"
echo "e.g : ./userMigration user"
exit
}
statusCheck ()
{
if [[ "$2" != "true" ]];then
echo "$1 : Incorrect Curl command or Parameters . Please verify."
exit
fi
}
#Method to Grab the List of Users in the AEM instance.
listUsers ()
{
echo "Getting List of Users:"
OUTPUT=$(curl --write-out -s -u ${CREDS} "http://${HOST}:${PORT}${QUERYURL}?${PARAMSUSERS}")
STATUS=$(jq '.success' <<< "${OUTPUT}")
statusCheck "ListUsers" ${STATUS}
addFilters "${OUTPUT}" $1
}
#Method to Grab the List of Groups in the AEM instance.
listGroups ()
{
echo "Getting List of Groups:"
OUTPUT=$(curl --write-out -s -u ${CREDS} "http://${HOST}:${PORT}${QUERYURL}?${PARAMSGROUP}")
STATUS=$(jq '.success' <<< "${OUTPUT}")
statusCheck "ListGroups" ${STATUS}
addFilters "${OUTPUT}" $1
}
# Method to Create a package with the Given PackageName and Date Stamp.
createPackage ()
{
echo "Creating Package:"
OUTPUT=$(curl --write-out -s -u ${CREDS} \
"http://${HOST}:${PORT}/crx/packmgr/service/.json/etc/packages/users/$1.zip?cmd=create" \
-d packageName=$1 \
-d groupName=users)
STATUS=$(jq '.success' <<< "${OUTPUT}")
statusCheck "Create" ${STATUS}
}
# Method to add Filters to the created package.
addFilters ()
{
OUTPUT=$1 | jq .
RESULTS=$(jq '.results' <<< ${OUTPUT})
STATUS=$(jq '.success' <<< ${OUTPUT})
USERPATHS=$(jq '.hits[]."jcr:path"' <<< ${OUTPUT} | sed 's/"//g' )
USERNAMES=$(jq '.hits[]."rep:principalName"' <<< ${OUTPUT} | sed 's/"//g' )
ARRAYUSERPATHS=( $USERPATHS )
ARRAYUSERNAMES=( $USERNAMES )
FILTER="["
for ((i=0; i<${RESULTS}; i++)); do
FILTER="${FILTER}{\"root\":\"${ARRAYUSERPATHS[${i}]}\", \"rules\":[{\"modifier\":\"exclude\",\"pattern\":\"${ARRAYUSERNAMES[${i}]}/.tokens\"}]}, "
done
FILTER="${FILTER:0:${#FILTER}-2}]"
echo "Add Filter to Package"
OUTPUT=$(curl --write-out -s -u ${CREDS} \
"http://${HOST}:${PORT}/crx/packmgr/update.jsp" \
-F "path=/etc/packages/users/$2.zip" \
-F "packageName=$2" \
-F "groupName=users" \
-F "_charset_=UTF-8" \
-F "filter=${FILTER}")
STATUS=$(jq '.success' <<< "${OUTPUT}")
statusCheck "Filter" ${STATUS}
}
# Method to build a package created above.
buildPackage ()
{
echo "Build Package:"
OUTPUT=$(curl --write-out -s -u ${CREDS} -X POST \
"http://${HOST}:${PORT}/crx/packmgr/service/.json/etc/packages/users/$1.zip?cmd=build")
STATUS=$(jq '.success' <<< "${OUTPUT}")
statusCheck "Build" ${STATUS}
}
# Perform the actions
if [ "$1" = "user" ]; then
createPackage ${PACKAGENAMEUSERS}
listUsers ${PACKAGENAMEUSERS}
buildPackage ${PACKAGENAMEUSERS}
elif [ "$1" = "group" ] ; then
createPackage ${PACKAGENAMEGROUPS}
listGroups ${PACKAGENAMEGROUPS}
buildPackage ${PACKAGENAMEGROUPS}
else
usage
fi