Remote backup, upload project, restart server under Linux
Linux Server for remote, original project backup, delete, new project upload, and remote Restart the server!
Split into a main shell to invoke the three shell file steps to complete.
MAINSH.SH executes multiple scripts in sequence at a time, completing the three-step sh sequence:
#!/bin/sh
#此Shell一次按顺序执行多个脚本.
#参数::
#远程jboss服务器所在的ip.
ip=127.0.0.1.
. #服务器登录账户
. userx= ' TSKK '
#服务器登录密码
passx= ' Sasa '
#部署jboss远程服务器上项目war文件的全路径名称 (for backup)
Jbossfiledirname=/opt/app/jboss5/server/warset/jasmine.war.
#备份文件的主目录.
Filebackupdir=/home/tskk/backup.
#同名备份区分标识 (to prevent backup of different project files with duplicate names)
Backupflag=tskk.
File full path name under #本地Linux服务器 (or Jenkins under Project)
Jenkinsfiledirname=/opt/app/jenkins/workspace/jasmine.war.
#jboss下服务器/bin path (for restart)
Jbossbinfiledir=/opt/app/jboss5/server/bin.
20.
#远程操作shell文件的全路径, can also be a different folder
22.shfiledir=/opt/app/tskk/commonsh
#远程服务器拷贝, delete, upload, restart JBoss
24.sh $shfiledir/remotebackupdeletewar.sh $IP $userx $passx $jbossfiledirname $filebackupdir
25.sh $shfiledir/remoteuploadwar.sh $IP $userx $passx $jenkinsfiledirname $jbossfiledirname
26.sh $shfiledir/remoterestartjboss.sh $IP $userx $passx $jbossbinfiledir
Note: sh ***.sh parm1 parm2 ..., multiple scripts can be executed sequentially
remotebackupdeletewar.sh Implementing remote Backup files:
#!/bin/sh
02.echo "File Backupdelete start .........."
#访问远程服务器ip, User,pass.
04.ip=$1
05.userx=$2
06.passx=$3
. #jboss服务器上项目war文件的全路径名称 Eg:/opt/app/jboss5/server/warset/jsm.war
08.jbossfiledirname=$4
Eg:/opt/app/jboss5/server/warset #dirname获取jboss上war文件路径.
10.mianjbossfiledir= ' DirName $jbossfiledirname '
One. #basename获取jboss上war文件的名称 Eg:jsm.war
12.jbossfilename= ' basename $jbossfiledirname '
#备份文件的主目录.
14.mainbackupdir=$5
#备份同名标识.
16.backupflag=$6
#备份文件夹的名称 eg:tskk-jsm.war201412021439
18.date=$ (DATE +%y%m%d%h%m)
19.filename2= $backupflag-${jbossfilename}${date}
#sshpass登录远程服务器, Backup and delete war files
21.sshpass-p $passx ssh-o stricthostkeychecking=no-tt [email protected] $IP <<sshaccess
22.CD $mainbackupdir
23.mkdir $filename 2
24.CD $mianjbossfiledir
25.cp-r $jbossfilename $mainbackupdir/$filename 2
26.RM-RF $jbossfilename
27.exit
28.sshaccess
29.echo "File Backupdelete end-ok ......... ..."
30.exit 0
Note: Shell command sshpass non-interactive SSH with password authentication
-tt prevent scripts from calling SSH after Pseudo-terminal would not be allocated because stdin are not a terminal problem
-O Stricthostkeychecking=no avoid a public key check for the first logon
remoteuploadwar.sh Implementing a Remote Project Deployment server:
#!/bin/sh
02.echo "File Upload start .........."
#访问远程服务器ip, User,pass.
04.ip=$1
05.userx=$2
06.passx=$3
. #Jenkins下文件全路径名称
08.jenkinsfiledirname=$4
#获取Jenkins下文件路径.
10.jenkinsfiledir= ' DirName $jenkinsfiledirname '
#获取Jenkins上文件名称.
12.jenkinsfilename= ' basename $jenkinsfiledirname '
#jboss上war文件全路径名称.
14.jbossfiledirname=$5
#jboss上war文件路径.
16.jbossfiledir= ' DirName $jbossfiledirname '
#jboss上war文件名称
18.jbossfilename= ' basename $jbossfiledirname '
#对Jenkins下jar按jboss上规范给文件命名
#cd $jenkinsfiledir
#mv $jenkinsfilename $jbossfilename
#重命名后Jenkins上文件全路径名称.
#rejenkinsfiledirname = $jenkinsfiledir/$jbossfilename
#sshpass远程上传文件到jboss服务器
25.sshpass-p $passx scp-o stricthostkeychecking=no $jenkinsfiledirname [email protected] $IP: $jbossfiledir << Sshpassupload
26.exit
27.sshpassupload
28.echo "File Upload end-ok ......... ..."
29.exit 0
remoterestartjboss.sh to enable remote server restart:
#!/bin/sh
02.echo "JBoss restart Start .........."
#获取jboss服务器的连接信息ip, User,pass.
04.ip=$1
05.userx=$2
06.passx=$3
. #获取jboss服务器的/bin Directory
08.jbossbindir=$4
09.sshpass-p $passx ssh-tt-o stricthostkeychecking=no [email protected] $IP <<jbossrestart
10.CD $jbossbindir
11.sudo-u jboss5./shutdown.sh
12.sudo-u jboss5./start.sh
13.exit
14.jbossrestart
15.echo "JBoss Restart OK .........."
16.exit 0
Realized, backup, upload, restart the function, but did three times the connection to the server disconnected!
Remote backup, upload project, restart server under Linux