Php+shell-Based publishing system

Source: Internet
Author: User
Tags node server rsync

I. Issues to be addressed by the publishing system

A more complete publishing system must first be able to publish files, and secondly, when the issue of issues should be able to support the revocation, to avoid long time cannot solve the problem affecting the normal operation of the product.
For publishing scripting languages such as Php,shell join syntax checking, there are syntax errors preventing this release.

Two. Principle

We are using the publishing system based on Php+shell development, the release of the time to fill out the file list, from the current machine A to target machine B, before the release of a machine through the SSH protocol authentication login to the B machine execute shell command backup file list to be published
Then use rsync to synchronize the file list from the A machine to the B machine, write the current release record to the database when it is finished, and overwrite the corresponding backup file to the B machine when the rollback is needed.

Three. The problem to be resolved

1. Using rsync and backing up the target machine files requires a password-free login

A machine to B machine to achieve a password-free login, you need to use Ssh-keygen to generate the current user's login key, the current user's public key is appended to the target machine user's. ssh/authorized_keys file
The following shell is the Authorized_keys file that appends the public key of the current machine to the target machine

user=wwwip=192.168.1.100 #B机器ipPORT =22 #B机器SSH协议端口cat/home/${user}/.ssh/id_rsa.pub | SSH ${user}@{ip} ' cat>>/home/{$USER}/.ssh/authorized_keys '

Follow-up without entering the password can be in a machine-free login B Machine

2. Publish to more than one machine
If the target machine has multiple units, it can be circulated to the target machine. If the file is too large for each update, it can be published to a central node server and then synced to other machines using Sersync. Https://code.google.com/archive/p/sersync/downloads
Modify the CONFIG. rsync node <ssh start= "True"/>, and use the password-free user to start the Sersync service

#!/bin/bash#-------------------------------------------------------------------------------------------------------#从当前机器发布文件到远程机器, if it is a distributed machine, to a machine as the main node through Sersync synchronization to other nodes # # #参数说明 # released #$1: Perform action send#$2: Remote target machine ip#$3: Remote target machine Port #$4: Current machine Publishing root directory #$5: Remote target machine Publishing root directory #$6: Remote target machine backup root directory #$7: Release version number #$8: File list multiple files or directories used","separated # # # rollback #$1: Perform action roll#$2: Remote target machine ip#$3: Remote target machine Port #$4: Current machine Publishing root directory #$5: Remote target machine Publishing root directory #$6: Remote target machine backup root directory #$7: Roll Back the version number #----------------------------------------------------------------------------------------------------Source/etc/Profileumask022readonly target_ip=$2#远程目标机器ipreadonly Target_port=$3#远程目标机器ipreadonly USER=www #执行远程shell命令用户readonly source_root=$4#当前机器发布根目录readonly Target_root=$5#远程目标机器发布目录readonly Backup_root=$6#远程目标机器备份路径readonly execssh="/usr/bin/ssh-p ${target_port}-o stricthostkeychecking=no ${user}@${target_ip}"#在远程目标机器执行shell命令 # PublishfunctionSend () {IFS=","Error=0Afile=($1)     for file inch${afile[@]} Do        if[!-E"${source_root}/${file}"]; ThenfileList+={$file} error=1        fi     Done    if["${error}"!=0]; Then        Echo "file list is not exists in ${source_root}"        Echo$fileList Exit1    fiEval"${execssh} ${target_root}/cron/sendfile.sh backup ${1} ${2} ${target_root} ${backup_root}"        if["$?"==0]; ThenCD ${source_root} fileList=$(Echo "${1}"|TR "," " ")        #/usr/bin/rsync-avzr-eSSH${filelist} ${user}@${target_ip}:${target_root} eval"/USR/BIN/RSYNC-AVZR '-e ssh-p ${target_port} ' ${filelist} ${user}@${target_ip}:${target_root}"    Else        Echo "Back up faild!!!"Exit2    fiExit0} #回滚functionRoll () {$execssh"tar zxf ${backup_root}/${1}.bak.tar.gz-c ${target_root}"    Echo 'OK'} #备份functionBackup () {Target_root=$3Back_root=$4CD ${target_root} IFS=","Afile=($1) FileList=""     for file inch${afile[@]} Do        if[-E"${target_root}/${file}"]; ThenfileList+="$file"        fi     Done    if[-N"${filelist}"]; Then        if[!-d ${back_root}]; Then            mkdir-P ${back_root}if["$?"!=0]; Then                Echo "mkdir backup dir ${back_root} fail"Exit1            fi        fi        TarCzf ${back_root}/${2}.bak.Tar. GZ $1        if["$?"==0]; Then            Echo "backup File ${back_root}/${2}.bak.tar.gz!"        fi     fi}functionArgscheck () {if[-Z $2] || [-Z $3] || [-Z $4] || [-Z $5] || [-Z $6] || [-Z $7]; Then        Echo "useage ${0} send|roll target_ip target_port source_root target_root backup_root VER"Exit1    fi    if[!-D $4]; Then        Echo "Source_root ${source_root} is not exsists!"Exit2    fi} Case$1 inch    "Send") Argscheck"[email protected]"Send $8$7#文件列表版本号;; "Backup") Backup $2$3$4$5    ;; " Roll") Argscheck"[email protected]"Roll $7#版本号;; *)    Echo "useage ${0} send|roll target_ip target_port source_root target_root backup_root VER"    ;;Esac
Shell Script

  



Php+shell-Based publishing system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.