Skip to content

Instantly share code, notes, and snippets.

@ChrisWelsh-mis
Last active October 4, 2018 22:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisWelsh-mis/017febdb356188d868e7a6e27a711e88 to your computer and use it in GitHub Desktop.
Save ChrisWelsh-mis/017febdb356188d868e7a6e27a711e88 to your computer and use it in GitHub Desktop.
#!/bin/bash
## This EC2 instance will need to be in the 'jenkins-ci-server' IAM Role
# get latest updates
sudo yum update -y
# add Jenkins repo
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
# import key from Jenkins CI
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
# install Jenkins
sudo yum install jenkins -y
# install Java 8
sudo yum install java-1.8.0-openjdk-headless.x86_64 -y
sudo /usr/sbin/alternatives --set java /usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin/java
sudo /usr/sbin/alternatives --set javac /usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin/javac
# disable startup wizard
sudo sed -i 's/JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true"/JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Djenkins.install.runSetupWizard=false"/g' /etc/sysconfig/jenkins
# create backups directory
JENKINS_BKUP_DIR="/var/lib/jenkins/backups"
sudo mkdir -p "$JENKINS_BKUP_DIR"
# get backup from S3
S3_BUCKET="s3://<BUCKET_NAME/DIRECTORY>" #S3 Bucket name and directory
S3_BUCKET_DL="s3://<BUCKET_NAME" # S3 Bucket root
JENKINS_TAR="$(aws s3 ls "$S3_BUCKET" --recursive | sort | tail -n 1 | awk '{print $4}')"
sudo aws s3 cp "$S3_BUCKET_DL""$JENKINS_TAR" "$JENKINS_BKUP_DIR"
# change permissions
sudo chown -R jenkins:jenkins "$JENKINS_BKUP_DIR"
# untar file
cd "$JENKINS_BKUP_DIR" || exit
sudo tar --same-owner -xjf $(basename "$JENKINS_TAR") -C "$JENKINS_BKUP_DIR"
# install thinBackup
sudo wget http://localhost:8080/jnlpJars/jenkins-cli.jar
sudo java -jar jenkins-cli.jar -s http://localhost:8080/ install-plugin http://updates.jenkins-ci.org/latest/thinBackup.hpi
# start Jenkins as a service
sudo service jenkins enable
sudo service jenkins start
# removes itself after execution
rm -- "$0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment