shell script – auto install 範本過程

這篇放著 auto install shell script 範本,有可能會隨時修改,當作記錄,以下是我自已參考別人程式碼進行修改的範本

Custom shell script – 0902

此自動安裝檔內容加入以下內容

  • add install mysql
  • add import mysql big data
  • add show status progress in Linux Shell Scripts

主要程式

#apt-get install
echo Asia/Taipei > /etc/timezone
sudo dpkg-reconfigure --frontend noninteractive tzdata
echo -e "Start installation...\n"
sudo apt-get update
sudo apt install -y debconf-utils
echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections
echo iptables-persistent iptables-persistent/autosave_v6 boolean false | sudo debconf-set-selections
sudo apt-get -f  install
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password welcome1xx'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password welcome1xx'
sudo apt-get install -y mysql-server
sudo apt-get install -y iptables-persistent snmp snmpd python python-pip awscli htop nload lrzsz
sudo pip install pytz
sudo dpkg -i /root/mysql_deb/x64/mysql-server-core-5.5_5.5.52-0ubuntu0.14.04.1_amd64.deb
sudo dpkg -i /root/mysql_deb/x64/mysql-server-5.5_5.5.52-0ubuntu0.14.04.1_amd64.deb
#P2PHOSTS="/etc/hosts"
sleep 3
echo -e "Start Other installation...\n"
mkdir -p /var/prtg/scripts/
mkdir -p /var/prtg/scriptsxml/
CONF="/root/sys_config"
cp -rf ${CONF}/snmpd.conf /etc/snmp/
cp -rf ${CONF}/sysctl.conf /etc/
/sbin/sysctl -p
cp -rf ${CONF}/limits.conf /etc/security/
cp -rf ${CONF}/iotc.conf /etc/init
CONFPRTG="/root/prtg"
cp -rf ${CONFPRTG}/Get-nf_conntrack_count.py /var/prtg/scripts/
cp -rf ${CONFPRTG}/current_sessions.sh /var/prtg/scripts/
cp -rf ${CONFPRTG}/CustomUsedSpace.sh /var/prtg/scripts/
cp -rf ${CONFPRTG}/mysql_replication_status.py /var/prtg/scripts/
cp -rf ${CONFPRTG}/ServiceIndicator.sh /var/prtg/scripts/
cp -rf ${CONFPRTG}/202*.sh /var/prtg/scriptsxml/
cp -rf ${CONFPRTG}/all.sh /var/prtg/scriptsxml/
cp -rf ${CONFPRTG}/mlb.sh /var/prtg/scriptsxml/
/bin/systemctl enable iotc
echo "nf_conntrack_ipv4" >> /etc/modules
echo "nf_conntrack" >> /etc/modules
./firewall.sh
/sbin/iptables -L -vn
/sbin/iptables-save > /etc/iptables/rules.v4
echo -e "add ip myrealip..\n"
IOTC="/root/IOTC_Master-/"
cd ${IOTC}
./myrealip-pub.sh
 
IP=127.0.0.1
HOST=`cat /etc/mysql/my.cnf |grep 127.0.0.1|  awk  '{ print $2 }'`
sed -i 's/127\.0\.0\.1/0\.0\.0\.0/g' /etc/mysql/my.cnf
echo "$1" > /etc/hostname
###
/bin/mv /root/master-install-190829-3000.sh /root/script/
/usr/bin/crontab -l >cron.tmp
echo '00 15 * * *  /root/compress_log.sh > /dev/null' >>cron.tmp
/usr/bin/crontab cron.tmp
/bin/rm cron.tmp
#DB download"
SQL="/root/mysql_sql/"
cd ${SQL}
mysql -u root -pxxx  <iotc.sql
wget https://s3.amazonaws.com/xxxx/MySQL_Master/P2PDB_xxxxxxx.tgz
tar -zxvf P2PDB_xxxxxxxx.tgz
#Import databases
imeron=`date`
echo "Import started: OK"
dumpfile="/root/mysql_sql/P2PDB_Current.sql"
#ddl="set names utf8; "
ddl="$ddl set global net_buffer_length=1000000;"
ddl="$ddl set global max_allowed_packet=1000000000; "
ddl="$ddl SET foreign_key_checks = 0; "
ddl="$ddl SET UNIQUE_CHECKS = 0; "
ddl="$ddl SET AUTOCOMMIT = 0; "
# if your dump file does not create a database, select one
ddl="$ddl USE P2PDB; "
ddl="$ddl source $dumpfile; "
ddl="$ddl SET foreign_key_checks = 1; "
ddl="$ddl SET UNIQUE_CHECKS = 1; "
ddl="$ddl SET AUTOCOMMIT = 1; "
ddl="$ddl COMMIT ; "
ddl="$ddl CREATE USER '[email protected]%' IDENTIFIED BY '**29045478**' ; "
ddl="$ddl grant select on IOTCMasterDB_R.SVR_IP_Table to watchdog identified by 'xxxx' ; "
ddl="$ddl grant select on P2PDB.SVR_IP_Table to watchdog identified by 'xxxx'; "
ddl="$ddl flush privileges; "
echo "Import started: OK"
time mysql -h 127.0.0.1 -u root -pwelcome1 -e "$ddl"
 
# store end date to a variable
imeron2=`date`
echo "Start import:$imeron"
echo "End import:$imeron2"

