• jenkins备份和还原


    生产Jenkins还原步骤

    周六jenkins端口报警,登录vsphere管理后台发现其宿主机磁盘有问题,搞了半天没弄好,怀疑是宿主机的磁盘废了,为保证周一能用,放弃修复vsphere云主机,着手准备用jenkins的备份恢复(还好上上周把远程备份机换了一台磁盘大的)

    1、在新服务器上搭建1台新的jenkins

    其实线上有个备机,也是虚拟机,但是配置太低了,怕跑着跑着又挂了,索性重新拿台高配置的搞下

    这里有个坑,本来是想下载一个同版本的jenkins恢复,装好恢复后发现有的插件已经在低版本的jenkins不支持了,于是下载了最新稳定版的jenkins-2.204.5

    1.1 创建安装目录和日志目录

    #创建jenkins.war包目录
    mkdir -p /usr/lib/jenkins    #把jenkins.war放到这个目录内
    
    #创建日志目录
    mkdir -p /var/log/jenkins && chown -R uglyliu.uglyliu /var/log/jenkins
    
    #创建jenkins的workspace目录
    mkdir -p /var/lib/jenkins && chown -R uglyliu.uglyliu /var/lib/jenkins
    
    #创建jenkins备份目录
    mkdir -p /home/backup  && chown -R uglyliu.uglyliu /home/backup
    

    1.2 创建配置文件

    vi /etc/sysconfig/jenkins
    
    [root@ser10 jenkins]# cat /etc/sysconfig/jenkins 
    ## Path:        Development/Jenkins
    ## Description: Jenkins Continuous Integration Server
    ## Type:        string
    ## Default:     "/var/lib/jenkins"
    ## ServiceRestart: jenkins
    #
    # Directory where Jenkins store its configuration and working
    # files (checkouts, build reports, artifacts, ...).
    #
    JENKINS_HOME="/var/lib/jenkins"   #jenkins目录
    
    ## Type:        string
    ## Default:     ""
    ## ServiceRestart: jenkins
    #
    # Java executable to run Jenkins
    # When left empty, we'll try to find the suitable Java.
    #
    JENKINS_JAVA_CMD=""
    
    ## Type:        string
    ## Default:     "jenkins"
    ## ServiceRestart: jenkins
    #
    # Unix user account that runs the Jenkins daemon
    # Be careful when you change this, as you need to update
    # permissions of $JENKINS_HOME and /var/log/jenkins.
    #
    # JENKINS_USER="jenkins"
    JENKINS_USER="uglyliu"  #设置jenkins启动用户
    
    ## Type:        string
    ## Default: "false"
    ## ServiceRestart: jenkins
    #
    # Whether to skip potentially long-running chown at the
    # $JENKINS_HOME location. Do not enable this, "true", unless
    # you know what you're doing. See JENKINS-23273.
    #
    #JENKINS_INSTALL_SKIP_CHOWN="false"
    
    ## Type: string
    ## Default:     "-Djava.awt.headless=true"
    ## ServiceRestart: jenkins
    #
    # Options to pass to java when running Jenkins.
    #
    JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true"
    
    ## Type:        integer(0:65535)
    ## Default:     8080
    ## ServiceRestart: jenkins
    #
    # Port Jenkins is listening on.
    # Set to -1 to disable
    #
    JENKINS_PORT="8080"
    # JENKINS_PORT="80"
    
    ## Type:        string
    ## Default:     ""
    ## ServiceRestart: jenkins
    #
    # IP address Jenkins listens on for HTTP requests.
    # Default is all interfaces (0.0.0.0).
    #
    JENKINS_LISTEN_ADDRESS=""
    
    ## Type:        integer(0:65535)
    ## Default:     ""
    ## ServiceRestart: jenkins
    #
    # HTTPS port Jenkins is listening on.
    # Default is disabled.
    #
    JENKINS_HTTPS_PORT=""
    
    ## Type:        string
    ## Default:     ""
    ## ServiceRestart: jenkins
    #
    # Path to the keystore in JKS format (as created by the JDK 'keytool').
    # Default is disabled.
    #
    JENKINS_HTTPS_KEYSTORE=""
    
    ## Type:        string
    ## Default:     ""
    ## ServiceRestart: jenkins
    #
    # Password to access the keystore defined in JENKINS_HTTPS_KEYSTORE.
    # Default is disabled.
    #
    JENKINS_HTTPS_KEYSTORE_PASSWORD=""
    
    ## Type:        string
    ## Default:     ""
    ## ServiceRestart: jenkins
    #
    # IP address Jenkins listens on for HTTPS requests.
    # Default is disabled.
    #
    JENKINS_HTTPS_LISTEN_ADDRESS=""
    
    
    ## Type:        integer(1:9)
    ## Default:     5
    ## ServiceRestart: jenkins
    #
    # Debug level for logs -- the higher the value, the more verbose.
    # 5 is INFO.
    #
    JENKINS_DEBUG_LEVEL="5"
    
    ## Type:        yesno
    ## Default:     no
    ## ServiceRestart: jenkins
    #
    # Whether to enable access logging or not.
    #
    JENKINS_ENABLE_ACCESS_LOG="no"
    
    ## Type:        integer
    ## Default:     100
    ## ServiceRestart: jenkins
    #
    # Maximum number of HTTP worker threads.
    #
    JENKINS_HANDLER_MAX="100"
    
    ## Type:        integer
    ## Default:     20
    ## ServiceRestart: jenkins
    #
    # Maximum number of idle HTTP worker threads.
    #
    JENKINS_HANDLER_IDLE="20"
    
    ## Type:        string
    ## Default:     ""
    ## ServiceRestart: jenkins
    #
    # Pass arbitrary arguments to Jenkins.
    # Full option list: java -jar jenkins.war --help
    #
    JENKINS_ARGS=""
    JAVA_ARGS="[a-zA-Z0-9_.@-]+"   #设置可以以这种方式创建用户si.li
    

    1.3 创建启动脚本

    cat /etc/init.d/jenkins
    chmod +x /etc/init.d/jenkins
    
    #!/bin/sh
    #
    #     SUSE system statup script for Jenkins
    #     Copyright (C) 2007  Pascal Bleser
    #
    #     This library is free software; you can redistribute it and/or modify it
    #     under the terms of the GNU Lesser General Public License as published by
    #     the Free Software Foundation; either version 2.1 of the License, or (at
    #     your option) any later version.
    #
    #     This library is distributed in the hope that it will be useful, but
    #     WITHOUT ANY WARRANTY; without even the implied warranty of
    #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    #     Lesser General Public License for more details.
    #
    #     You should have received a copy of the GNU Lesser General Public
    #     License along with this library; if not, write to the Free Software
    #     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
    #     USA.
    #
    ### BEGIN INIT INFO
    # Provides:          jenkins
    # Required-Start:    $local_fs $remote_fs $network $time $named
    # Should-Start: $time sendmail
    # Required-Stop:     $local_fs $remote_fs $network $time $named
    # Should-Stop: $time sendmail
    # Default-Start:     3 5
    # Default-Stop:      0 1 2 6
    # Short-Description: Jenkins Automation Server
    # Description:       Jenkins Automation Server
    ### END INIT INFO
    
    # Check for missing binaries (stale symlinks should not happen)
    JENKINS_WAR="/usr/lib/jenkins/jenkins.war"
    test -r "$JENKINS_WAR" || { echo "$JENKINS_WAR not installed";
    	if [ "$1" = "stop" ]; then exit 0;
    	else exit 5; fi; }
    
    # Check for existence of needed config file and read it
    JENKINS_CONFIG=/etc/sysconfig/jenkins
    test -e "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not existing";
    	if [ "$1" = "stop" ]; then exit 0;
    	else exit 6; fi; }
    test -r "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not readable. Perhaps you forgot 'sudo'?";
    	if [ "$1" = "stop" ]; then exit 0;
    	else exit 6; fi; }
    
    JENKINS_PID_FILE="/var/run/jenkins.pid"
    
    # Source function library.
    . /etc/init.d/functions
    
    # Read config
    [ -f "$JENKINS_CONFIG" ] && . "$JENKINS_CONFIG"
    
    # Set up environment accordingly to the configuration settings
    [ -n "$JENKINS_HOME" ] || { echo "JENKINS_HOME not configured in $JENKINS_CONFIG";
    	if [ "$1" = "stop" ]; then exit 0;
    	else exit 6; fi; }
    [ -d "$JENKINS_HOME" ] || { echo "JENKINS_HOME directory does not exist: $JENKINS_HOME";
    	if [ "$1" = "stop" ]; then exit 0;
    	else exit 1; fi; }
    
    # Search usable Java as /usr/bin/java might not point to minimal version required by Jenkins.
    # see http://www.nabble.com/guinea-pigs-wanted-----Hudson-RPM-for-RedHat-Linux-td25673707.html
    candidates="
    /etc/alternatives/java
    /usr/lib/jvm/java-1.8.0/bin/java
    /usr/lib/jvm/jre-1.8.0/bin/java
    /usr/lib/jvm/java-1.7.0/bin/java
    /usr/lib/jvm/jre-1.7.0/bin/java
    /usr/local/java/jdk1.8.0_152/bin/java    #注意修改这个地方,你安装的java目录
    "
    for candidate in $candidates
    do
      [ -x "$JENKINS_JAVA_CMD" ] && break
      JENKINS_JAVA_CMD="$candidate"
    done
    
    #JAVA_CMD="$JENKINS_JAVA_CMD $JENKINS_JAVA_OPTIONS -DJENKINS_HOME=$JENKINS_HOME -jar $JENKINS_WAR"
    
    #下面这块增加了-Dhudson.security.HudsonPrivateSecurityRealm.ID_REGEX=$JAVA_ARGS,就是为了可以
    创建si.li这样的用户
    
    JAVA_CMD="$JENKINS_JAVA_CMD $JENKINS_JAVA_OPTIONS -DJENKINS_HOME=$JENKINS_HOME -Dhudson.security.HudsonPrivateSecurityRealm.ID_REGEX=$JAVA_ARGS -jar $JENKINS_WAR"
    PARAMS="--logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon"
    [ -n "$JENKINS_PORT" ] && PARAMS="$PARAMS --httpPort=$JENKINS_PORT"
    [ -n "$JENKINS_LISTEN_ADDRESS" ] && PARAMS="$PARAMS --httpListenAddress=$JENKINS_LISTEN_ADDRESS"
    [ -n "$JENKINS_HTTPS_PORT" ] && PARAMS="$PARAMS --httpsPort=$JENKINS_HTTPS_PORT"
    [ -n "$JENKINS_HTTPS_KEYSTORE" ] && PARAMS="$PARAMS --httpsKeyStore=$JENKINS_HTTPS_KEYSTORE"
    [ -n "$JENKINS_HTTPS_KEYSTORE_PASSWORD" ] && PARAMS="$PARAMS --httpsKeyStorePassword='$JENKINS_HTTPS_KEYSTORE_PASSWORD'"
    [ -n "$JENKINS_HTTPS_LISTEN_ADDRESS" ] && PARAMS="$PARAMS --httpsListenAddress=$JENKINS_HTTPS_LISTEN_ADDRESS"
    [ -n "$JENKINS_DEBUG_LEVEL" ] && PARAMS="$PARAMS --debug=$JENKINS_DEBUG_LEVEL"
    [ -n "$JENKINS_HANDLER_STARTUP" ] && PARAMS="$PARAMS --handlerCountStartup=$JENKINS_HANDLER_STARTUP"
    [ -n "$JENKINS_HANDLER_MAX" ] && PARAMS="$PARAMS --handlerCountMax=$JENKINS_HANDLER_MAX"
    [ -n "$JENKINS_HANDLER_IDLE" ] && PARAMS="$PARAMS --handlerCountMaxIdle=$JENKINS_HANDLER_IDLE"
    [ -n "$JENKINS_ARGS" ] && PARAMS="$PARAMS $JENKINS_ARGS"
    
    if [ "$JENKINS_ENABLE_ACCESS_LOG" = "yes" ]; then
        PARAMS="$PARAMS --accessLoggerClassName=winstone.accesslog.SimpleAccessLogger --simpleAccessLogger.format=combined --simpleAccessLogger.file=/var/log/jenkins/access_log"
    fi
    
    RETVAL=0
    
    case "$1" in
        start)
    	echo -n "Starting Jenkins "
    	daemon --user "$JENKINS_USER" --pidfile "$JENKINS_PID_FILE" $JAVA_CMD $PARAMS > /dev/null
    	RETVAL=$?
    	if [ $RETVAL = 0 ]; then
    	    success
    	    echo > "$JENKINS_PID_FILE"  # just in case we fail to find it
                MY_SESSION_ID=`/bin/ps h -o sess -p $$`
                # get PID
                /bin/ps hww -u "$JENKINS_USER" -o sess,ppid,pid,cmd | 
                while read sess ppid pid cmd; do
    		[ "$ppid" = 1 ] || continue
    		# this test doesn't work because Jenkins sets a new Session ID
                    # [ "$sess" = "$MY_SESSION_ID" ] || continue
    	       	echo "$cmd" | grep $JENKINS_WAR > /dev/null
    		[ $? = 0 ] || continue
    		# found a PID
    		echo $pid > "$JENKINS_PID_FILE"
    	    done
    	else
    	    failure
    	fi
    	echo
    	;;
        stop)
    	echo -n "Shutting down Jenkins "
    	killproc jenkins
    	RETVAL=$?
    	echo
    	;;
        try-restart|condrestart)
    	if test "$1" = "condrestart"; then
    		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
    	fi
    	$0 status
    	if test $? = 0; then
    		$0 restart
    	else
    		: # Not running is not a failure.
    	fi
    	;;
        restart)
    	$0 stop
    	$0 start
    	;;
        force-reload)
    	echo -n "Reload service Jenkins "
    	$0 try-restart
    	;;
        reload)
        	$0 restart
    	;;
        status)
        	status jenkins
    	RETVAL=$?
    	;;
        probe)
    	## Optional: Probe for the necessity of a reload, print out the
    	## argument to this init script which is required for a reload.
    	## Note: probe is not (yet) part of LSB (as of 1.9)
    
    	test "$JENKINS_CONFIG" -nt "$JENKINS_PID_FILE" && echo reload
    	;;
        *)
    	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
    	exit 1
    	;;
    esac
    exit $RETVAL
    

    1.4 启动jenkins

    /etc/init.d/jenkins start
    

    1.5 安装maven环境

    直接把旧的备份机/usr/local/maven这个目录scp拷贝过来,然后设置下以下环境变量
    
    
    MAVEN_HOME=/usr/local/maven
    export MAVEN_HOME
    export PATH=${PATH}:${MAVEN_HOME}/bin
    
    source /etc/profile
    

    1.6 安装node环境

    注意这里要切到uglyliu用户执行

    这里安装的时候遇到了几个坑,安装的时候一个提示git少了一个选项,记不太清了,反正yum安装的git命令就是有问题,可以编译安装一个,另外一个坑是克隆github项目出现SSL connect error,原因可能是ssl证书可能过期了,升级下包yum update -y nss curl libcurl或者直接yum update

    官方介绍:https://github.com/creationix/nvm #installation

    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash   (推荐)
    
    source ~/.bashrc
    
    npm install -g nrm
    
    nvm install v10.15.1
    
    nvm install v10.15.3
    

    1.7 编译安装git

    #下载高版本的git,git-2.9.3.tar.gz
    
    #安装依赖包、解压、编译安装
    yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl perl-devel -y
    tar -xf git-2.9.3.tar.gz
    cd git-2.9.3
    make prefix=/usr/local all
    make prefix=/usr/local install
    
    #配置环境变量
    GIT_HOME=/usr/local/git
    export GIT_HOME
    export PATH=${PATH}:${GIT_HOME}/bin
    
    

    1.8 安装jenkins必备插件

    AnsiColor                        #构建输出颜色
    Build With Parameters            #参数化构建 
    Pipeline                         #pipeline
    Configuration Slicing Plugin     #批量修改job
    Role-based Authorization Strategy#用户权限
    Email Extension Plugin           #发邮件的
    Git Parameter Plug-In            #git参数化构建
    ThinBackup                       #jenkins备份用的
    

    2、还原jenkins

    前面铺垫了那么多,现在来还原jenkins了,先用thinBackup设置jenkins的目录为/home/backup

    路径:Manager Jenkins --->ThinBackup--->Settings

    然后把之前的jenkins备份拷贝到/home/backup这个目录,在页面上点Restore,然后选择咱拷贝过去的备份文件,然后打开日志/var/log/jenkins/jenkins.log,过一会儿会显示restore successful字样

    还原后90%的数据都过来了,因为版本的关系,有些用户的信息没同步过来,不过也不是很多,重新创建丢失的用户即可

  • 相关阅读:
    Puppet报错汇总
    卖买股票的最佳收益
    JavaScript遍历JSON对象数据的方法
    网络IO模型
    STL pair类型的介绍
    JavaScript 代码执行顺序
    每日一练:#0001找出单独出现的数字
    更新GitHub项目出现There is no tracking information for the current branch. Please specify which branch you want to merge with. 怎么解决
    为什么MySQL数据库要用B+树存储索引?
    Node.js 获取本机Mac地址
  • 原文地址:https://www.cnblogs.com/uglyliu/p/12504872.html
Copyright © 2020-2023  润新知