安全服务脚本

系统:

centos6-7
mysql版本:5.6.29 jdk:1.8
文中ftp为假设,需要的包下载地址:http://note.youdao.com/noteshare?id=4c11cfbc269ef5836af270ae30838987&sub=A505D5FE5B3E4FF3B68E0E77AE0D1A90,根据情况可将该地址下的包上传到你的ftp中,方便下载即可,或者采用其他方式,因人而异。

#!/bin/bash
##############################################
#初始化脚本使用规范:
#执行:init_install.sh    $1
#$1= jdk、node、mysql、redis、htop、rabbitmq、mongodb、zookeeper  和 tomcat
#其中mysql的data放在/opt/App/mysql/下,tomcat的webapps放在/opt/App/tomcat/下;
#tomcat=项目名-端口
#项目名:自定义
#默认端口分别:8080 8005 8009 8443
#client_port:自定义的四位数(注:只能是四位数!)
#shutdown_port:client_port+1001
#ajp_port: client_port+1002
#https_port: client_port+1003
##############################################
    
export LANG=zh_CN.UTF-8
#Overall 
package_dir="/opt/Package"
install_path="/opt/Service"
ftp_downloadurl="http://WFF_FFW/soft"
app_url="/opt/App"
service_name="$1"
#rabbitmq
erlang_install_package="otp-service.tar.gz"
rabbitmq_install_package="rabbitmq-service.tar.gz"
#mongodb
mongodb_install_package="mongodb-service.tar.gz"
#tomcat8
tomcat_install_package="tomcat-service.tar.gz"
#jdk1.8
jdk8_install_package="jdk-service.tar.gz"
#node8
node_install_package="node-service.tar.gz"
#mysql5629
mysql_install_package="mysql.package.tar.gz"
#redis3
redis3_install_package="redis-service.tar.gz"
#htop
htop_install_package="epel-release-5-4.noarch.rpm"
#zookeeper
zookeeper_install_package="zookeeper-service.tar.gz"

mkdir_path(){

    /bin/mkdir -p /opt/{App,Script,Package,Backup,Service}
}

jdk() {
cd ${package_dir}
    yum install wget -y
if [ ! -f "$jdk_install_package" ]; then
    wget $ftp_downloadurl/$jdk8_install_package
fi

/bin/tar -zxf $package_dir/$jdk8_install_package -C $install_path
/bin/mv $install_path/$service_name-service  $install_path/$service_name

echo -e "JAVA_HOME=$install_path/$service_name
JRE_HOME=$install_path/$service_name/jre
PATH=\$PATH:\$JAVA_HOME/bin:\$JRE_HOME/bin:\$HOME/bin:/sbin:/usr/bin:/usr/sbinexport:/usr/local/sbin:/usr/local/bin:/bin:/usr/sbin:/root/bin
CLASSPATH=.:\$JAVA_HOME/lib/jt.jar:\$JAVA_HOME/lib/tools.jar:\$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH" >> /etc/profile

source /etc/profile
java -version

}

node(){
cd $package_dir
yum -y install wget
if [ ! -f "$node_install_package" ];then
    wget $ftp_downloadurl/$node_install_package
fi
/bin/tar -xf $node_install_package -C $install_path
/bin/mv $install_path/$service_name-service $install_path/$service_name
echo -e " export NODE_HOME=\"$install_path/$service_name\"
export PATH=\"\$PATH:\$NODE_HOME/bin\"
export NODE_PATH=\"\$NODE_HOME/lib/node_modules\"" >> /etc/profile
. /etc/profile
 $install_path/$service_name/bin/npm install forever -gd


}

mysql() {
cd ${package_dir}
yum install gcc-c++ wget ncurses-devel perl-Module-Install.noarch libtool openssl-devel make -y

if [ ! -f "$mysql_install_package" ]; then
        wget $ftp_downloadurl/$mysql_install_package 
fi

/bin/tar -zxf $mysql_install_package -C $install_path

groupadd mysql

/usr/sbin/useradd -g mysql -s /sbin/nologin -M mysql

chown mysql.mysql -R $install_path/$service_name

/bin/mkdir -p $app_url/$service_name
/bin/mv $install_path/$service_name/data   $app_url/$service_name

$install_path/$service_name/scripts/mysql_install_db --user=mysql --basedir=$install_path/mysql --datadir=$app_url/mysql/data

rm -rf /etc/my.cnf

cp $install_path/$service_name/support-files/mysql.server /etc/init.d/mysqld

sed -i '46s/$/\/opt\/Service\/mysql/g' /etc/init.d/mysqld
sed -i '47s/$/\/opt\/App\/mysql\/data/g' /etc/init.d/mysqld
sed -i '263s/datadir/basedir/g' /etc/init.d/mysqld

echo "[mysqld]
basedir=$install_path/mysql
datadir=$app_url/mysql/data
socket=$install_path/mysql/mysqld.sock
user = mysql
port = 3006
server_id = 1
log-bin = mysql-bin
log_bin_index = binlog.index
character_set_server = utf8
lower_case_table_names = 1
#binlog_ignore_db = mysql
#replicate-do-db = mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
innodb_file_per_table=1
#federated
#general_log=ON
#general_log_file=/opt/Service/mysql/mysql_general.lo
max_connections=1000
wait_timeout=300
interactive_timeout=300
key_buffer_size=128M  
query_cache_size=256M
query_cache_type=1
max_connect_errors=10
max_allowed_packet=32M
innodb_thread_concurrency = 8
innodb_log_buffer_size=32M
innodb_log_file_size = 50M
innodb_log_files_in_group = 3
read_buffer_size = 1M
bulk_insert_buffer_size = 64M

[mysql.server]
character_set_server = utf8
socket=$install_path/mysql/mysqld.sock

[client]
socket=$install_path/mysql/mysqld.sock
#character_set_server = utf8
default-character-set = utf8
user = zabbix
password = 'zabbix'

[mysqld_safe]
log-error=$install_path/mysql/mysqld.errorlog
pid-file=$install_path/mysql/localhost.localdomain.pid
character_set_server = utf8

[mysql]
socket=$install_path/mysql/mysqld.sock
#character_set_server = utf8
default-character-set = utf8

[mysqldump]
socket=$install_path/mysql/mysqld.sock
default-character-set = utf8

[mysqladmin]
socket=$install_path/mysql/mysqld.sock
#character_set_server = utf8" > $install_path/mysql/my.cnf

echo -e "export MYSQL_HOME=\"$install_path/mysql\"
export PATH=\"\$PATH:\$MYSQL_HOME/bin\"" >> /etc/profile

. /etc/profile

echo "mysql -V"
$install_path/mysql/bin/mysql -V
sleep 5

/etc/init.d/mysqld start

echo "Please Set Your Mysql Password :"
read PASSWORD

echo -e "${red_col}Please Enter \"Enter\" direcity${reset_col}"
export MYSQL_HOME="/opt/Service/mysql"
$install_path/mysql/bin/mysql -uroot -p <<EOF
delete from mysql.user where Host='::1';
delete from mysql.user where Host='localhost.localdomain';
delete from mysql.user where User='';
update mysql.user set password=password("$PASSWORD") where user="root";
grant all privileges on *.* to 'root'@'%' identified by "$PASSWORD";
flush privileges;
select user,password,host from mysql.user;
EOF
#mysql -uroot -p -e "delete from mysql.user where Host='::1';"
#mysql -uroot -p -e "delete from mysql.user where Host='localhost.localdomain';"
#mysql -uroot -p -e "delete from mysql.user where User='';"
#mysql -uroot -p -e "update mysql.user set password=password("$PASSWORD") where user="root";"
#mysql -uroot -p -e "grant all privileges on *.* to 'root'@'%' identified by "$PASSWORD";"
#mysql -uroot -p -e "flush privileges;"
#mysql -uroot -p -e "select user,host,password from mysql.user;"

netstat -ntpl |grep "3306"

}


redis() {

cd ${package_dir}
yum install wget make gcc-c++ -y

if [ ! -f "$redis3_install_package" ]; then 
    wget $ftp_downloadurl/$redis3_install_package 
fi

/bin/tar -zxf $redis3_install_package -C $install_path

/bin/mv $install_path/$service_name-service  $install_path/$service_name
cd $install_path/$service_name

make

sed -i '61s/127.0.0.1/0.0.0.0/g' $install_path/$service_name/redis.conf
sed -i '128s/no/yes/g' $install_path/$service_name/redis.conf
sed -i '593s/no/yes/g' $install_path/$service_name/redis.conf
sed -i '481s/^/requirepass redis/g' $install_path/$service_name/redis.conf

echo -e "export REDIS_HOME=\"$install_path/$service_name\"
export PATH=\"\$PATH:\$REDIS_HOME/src\"" >> /etc/profile

cd /root

source /etc/profile

redis-server -v

redis-server $install_path/$service_name/redis.conf

netstat -ntpl |grep "6379"

echo "
shell tool
#!/bin/bash
redis-cli -h 127.0.0.1 -n 0 keys "*" | while read key
do
    redis-cli -h 127.0.0.1 -n 0 --raw dump \$key | perl -pe 'chomp if eof' | redis-cli -h 123.123.123.123 -n 0 -x restore \$key 0
    echo "migrate key \$key"
done
"

netstat -ntpl |grep "6379"
echo "${red_col}Redis Password is \"redis\"${reset_col}"

}

htop() {

cd ${package_dir}

yum install wget -y

if [ ! -f "$htop_install_package" ]; then
       wget $ftp_downloadur/$htop_install_package
fi

/bin/rpm -ivh  $htop_install_package

/bin/rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL

yum install htop -y  
}


rabbitmq() {

cd ${package_dir}
yum install perl gcc-c++ mesa* freeglut* wget ncurses-devel openssl-devel unixODBC-devel zip unzip xmlto httpd -y

if [ ! -f "$erlang_install_package" ]; then
        wget $ftp_downloadurl/$erlang_install_package 
fi


if [ ! -f "$rabbitmq_install_package" ]; then
        wget  $ftp_downloadurl/$rabbitmq_install_package 
fi

/bin/tar -zxf $erlang_install_package
cd   otp-service
./configure --prefix=$install_path/erlang --without-javac
make
make install

echo -e "export ERL_HOME=\"/opt/Service/erlang\"  
export PATH=\"\$PATH:\$ERL_HOME/bin\"" >> /etc/profile

source /etc/profile

cd $package_dir

chmod +x $rabbitmq_install_package
tar -zxf $rabbitmq_install_package
cd $service_name-service
make
make install TARGET_DIR=$install_path/mq/rabbitmq SBIN_DIR=$install_path/mq/rabbitmq/sbin MAN_DIR=$install_path/mq/rabbitmq/man

cd $install_path/mq/rabbitmq/sbin/
mkdir /etc/rabbitmq/ 

./rabbitmq-plugins enable rabbitmq_management 

./rabbitmq-server start &

}

