Skip to content

Instantly share code, notes, and snippets.

@geerlingguy
Created March 5, 2018 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geerlingguy/c58c53e68bb042a59851202c92cebfb4 to your computer and use it in GitHub Desktop.
Save geerlingguy/c58c53e68bb042a59851202c92cebfb4 to your computer and use it in GitHub Desktop.
Back up a JENKINS_HOME directory to Amazon S3. Meant to be run in a Jenkins job on the server.
---
- hosts: localhost
gather_facts: no
connection: local
vars:
aws_region: us-east-1
aws_profile: default
s3_bucket_name: jenkins-backup
jenkins_home: /var/lib/jenkins
tasks:
- name: Ensure an S3 bucket exists for the backups.
s3_bucket:
name: "{{ s3_bucket_name }}"
versioning: yes
- name: Sync the contents of JENKINS_HOME to S3.
command: >
aws s3 sync . s3://{{ s3_bucket_name }}
--delete
--only-show-errors
--exclude '.aws/*'
--exclude '.ssh/*'
--exclude 'workspace/*'
--exclude 'workspace'
args:
chdir: "{{ jenkins_home }}"
register: sync_command
# See: https://github.com/ansible/ansible/issues/32472
failed_when: False
# Allow up to 1 hour for this task to complete, check every 20s.
async: 3600
poll: 20
# See: https://github.com/aws/aws-cli/issues/2473
- name: Confirm sync command completed without any critical failures.
assert:
that: sync_command.rc == 0 or sync_command.rc == 2
- debug:
msg: "Backup to s3://{{ s3_bucket_name }} complete."
@rverma-nikiai
Copy link

Which dsl is this? I believe jenkins dsl is groovy only.

@mehulrts
Copy link

mehulrts commented Sep 6, 2018

looks like ansible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment