-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_setup.sh
112 lines (91 loc) · 2.88 KB
/
node_setup.sh
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
#!/bin/bash
set -e
username=$1
echo "Node username: $username"
read -e -p "Please enter a node folder: " -i "node" folder
if [ ! -d "/home/$username" ]
then
useradd -d /home/$username -m $username
usermod -a -G sudo $username
fi
nodepath="/home/$username/$folder"
if [ ! -d "$nodepath" ]
then
read -p "Do you want to clone a git repository? (Y/n) " -r
if [[ ! $REPLY =~ ^[nN]$ ]]
then
read -p "Enter git url: " -i "https://" git
su -l $username -c "cd ~/ && git clone $git $folder && cd ~/$folder"
fi
fi
read -p "Do you want to rsync files from remote server? (y/N) " -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
read -p "Do you want generate public SSH key? (y/N) " -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
ssh-keygen -f ~/.ssh/id_rsa -q -P ""
cat ~/.ssh/id_rsa.pub
echo ""
echo "Please append the following key on the remote server"
echo "ie. \`nano ~/.ssh/authorized_keys\`"
echo "Press enter to continue..."
read
fi
read -e -p "Enter remote user login: " -i "root" remoteUser
read -p "Enter remote server ip: " remoteIp
read -e -p "Enter remote server port: " -i "22" remotePort
read -e -p "Enter remote path: " -i "$nodepath/" remotePath
if [ ! -d "$nodepath" ]
then
mkdir $nodepath
fi
rsync -vaz -e "ssh -p $remotePort" $remoteUser@$remoteIp:$remotePath $nodepath/
rm -rf $nodepath/node_modules/*
chown -R $username:$username $nodepath
else
if [ ! -d "$nodepath" ]
then
echo "Please create node files under: $nodepath"
exit
fi
remoteUser=root
remotePort=22
fi
read -p "Do you want restore database from remote server? (y/N) " -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
read -e -p "Enter remote user login: " -i "$remoteUser" remoteUser
read -e -p "Enter remote server ip: " -i "$remoteIp" remoteIp
read -e -p "Enter remote server port: " -i "$remotePort" remotePort
read -e -p "Enter db username: " -i $username dbUser
read -p "Enter db pasword: " dbPassword
read -e -p "Enter db name: " -i $username dbName
mongodump="mongodump -u $dbUser -p $dbPassword --db $dbName"
echo "dump database..."
ssh -p $remotePort $remoteUser@$remoteIp "cd /tmp && $mongodump && tar -cf dump.tar dump && rm -rf dump"
scp -P $remotePort $remoteUser@$remoteIp:/tmp/dump.tar /tmp/dump.tar
ssh -p $remotePort $remoteUser@$remoteIp "cd /tmp && rm -f dump.tar"
echo "extract database..."
rm -rf /tmp/dump
tar -xf /tmp/dump.tar -C /tmp
rm -f /tmp/dump.tar
echo "restore database..."
mongorestore --noIndexRestore /tmp/dump
rm -rf /tmp/dump
fi
read -p "Do you want to \`npm install\`? (Y/n) " -r
if [[ ! $REPLY =~ ^[nN]$ ]]
then
su -l $username -c "cd ~/$folder && npm install"
fi
read -p "Add startup script? (Y/n) " -r
if [[ ! $REPLY =~ ^[nN]$ ]]
then
read -e -p "Enter script: " -i "cd ~/$folder; sh ./run.sh" initScript
sed -i -e "\$i \su -l $username -c \"$initScript\"\n" /etc/rc.local
fi
echo "Done! Please configure node instance."
echo "Type 'exit' when you done."
echo ""
su $username