配置免密登錄伺服器及下載備份文件#!/bin/bash BACKUP=192.168.30.233 #一行寫一個IP BACKUP_PASSWD="lxzl_root*#2021" #root密碼 MYSQLBACKUP=192.168.30.232 #一行寫一個IP MYSQLBACKUP_PAS ...
配置免密登錄伺服器及下載備份文件
#!/bin/bash BACKUP=192.168.30.233 #一行寫一個IP BACKUP_PASSWD="lxzl_root*#2021" #root密碼 MYSQLBACKUP=192.168.30.232 #一行寫一個IP MYSQLBACKUP_PASSWD="lxzl_root*#2021" #root密碼 #檢查是否安裝expect function init { rpm -qa | grep expect if [[ $? == 0 ]]; then echo "expect已安裝" else yum -y install expect fi #抓取伺服器IP及寫入hosts文件 sed -n "/^\[$1/,/^$/p" /etc/ansible/init_server/hosts | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' > /etc/ansible/init_server/ip.list && sed -n "/^\[$1/,/^$/p" /etc/ansible/init_server/hosts >> /etc/ansible/hosts } #function ssh_hosts { #sed -n '/^\[$1/,/^$/p' /etc/ansible/init_server/hosts | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' > /etc/ansible/init_server/ip.list && sed -n '/^\[$1/,/^$/p' /etc/ansible/init_server/hosts >> /etc/ansible/hosts # SERVERS=`cat /etc/ansible/init_server/ip.list` #一行寫一個IP # PASSWD="123456" #root密碼 # for server in $Server # do # sshcopyid # done #} #BACKUP=192.168.30.233 #一行寫一個IP #BACKUP_PASSWD="lxzl_root*#2021" #root密碼 #輸入免密伺服器密碼函數 function sshcopyid { expect -c " set timeout -1; spawn ssh-copy-id $1; expect { \"yes/no\" { send \"yes\r\" ;exp_continue; } \"password:\" { send \"$PASSWD\r\";exp_continue; } }; interact " } #免密登錄備份伺服器拉去備份文件 function backup_sshcopyid { expect -c " set timeout -1; spawn ssh-copy-id $1; expect { \"yes/no\" { send \"yes\r\" ;exp_continue; } \"password:\" { send \"$BACKUP_PASSWD\r\";exp_continue; } }; interact " } #免密登錄MYSQL備份伺服器拉去備份文件 function mysqlbackup_sshcopyid { expect -c " set timeout -1; spawn ssh-copy-id $1; expect { \"yes/no\" { send \"yes\r\" ;exp_continue; } \"password:\" { send \"$MYSQLBACKUP_PASSWD\r\";exp_continue; } }; interact " } #ansible配置免密登錄部署伺服器 function ssh_server { SERVERS=`cat /etc/ansible/init_server/ip.list` #一行寫一個IP PASSWD="LXZLProSvr4ROOT*#2021" #root密碼 #PASSWD="123456" #root密碼 for server in $SERVERS do sshcopyid $server done } #下載代理備份文件 function proxy { ansible_nginx=/etc/ansible/init_server/roles/nginx/files/ if [ $1 == "NGINX" ];then scp $BACKUP:/lxserver/backup/Nginx/192.168.0.10/`date +%F`/*.tar.gz $ansible_nginx #/etc/ansible/init_server/roles/nginx/files/ rm -rf nginx && tar xzf nginx_10.tar.gz ssh_server elif [ $1 == "JENKINS" ];then scp $BACKUP:/lxserver/backup/Nginx/192.168.0.120/`date +%F`/*.tar.gz $ansible_nginx #/etc/ansible/init_server/roles/nginx/files/ rm -rf nginx && tar xzf nginx_120.tar.gz ssh_server elif [ $1 == "SLB" ];then scp $BACKUP:/lxserver/backup/Nginx/192.168.0.200/`date +%F`/*.tar.gz $ansible_nginx #/etc/ansible/init_server/roles/nginx/files/ rm -rf nginx && tar xzf nginx_200.tar.gz ssh_server else exit fi } #下載NoSql備份文件 function Nosql { scp $BACKUP:/lxserver/backup/MongoDB/192.168.0.71/`date +%F -d "1 days ago"`/*.tar.gz /etc/ansible/init_server/roles/mongodb/files/ } #下載MYSQL備份文件 function mysql { ansible_mysql=/etc/ansible/init_server/roles/mysql/files/ if [ $1 == "ERPMYSQL" ];then rm -f $ansible_mysql/*.sql.gz scp $MYSQLBACKUP:/lxserver/backup/MySQL/192.168.0.82/`date +%F -d "1 days ago"`/*.sql.gz $ansible_mysql #rm -rf nginx && tar xzf nginx_10.tar.gz ssh_server elif [ $1 == "AMSMYSQL" ];then rm -f $ansible_mysql/*.sql.gz scp $MYSQLBACKUP:/lxserver/backup/MySQL/192.168.0.84/`date +%F -d "1 days ago"`/*.sql.gz $ansible_mysql #/etc/ansible/init_server/roles/nginx/files/ #rm -rf nginx && tar xzf nginx_120.tar.gz ssh_server elif [ $1 == "HSCSMYSQL" ];then rm -f $ansible_mysql/*.sql.gz scp $MYSQLBACKUP:/lxserver/backup/MySQL/192.168.0.162/`date +%F -d "1 days ago"`/*.sql.gz $ansible_mysql #/etc/ansible/init_server/roles/nginx/files/ #rm -rf nginx && tar xzf nginx_200.tar.gz ssh_server elif [ $1 == "WMSMYSQL" ];then rm -f $ansible_mysql/*.sql.gz scp $MYSQLBACKUP:/lxserver/backup/MySQL/192.168.0.164/`date +%F -d "1 days ago"`/*.sql.gz $ansible_mysql #/etc/ansible/init_server/roles/nginx/files/ #rm -rf nginx && tar xzf nginx_200.tar.gz ssh_server elif [ $1 == "ALMMYSQL" ];then rm -f $ansible_mysql/*.sql.gz scp $MYSQLBACKUP:/lxserver/backup/MySQL/192.168.0.166/`date +%F -d "1 days ago"`/*.sql.gz $ansible_mysql #/etc/ansible/init_server/roles/nginx/files/ #rm -rf nginx && tar xzf nginx_200.tar.gz ssh_server else exit fi } #main函數 function main { if [ $1 == "NGINX" -o $1 == "SLB" -o $1 == "JENKINS" ];then init #備份伺服器 backup_sshcopyid $BACKUP proxy $1 elif [ $1 == "NoSql" ];then init backup_sshcopyid $BACKUP Nosql $1 ssh_server elif [ $1 == "HSCS-NoSql" -o $1 == "APP" -o $1 == "KAFKA" -o $1 == "FASTDFS" -o $1 == "SERVER" ];then init ssh_server elif [ $1 == "ERPMYSQL" -o $1 == "HSCSMYSQL" -o $1 == "AMSMYSQL" -o $1 == "WMSMYSQL" -o $1 == "ALMMYSQL" -o $1 == "MYSQL" ];then init ssh_server mysqlbackup_sshcopyid $MYSQLBACKUP mysql $1 #ssh_server else echo "輸入參數錯誤,請輸入:APP | NGINX | SLB | JENKINS | NoSql | HSCS-NOSQL | KAFKA | FASTDFS | SERVER | ERPMYSQL | AMSMYSQL | HSCSMYSQL | WMSMYSQL | ALMMYSQL" exit fi } #程式入口 main $1
安裝配置redis
cat HSCS-Init_Nosql.yml - name: init server #gather_facts: False remote_user: root hosts: Hscs-NoSql roles: - init - hscs-redis
安裝配置mongodb及數據恢復
cat roles/mongodb/tasks/main.yml - name: copy install package copy: src: "{{ item }}" dest: /root with_items: - mongodb-org-4.0.1-1.el7.x86_64.rpm - mongodb-org-server-4.0.1-1.el7.x86_64.rpm - mongodb-org-mongos-4.0.1-1.el7.x86_64.rpm - mongodb-org-shell-4.0.1-1.el7.x86_64.rpm - mongodb-org-tools-4.0.1-1.el7.x86_64.rpm - name: install mongod shell: yum localinstall -y /root/mongodb-* && rm -f /root/mongodb-* /etc/mongo* && rm -f /usr/lib/systemd/system/mongo* - name: copy mongo service && conf copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" with_items: - { src: 'mongod19000.conf', dest: '/etc/', mode: '0644' } - { src: 'mongod19001.conf', dest: '/etc/', mode: '0644' } - { src: 'mongod19000.service', dest: '/usr/lib/systemd/system', mode: '0644' } - { src: 'mongod19001.service', dest: '/usr/lib/systemd/system', mode: '0644' } - name: mkdir dir file: path: "{{ item }}" state: directory with_items: - /var/run/mongodb19000 - /var/lib/mongo19000 - /var/log/mongodb19000 - /var/run/mongodb19001 - /var/lib/mongo19001 - /var/log/mongodb19001 - name: Unarchive a file that is already on the remote machine unarchive: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" with_items: - { src: 'mongodb_71_19000_all.tar.gz', dest: '/var/lib/mongo19000', mode: '0755' } - { src: 'mongodb_71_19001_all.tar.gz', dest: '/var/lib/mongo19001', mode: '0755' } - name: daemon-reload shell: systemctl daemon-reload - name: systemctl start service: name: "{{ item }}" state: started enabled: yes with_items: - mongod19000 - mongod19001 - name: 19000 restore data shell: mongorestore -h 127.0.0.1:19000 -d "{{ item.name }}" "{{ item.path }}" with_items: - { name: 'workflow', path: '/var/lib/mongo19000/mongodb_71_19000_all/workflow' } - { name: 'risk_system', path: '/var/lib/mongo19000/mongodb_71_19000_all/risk_system' } - { name: 'admin', path: '/var/lib/mongo19000/mongodb_71_19000_all/admin' } - name: 19001 restore data shell: mongorestore -h 127.0.0.1:19001 -d "{{ item.name }}" "{{ item.path }}" with_items: - { name: 'tencent_qm_status', path: '/var/lib/mongo19001/mongodb_71_19001_all/tencent_qm_status' } - { name: 'lxzl_message', path: '/var/lib/mongo19001/mongodb_71_19001_all/lxzl_message' } - { name: 'admin', path: '/var/lib/mongo19001/mongodb_71_19001_all/admin' } - name: 19000 restore data shell: mongorestore -h 127.0.0.1:19000 -u '{{ item.user}}' -p '{{ item.password}}' -d "{{ item.name }}" "{{ item.path }}" with_items: - { user: 'workflow', password: 'workflow', name: 'workflow', path: '/var/lib/mongo19000/mongodb_71_19000_all/workflow' } - { user: 'risk_system', password: 'risk_system', name: 'risk_system', path: '/var/lib/mongo19000/mongodb_71_19000_all/risk_system' } - { user: 'root', password: 'lx_root*#2020', name: 'admin', path: '/var/lib/mongo19000/mongodb_71_19000_all/admin' } - name: 19001 restore data shell: mongorestore -h 127.0.0.1:19001 -u '{{ item.user}}' -p '{{ item.password}}' -d "{{ item.name }}" "{{ item.path }}" with_items: - { user: 'tencent_qm_status', password: 'tencent_qm_status', name: 'tencent_qm_status', path: '/var/lib/mongo19001/mongodb_71_19001_all/tencent_qm_status' } - { user: 'lxzl_message', password: 'lxzl_message', name: 'lxzl_message', path: '/var/lib/mongo19001/mongodb_71_19001_all/lxzl_message' } - { user: 'root', password: 'lx_root*#2020', name: 'admin', path: '/var/lib/mongo19001/mongodb_71_19001_all/admin' } shell: systemctl daemon-reload - name: systemctl start service: name: "{{ item }}" state: started enabled: yes with_items: - mongod19000 - mongod19001 - name: reboot shell: reboot
cat Init_Mysql.yml - name: init server gather_facts: False remote_user: root hosts: Mysql vars: - key: 164 #主機變數,指定後續恢復那台mysql數據 roles: - init - mysql
安裝配置mysql及數據恢復
cat roles/mysql/tasks/main.yml - name: copy mysql copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" with_items: - { src: 'mysql-community-common-5.7.11-1.el7.x86_64.rpm', dest: '/opt', mode: '0644' } - { src: 'mysql-community-libs-5.7.11-1.el7.x86_64.rpm', dest: '/opt', mode: '0644' } - { src: 'mysql-community-libs-compat-5.7.11-1.el7.x86_64.rpm', dest: '/opt', mode: '0644' } - { src: 'mysql-community-client-5.7.11-1.el7.x86_64.rpm', dest: '/opt', mode: '0644' } - { src: 'mysql-community-server-5.7.11-1.el7.x86_64.rpm', dest: '/opt', mode: '0644' } #- name: stop mariadb # service: # name: mariadb # state: stopped - name: remove mariadb yum: name: mariadb state: absent - name: install mysql yum: name: "{{ packages }}" vars: packages: - /opt/mysql-community-common-5.7.11-1.el7.x86_64.rpm - /opt/mysql-community-libs-5.7.11-1.el7.x86_64.rpm - /opt/mysql-community-libs-compat-5.7.11-1.el7.x86_64.rpm - /opt/mysql-community-client-5.7.11-1.el7.x86_64.rpm - /opt/mysql-community-server-5.7.11-1.el7.x86_64.rpm - name: copy mysql config file copy: src='my.cnf' dest='/etc/' mode='0644' - name: start mysqld service: name: mysqld state: started enabled: yes - name: copy change password copy: src: "mysql.sh" dest: "/root" - name: chang root password shell: sh /root/mysql.sh - name: copy AMS file that is already on the remote machine copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" with_items: - { src: 'mysql_84_db_lxzl_ams_admin.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_84_db_lxzl_ams.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_84_db_lxzl_ams_workflow.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_84_db_lxzl_nacos.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_84_db_lxzl_openapi.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_84_db_lxzl_rules_engine.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_84_mysql.sql.gz', dest: '/root', mode: '0644' } when: key == 84 - name: gzip backup file shell: cd /root && gzip -d {{ item }} with_items: - mysql_84_db_lxzl_ams_admin.sql.gz - mysql_84_db_lxzl_ams.sql.gz - mysql_84_db_lxzl_ams_workflow.sql.gz - mysql_84_db_lxzl_nacos.sql.gz - mysql_84_db_lxzl_openapi.sql.gz - mysql_84_db_lxzl_rules_engine.sql.gz - mysql_84_mysql.sql.gz when: key == 84 - name: copy ERP file that is already on the remote machine copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" with_items: - { src: 'mysql_82_db_lxzl_app.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_bank_enterprise.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_bill.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_contract.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_coupon.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_datacenter.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_dingding.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_erp.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_file_gateway.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_message_gateway.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_oauth.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_payment_gateway.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_product.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_risk_system.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_sap_assets_modification.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_sms.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_worker.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_workflow.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_db_lxzl_zl_jd.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_82_mysql.sql.gz', dest: '/root', mode: '0644' } when: key == 82 - name: gzip backup file shell: cd /root && gzip -d {{ item }} with_items: - mysql_82_db_lxzl_app.sql.gz - mysql_82_db_lxzl_bank_enterprise.sql.gz - mysql_82_db_lxzl_bill.sql.gz - mysql_82_db_lxzl_contract.sql.gz - mysql_82_db_lxzl_coupon.sql.gz - mysql_82_db_lxzl_datacenter.sql.gz - mysql_82_db_lxzl_dingding.sql.gz - mysql_82_db_lxzl_erp.sql.gz - mysql_82_db_lxzl_file_gateway.sql.gz - mysql_82_db_lxzl_message_gateway.sql.gz - mysql_82_db_lxzl_oauth.sql.gz - mysql_82_db_lxzl_payment_gateway.sql.gz - mysql_82_db_lxzl_product.sql.gz - mysql_82_db_lxzl_risk_system.sql.gz - mysql_82_db_lxzl_sap_assets_modification.sql.gz - mysql_82_db_lxzl_sms.sql.gz - mysql_82_db_lxzl_worker.sql.gz - mysql_82_db_lxzl_workflow.sql.gz - mysql_82_db_lxzl_zl_jd.sql.gz - mysql_82_mysql.sql.gz when: key == 82 - name: copy HSCSMYSQL file that is already on the remote machine copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" with_items: - { src: 'mysql_162_db_lxzl_hscs.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_162_mysql.sql.gz', dest: '/root', mode: '0644' } when: key == 162 - name: shell: cd root && gzip -d {{ item }} with_items: - mysql_162_db_lxzl_hscs.sql.gz - mysql_162_mysql.sql.gz when: key == 162 - name: copy WMSMYSQL file that is already on the remote machine copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{ item.mode }}" with_items: - { src: 'mysql_164_db_lxzl_purchase.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_164_db_lxzl_sap.sql.gz', dest: '/root', mode: '0644' } - { src: 'mysql_164_db_lxzl_wms.sql.gz', dest: '/root', mode: '0644' }