add import mysql big data

#!/bin/sh 

  # store start date to a variable
  imeron=`date`

  echo "Import started: OK"
  dumpfile="/root/mysql_sql/xxxxx.sql"

 # ddl="set names utf8; "
  ddl="$ddl set global net_buffer_length=1000000;"
  ddl="$ddl set global max_allowed_packet=1000000000; "
  ddl="$ddl SET foreign_key_checks = 0; "
  ddl="$ddl SET UNIQUE_CHECKS = 0; "
  ddl="$ddl SET AUTOCOMMIT = 0; "
  # if your dump file does not create a database, select one
  ddl="$ddl USE P2PDB; "
  ddl="$ddl source $dumpfile; "
  ddl="$ddl SET foreign_key_checks = 1; "
  ddl="$ddl SET UNIQUE_CHECKS = 1; "
  ddl="$ddl SET AUTOCOMMIT = 1; "
  ddl="$ddl COMMIT ; "

  echo "Import started: OK"

  time mysql -h 127.0.0.1 -u root -pxxxx -e "$ddl"

  # store end date to a variable
  imeron2=`date`

  echo "Start import:$imeron"
  echo "End import:$imeron2"

add show status progress in Linux Shell Scripts

while true; do echo -n .; sleep 1; done&
trap 'kill $!' SIGTERM SIGKILL

echo "Running tar command:"
tar
echo done

kill $!

Custom shell script – 0702

安裝方式:到本機用 wget 指令下載安裝包之後解封,用以下程式碼執行

wget -qO- https://s3.amazonaws.com/xx/xx/xx-Worldwide.tgz | tar xvz

echo -e "Change Taiwan Time\n"
sudo timedatectl set-timezone Asia/Taipei
echo -e "Start installation...\n"
sudo apt-get update
sudo apt install -y debconf-utils
echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections
echo iptables-persistent iptables-persistent/autosave_v6 boolean false | sudo debconf-set-selections
sudo apt-get install -y iptables-persistent snmpd python python-pip awscli htop nload speedtest-cli lrzsz
sudo pip install pytz
P2PHOSTS="/etc/hosts"
echo '50.19.254.134           m1.iotcplatform.com     m1
122.248.234.207         m2.iotcplatform.com     m2
46.137.188.54           m3.iotcplatform.com     m3
106.15.79.10            m5.iotcplatform.com     m5
120.24.59.150           m6.iotcplatform.com     m6
114.215.137.159         m7.iotcplatform.com     m7
104.199.156.58          m8.iotcplatform.com     m8 
52.79.197.188           m9.iotcplatform.com     m9
50.19.254.134           m1.tutk.com
122.248.234.207         m2.tutk.com
46.137.188.54           m3.tutk.com
106.15.79.10            m4.tutk.com
106.15.79.10            m5.tutk.com
120.24.59.150           m6.tutk.com
114.215.137.159         m7.tutk.com
104.199.156.58          m8.tutk.com
52.79.197.188           m9.tutk.com' >> ${P2PHOSTS}
sleep 3
echo -e "Start Other installation...\n"
mkdir -p /var/prtg/scripts/ 
CONF="/root/c" 
cp -rf ${CONF}/snmpd.conf /etc/snmp/
cp -rf ${CONF}/sysctl.conf /etc/
/sbin/sysctl -p
cp -rf ${CONF}/limits.conf /etc/security/
cp -rf ${CONF}/iotc.service /etc/systemd/system/
cp -rf ${CONF}/Get-nf_conntrack_count.py /var/prtg/scripts/
/bin/systemctl enable iotc
echo "nf_conntrack_ipv4" >> /etc/modules
echo "nf_conntrack" >> /etc/modules
./firewall.sh
/sbin/iptables -L -vn
/sbin/iptables-save > /etc/iptables/rules.v4
echo -e "add ip myrealip..\n"
IOTC="/root/IOTC_Server/"
cd ${IOTC}
/bin/sh myrealip-pub.sh
 
IP=127.0.0.1
HOST=`cat /etc/hosts |grep 127.0.0.1|  awk  '{ print $2 }'`
sed -i "/$IP/ s/.*/$IP\t$1/g" /etc/hosts
echo "$1" > /etc/hostname
###
echo '*/3 * * * *   /root/checkdisk.sh 2>&1
*/5 * * * *    /root/deletelog2.sh 2>&1
1 3 * * *  /root/script/p2p_hostlist.sh 2>&1
' >> /etc/crontab 
echo '/var/log/syslog
{
	rotate 7
	daily
	missingok
	notifempty
	delaycompress
	compress
	postrotate
		invoke-rc.d rsyslog rotate > /dev/null
	endscript
}
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
{
	rotate 4
	weekly
	missingok
	notifempty
	compress
	delaycompress
	sharedscripts
	postrotate
		invoke-rc.d rsyslog rotate > /dev/null
	endscript
}
/var/log/auth.log
{
        rotate 12
        weekly
        missingok
        notifempty
        compress
        delaycompress
        sharedscripts
        postrotate
                invoke-rc.d rsyslog rotate > /dev/null
        endscript
}
' > /etc/logrotate.d/rsyslog
/bin/mv /root/p2p-install-0921-22013.sh /root/script/

就解決我無法下載的問題了…