Jenkins 安装和备份
1. Docker-compose 安装
cat > docker-compose.yml << EOF
version: '3.1'
services:
jenkins:
image: 'jenkins/jenkins'
restart: always
# root 用户,可以不用设置/opt/jenkins/jenkins_home的权限,否者将用Jenkins用户,需要单独授权此目录
user: root
container_name: jenkins
# 端口号:映射端口:容器端口
ports:
- '8080:8080'
- '50000:50000'
# 数据持久化:宿主机目录:容器目录
volumes:
- /opt/jenkins/jenkins_home:/var/jenkins_home
EOF
docker-compose up -d
2. Tomcat 安装
#!/bin/bash
# 有网的情况下才能操作
# 修改监听的端口,直接修改Tomcat的端口,默认8080即可
# 设置家目录。如果不设置,默认在运行Jenkins的用户的家目录下<~/.jenkins>
JENKINS_BASE="/opt/jenkins"
JENKINS_HOME="${JENKINS_BASE}/jenkins_home"
# jenkins.war下载url
JENKINS_DL='https://updates.jenkins-ci.org/latest/jenkins.war'
# 下载保存到的目录
JENKINS_Download="${JENKINS_BASE}/download"
# tomcat 下载url:可查看:https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/
TOMCAT_DL='https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gz'
TOMCAT="apache-tomcat-9.0.56"
JENKINS_BIN="${JENKINS_BASE}/bin"
# 配置systemctl的文件位置
JENKINS_SYSTEMCTL='/usr/lib/systemd/system/jenkins_tomcat.service'
# 如下自动执行
mkdir -p ${JENKINS_HOME} ${JENKINS_Download} ${JENKINS_BIN}
# 检查jdk是否安装
java -version > /dev/null 2>&1
if [ "$?" -ge 1 ];then
echo "安装jdk1.8,请稍后..."
yum install -y java-1.8.0-openjdk* > /dev/null 2>&1
java -version > /dev/null 2>&1
if [ $? -ge 1 ]; then
echo "JDK1.8安装失败,退出"
exit
fi
JAVA_PATH_COMMAD=`which java`
echo "JDK1.8安装 成功"
else
echo "已安装过JDK:"
JAVA_PATH_COMMAD=`which java`
fi
# 下载Tomcat
if [[ -f "${JENKINS_Download}/${TOMCAT}.tar.gz" ]]; then
echo "${JENKINS_Download} 目录有 ${TOMCAT}.tar.gz ,无需下载和移动"
else
echo "正在下载:${TOMCAT_DL}"
wget --no-check-certificate ${TOMCAT_DL} -P ${JENKINS_Download} > /dev/null 2>&1
[ $? -ge 1 ] && echo "下载失败,程序退出" && exit
echo "下载完成:${TOMCAT_DL}"
fi
echo "解压${TOMCAT}.tar.gz"
tar xzvf ${JENKINS_Download}/${TOMCAT}.tar.gz -C ${JENKINS_BIN} > /dev/null 2>&1
echo "解压完成"
# 下载Jenkins.war
if [[ -f "${JENKINS_Download}/jenkins.war" ]]; then
echo "${JENKINS_Download} 目录有 jenkins.war,无需下载和移动"
else
echo "正在下载:${JENKINS_DL}"
wget --no-check-certificate ${JENKINS_DL} -P ${JENKINS_Download} > /dev/null 2>&1
if [ $? -ge 1 ]; then
echo "下载失败,程序退出,退出"
exit
fi
echo "下载完成:${JENKINS_DL}"
fi
mv -f ${JENKINS_Download}/jenkins.war ${JENKINS_BIN}/${TOMCAT}/webapps
echo "已经移动:${JENKINS_Download}/jenkins.war 到 ${JENKINS_BIN}/${TOMCAT}/webapps"
# 配置systemctl
cat > ${JENKINS_SYSTEMCTL} << EOF
[Unit]
Description=Tomcat Jenkins Daemon
After=network.target
Requires=network.target
[Service]
Type=forking
User=root
Restart=always
Environment=JENKINS_HOME=${JENKINS_HOME}
ExecStart=${JENKINS_BIN}/${TOMCAT}/bin/startup.sh
ExecStop=${JENKINS_BIN}/${TOMCAT}/bin/shutdown.sh
RestartSec=10
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
echo "配置Jenkins.service完成:${JENKINS_SYSTEMCTL}"
systemctl daemon-reload
systemctl enable jenkins_tomcat.service
echo "已设置开机自启动"
systemctl start jenkins_tomcat.service
echo "已启动jenkins"
echo "初始密码,请稍后查看文件:${JENKINS_HOME}/secrets/initialAdminPassword"
echo "URL: http://ip:8080/jenkins"
3. JDK 安装
#!/bin/bash
# 有网的情况下才能操作
JENKINS_BASE="/opt/jenkins"
# 设置家目录。如果不设置,默认在运行Jenkins的用户的家目录下<~/.jenkins>
JENKINS_HOME="${JENKINS_BASE}/jenkins_home"
# 自定义端口如果为空,则默认监听8080
customHttpPort=""
httpPort=${customHttpPort:-"8080"}
# jenkins.war下载url
JENKINS_DL='https://updates.jenkins-ci.org/latest/jenkins.war'
# 下载保存到的目录
JENKINS_Download="${JENKINS_BASE}/download"
# jenkins 运行目录
JENKINS_BIN="${JENKINS_BASE}/bin"
# 配置systemctl的文件位置
JENKINS_SYSTEMCTL='/usr/lib/systemd/system/jenkins.service'
# 如下自动执行
mkdir -p ${JENKINS_HOME} ${JENKINS_Download} ${JENKINS_BIN}
# 检查wget是否安装
rpm -q wget > /dev/null 2>&1
if [ "$?" -ge 1 ];then
echo "安装wget,请稍后..."
yum install -y wget > /dev/null 2>&1
rpm -q wget > /dev/null 2>&1
if [ $? -ge 1 ]; thenecho "wget安装失败,退出"exitfi
echo "wget安装 成功"
fi
# 检查jdk是否安装
java -version > /dev/null 2>&1
if [ "$?" -ge 1 ];then
echo "安装jdk1.8,请稍后..."
yum install -y java-1.8.0-openjdk* > /dev/null 2>&1
java -version > /dev/null 2>&1
if [ $? -ge 1 ]; thenecho "JDK1.8安装失败,退出"exitfi
JAVA_PATH_COMMAD=`which java`
echo "JDK1.8安装 成功"
else
echo "已安装过JDK:"
JAVA_PATH_COMMAD=`which java`
fi
# 下载Jenkins.war
if [[ -f "${JENKINS_BIN}/jenkins.war" ]]; then
echo "${JENKINS_BIN} 目录有 jenkins.war,无需下载和移动"
else
echo "正在下载:${JENKINS_DL}"
wget --no-check-certificate ${JENKINS_DL} -P ${JENKINS_Download} > /dev/null 2>&1
[ $? -ge 1 ] && echo "下载失败,程序退出" && exit
echo "下载完成:${JENKINS_DL}"
fi
echo "移动${JENKINS_BIN}/jenkins.war 到 ${JENKINS_BIN} 完成"
mv -f ${JENKINS_Download}/jenkins.war ${JENKINS_BIN}
# 配置systemctl
echo "配置${JENKINS_SYSTEMCTL}"
cat > ${JENKINS_SYSTEMCTL} << EOF
[Unit]
Description=Jenkins Daemon
After=network.target
Requires=network.target
[Service]
User=root
Restart=always
Environment=JENKINS_HOME=${JENKINS_HOME}
ExecStart=${JAVA_PATH_COMMAD} -jar ${JENKINS_BIN}/jenkins.war -httpPort=${httpPort}
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
echo "配置完成${JENKINS_SYSTEMCTL}"
# 启动
echo "启动jenkins服务"
systemctl daemon-reload
systemctl enable jenkins.service
echo "已设置开机自启动"
systemctl start jenkins.service
echo "已启动jenkins"
echo "初始密码,请稍后查看文件:${JENKINS_HOME}/secrets/initialAdminPassword"
echo "端口号为${httpPort}"
4. 备份
Jenkins_home 存储了所有 Jenkins 的数据
启动Jenkins之后指定Jenkins_home 即可恢复
所以备份Jenkins,直接备份Jenkins_home目录即可
可通过如下方法:
1. 通过git版本进行备份
2. 通过每天打包压缩整个jenkins_home
3. 通过jenkins自带的插件