Git Product home page Git Product logo

jenkins-backup-s3's Introduction

jenkins-backup-s3

A collection of scripts to backup Jenkins configuration to S3, as well as manage and restore those backups. By default runs silently (no output) with proper exit codes. Log Level option enables output.

Setup

pip install jenkins-backup-s3

Configure S3 and IAM

  • Create an S3 bucket to store backups.

  • Create an IAM role with STS:AssumeRole and a trust Service ec2.amazonaws.com. The IAM role must have the GetObject, DeleteObject, PutObject and ListBucket S3 permissions for that bucket.

Usage

Setup with cron for ideal usage.

backup-jenkins {OPTIONS} {COMMAND} {COMMAND_OPTIONS}

Options can be set directly or via and environment variable.

The only required option is your S3 bucket:

  • backup-jenkins --bucket={BUCKET_NAME}

Other available options are:

Bucket prefix (defaults to "jenkins-backups"):

  • backup-jenkins --bucket-prefix={BUCKET_PREFIX}

Bucket region (defaults to "us-east-1"):

  • backup-jenkins --bucket-region={BUCKET_REGION}

Available commands:

  • create
  • restore
  • list
  • delete
  • prune

Run backup-jenkins {COMMAND} --help for command-specific options.

Running a daily backup on Jenkins

Create a new item in Jenkins and configure a build of this repository.

Set the shell / virtualenv builder (if you have it installed) to run backup-jenkins create.

Set the build on a daily CRON schedule.

jenkins-backup-s3's People

Contributors

izakp avatar matt0x6f avatar mcgillij avatar ssmiller25 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jenkins-backup-s3's Issues

Prune undesirably removes too many backup files

When pruning backup files, I expect to keep the last "n" backups in my backup-bucket, when "n" is provided as prune-argument like

backup jenkins --backup some --backup-region eu-west-1 prune n.

However, we experienced for n=1 that ALL backed up files got removed from s3. After some digging into the code, we figured out that the problem is with this line:

for backup_id in s3.backups()[::keep]:

I played around with python and figured out, that the using "keep" like done here, means it is used as the stepsize.

>>> L=range(10)
>>> L
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[::1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[::2]
[0, 2, 4, 6, 8]

So, it makes absolutely sense that all my backups got removed when calling "prune 1".

Now, is it the desired behaviour? Like I said, I would expect sth. like

for backup_id in s3.backups()[keep + 1::]:

>>> keep=1
>>> L[keep+1::]
[2, 3, 4, 5, 6, 7, 8, 9]

Add a license

This is a great little script. Would be nice if we had a license.

Also, adding a setup.py and making it available on PyPI/pip would be nice!

Restore doesn't seem to work

Hi, I've just tried backing up and restoring with a fresh jenkins instance. Is there something I need to do other than running the command? Also just wondering if plugins are included in the backup.

Much appreciated.

s3tranfer / botocore dependencies need update

We are seeing the following error when executing the script in the current version. When we explicitly reinstall s3transfer and botocore to match the desired versions, described in the setup.py file, the script is running fine again.

+ backup-jenkins ...
Traceback (most recent call last):
  File "/usr/bin/backup-jenkins", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3086, in <module>
    @_call_aside
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3070, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3099, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 576, in _build_master
    return cls._build_from_requirements(__requires__)
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 589, in _build_from_requirements
    dists = ws.resolve(reqs, Environment())
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 778, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 's3transfer==0.1.11' distribution was not found and is required by jenkins-backup-s3

Performing a pip show s3transfer shows that version 0.1.13 is installed locally. pip show botocore shows 1.10.0 to be installed on my system.

I digged into the boto 1.4.7 dependencies (https://github.com/boto/boto3/releases/tag/1.4.7) and found the following line in their setup file:

requires = [
    'botocore>=1.7.0,<1.8.0',
    'jmespath>=0.7.1,<1.0.0',
    's3transfer>=0.1.10,<0.2.0'
]

I'm not familiar with the python setup-flow, but from my point of view, these requirement definitions do collide with the ones stated in this project. Overall, why do you explicitly define the s3transfer and botocore dependencies at all? Aren't they transitively added?

Quick question

Hi there,
I'm just wondering if this has been tested for situations where there are job(s) running while a backup is being attempted to be created or restored. I've spent a bit of time researching remotely disabling jobs via the Jenkins restul api but have found that this seems impossible to do for multibranch pipeline jobs.

Cheers,
Mike

pruning object does not work.

if i use this script to make a backup i cannot delete with the script using the prune action. i can delete the object manually/directly however. i suspect there is something about the IAM role requirements that is not listed, or some libs are outdated.

ex command run that fails:
$ backup-jenkins --bucket <BUCKET_NAME> --bucket-region us-west-2 prune 1

here is error:
Traceback (most recent call last):
File "/usr/local/bin/backup-jenkins", line 11, in
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/jenkins_backup_s3/backup.py", line 249, in main
cli(obj={})
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 722, in call
return self.main(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/jenkins_backup_s3/backup.py", line 209, in prune
_delete_command(backup_id, ctx.obj['BUCKET'], ctx.obj['BUCKET_PREFIX'], ctx.obj['BUCKET_REGION'], dry_run)
File "/usr/local/lib/python2.7/dist-packages/jenkins_backup_s3/backup.py", line 186, in _delete_command
s3.delete(backup_id)
File "/usr/local/lib/python2.7/dist-packages/jenkins_backup_s3/backup.py", line 62, in delete
s3_object = self.s3.Object(self.__bucket, key).delete()
File "/usr/local/lib/python2.7/dist-packages/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/boto3/resources/action.py", line 83, in call
response = getattr(parent.meta.client, operation_name)(**params)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 612, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the DeleteObject operation: Access Denied

Botocore 1.13.38 unsupported version of python-dateutil 2.8.0+

I encountered this error when I use jenkins in docker with tag 2.197 >

jenkins@0bf6fe53e749:/$ backup-jenkins --bucket-region=us-west-XX \
>                --bucket=XXXXXXXXXXXX \
>                --bucket-prefix=jenkins-backups/$JENKINS_SERVER \
>                create \
>                --jenkins-home=$JENKINS_HOME \
>                --include-builds
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 655, in _build_master
    ws.require(__requires__)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 963, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 854, in resolve
    raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.ContextualVersionConflict: (python-dateutil 2.8.1 (/usr/local/lib/python3.5/dist-packages), Requirement.parse('python-dateutil<2.8.1,>=2.1; python_version >= "2.7"'), {'botocore'})

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/backup-jenkins", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3019, in <module>
    @_call_aside
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3003, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 657, in _build_master
    return cls._build_from_requirements(__requires__)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 670, in _build_from_requirements
    dists = ws.resolve(reqs, Environment())
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 854, in resolve
    raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.ContextualVersionConflict: (python-dateutil 2.8.1 (/usr/local/lib/python3.5/dist-packages), Requirement.parse('python-dateutil<2.8.1,>=2.1; python_version >= "2.7"'), {'botocore'})

I managed to fix this
ldoming@6c40b2e

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.