jenkinsjenkins-plugins

Move Jenkins job to cold storage


I want to free up space on my Jenkins server and move old projects into cold storage. We have jobs we have not touched for years, but since they were released products we don't want to lose them entirely.

I found the Shelve Project plugin for Jenkins which says:

the data is archived into a tar file and stored on the server

That's a great first step. Now I'd like to find those tar files, and move them to another server.

  1. Where are the files?
  2. Can I just remove them, or is there some index file I must edit as well?
  3. Are the jobs archived, or just the configuration?

Solution

  • I had better luck just doing this directly with a small bash script, shown below.

    I moved my projects to cold storage by the following:

    1. Open the Jenkins UI.
    2. Press Manage Jenkins > Prepare for Shutdown > Prepare for Shutdown.
    3. Wait for the server to become idle.
    4. Run the script for each project. This deletes the job information from the Jenkins folders and moves it to a zipped tar file.
    5. Open the Jenkins UI.
    6. Press Manage Jenkins > Reload Configuration from Disk. This refreshes the UI without the jobs that were removed.
    7. Press Manage Jenkins > Prepare for Shutdown > Cancel Shutdown. This brings Jenkins fully online without those offloaded jobs.
    8. Move each of the files created in step 4 to cold storage.

    You can recover them in a similar way by uncompressing the tar file, copying the files to their original location, and reloading Jenkins. After doing that, all the job artifacts and configuration history show properly.

    To do this, I created this script:

    #!/bin/bash
    set -e
    
    # Start of installation-specific configuration
    JENKINS_HOME=/var/lib/jenkins
    BUILD_DIR=/data/jenkins/builds
    ARCHIVE_ROOT=~/testArchive
    # End of installation-specific configuration
    
    if [ "$#" -ne 1 ]; then
        echo
        echo "Usage: "
        echo "$0 JenkinsJobName"
        echo
        echo "Error: Must specify job argument"
        echo
        exit 1
    fi
    fileToArchive="$1"
    
    projArchiveRoot="${ARCHIVE_ROOT}/${fileToArchive}"
    
    # First archive the jobs folder
    mkdir -p "${projArchiveRoot}/${JENKINS_HOME}/jobs"
    mv "${JENKINS_HOME}/jobs/${fileToArchive}" \
       "${projArchiveRoot}/${JENKINS_HOME}/jobs/"
    
    # Next archive the configuration history (assuming you use that plug-in)
    mkdir -p "${projArchiveRoot}/${JENKINS_HOME}/config-history/jobs"
    mv "${JENKINS_HOME}/config-history/jobs/${fileToArchive}" \
       "${projArchiveRoot}/${JENKINS_HOME}/config-history/jobs/"
    
    # If you have modified your ${JENKINS_HOME}/config.xml to have a specific
    # buildsDir you must also back those up.  If you modify these on a
    # project-by-project basis using the Advanced options, you must account
    # for the build artifacts yourself
    mkdir -p "${projArchiveRoot}/${BUILD_DIR}"
    mv  "${BUILD_DIR}/${fileToArchive}" \
        "${projArchiveRoot}/${BUILD_DIR}"
    
    # Now make a single tar file with all the contents for one project
    pushd "${projArchiveRoot}"
    tar cvzf "${projArchiveRoot}.tar.gz" *
    popd
    rm -rf ${projArchiveRoot}
    
    # And now instruct the user we're done
    echo Archived \"$fileToArchive\" please reload configuration from disk
    

    You likely want to back up your server before you start, and create a test project with configuration history and build artifacts to test with. Once you're comfortable, you can offload huge chunks of data with a few clicks.