mongodb() {

cd ${package_dir}
yum install wget -y

if [ ! -f "$mongodb_install_package" ]; then
    wget  $ftp_downloadurl/$mongodb_install_package 
    fi

/bin/tar -zxf $mongodb_install_package -C $install_path

/bin/mv $install_path/$service_name-service $install_path/$service_name

/bin/mkdir $install_path/mongodb/data
/bin/mkdir $install_path/mongodb/logs

echo -e "export MONGODB_HOME=\"/opt/Service/mongodb\"
export PATH=\"\$PATH:\$MONGODB_HOME/bin\"" >> /etc/profile

. /etc/profile

mongod --port 27017 --fork --dbpath=$install_path/mongodb/data/ --logpath=$install_path/mongodb/logs/mongodb.log --logappend  > /dev/null 2>&1


echo "mongod --port 27017 --fork --dbpath=$install_path/mongodb/data/ --logpath=$install_path/mongodb/logs/mongodb.log --logappend  > /dev/null 2>&1 " > $install_path/mongodb/start.sh

echo "killall mongod" > $install_path/mongodb/stop.sh

chmod +x $install_path/mongodb/*.sh

echo "#!/bin/bash
#chkconfig 80 12 

start(){
netstat -nutpl |grep mongod
if [ \$? -eq 1 ];then
   echo \"Mongodb is running\"
   $install_path/mongodb/bin/mongod --port 27017 --fork --dbpath=$install_path/mongodb/data/ --logpath=$install_path/mongodb/logs/mongodb.log --logappend   > /dev/null 2>&1
fi
}

stop(){
netstat -nutpl |grep mongod
if [ \$? -eq 0 ];then
        killall mongod
echo \"Mongodb stoped\"
fi
}

status(){
STATUS=\`netstat -nutpl |grep mongod |wc -l\`
if [ \$STATUS -eq 0 ];then
        echo \"Mongodb stoped\"
    else
        echo \"Mongodb running\"
fi
}

case \"\$1\" in
start)
start
;;
restart)
stop
sleep 3
start
;;
status)
status
;;
stop)
stop
;;
*)
echo \"Usage: \$0 {start|stop|status|restart}\"

esac
exit" > /etc/init.d/mongod

chmod +x /etc/init.d/mongod

}

zookeeper(){

cd ${package_dir}
yum install wget -y

if [ ! -f "$zookeeper_install_package" ]; then
    wget  $ftp_downloadurl/$zookeeper_install_package
    fi

/bin/tar -zxf $zookeeper_install_package -C $install_path

echo "
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
dataLogDir=$install_path/$service_name-service/logs
clientPort=2181" > $install_path/$service_name-service/conf/zoo.cfg

$install_path/$service_name-service/bin/zkServer.sh start 

#$install_path/$service_name-service/bin/zkCli.sh -server localhost:2181

}


install_tomcat(){
client_port=`echo ${service_name:0-4:4}`
shutdown_port=$((10#${client_port}+1001))
ajp_port=$((10#${client_port}+1002))
https_port=$((10#${client_port}+1003))

/bin/mkdir -p $app_url/$service_name
cd ${package_dir}

if [ ! -f "$tomcat_install_package" ]; then
       wget $ftp_downloadurl/$tomcat_install_package
fi
    /bin/tar -zxf $tomcat_install_package -C $install_path

    /bin/mv -f $install_path/tomcat-service  $install_path/$service_name
    /bin/mv $install_path/$service_name/webapps  $app_url/$service_name/
    sed -i "s/webapps/\/opt\/App\/$service_name\/webapps/g"    $install_path/$service_name/conf/server.xml
    sed -i "s/8080/$client_port/g" $install_path/$service_name/conf/server.xml
    sed -i "s/8005/$shutdown_port/g" $install_path/$service_name/conf/server.xml
    sed -i "s/8009/$ajp_port/g" $install_path/$service_name/conf/server.xml
    sed -i "s/8443/$https_port/g" $install_path/$service_name/conf/server.xml

}

list_name="jdk node mysql redis htop rabbitmq mongodb zookeeper"

if [[ "$list_name" =~ "$1" ]]
then
        mkdir_path
        ${1}
   elif  [[ $1 == *-* ]];then
        mkdir_path
        install_tomcat
   else
        echo "please init_install.sh jdk|node|mysql|redis|htop|rabbitmq|mongodb|zookeeper"

fi

猜你喜欢

转载自blog.csdn.net/weixin_33973600/article/details/87558349