Backup and restore jenkins on ubuntu server

This article will follow my experiences with backing up jenkins from one ubuntu server machine and restoring it in another.
Jenkins has the git addon and .ssh keys for accessing a bitbucket account.

Backup jenkins on old-server

To backup jenkins, all you need to do is copy the contents of the jenkins home directory which on my apache2/tomcat7 ubuntu server is located at /usr/share/tomcat7/.jenkins

Restore jenkins backup on the new server (server name: userver)

On the new server (userver), remove all files within the .jenkins directory
$ cd /usr/share/tomcat7/.jenkins
$ sudo rm -r *

Copy the contents of your .jenkins backup your .jenkins dir
$ sudo cp -r /your-backup-location/.jenkins/* .

Change the ownership of the copied files to tomcat7:tomcat7
$ sudo chown -R tomcat7:tomcat7 *

Restart the tomcat7 service
$ sudo service tomcat7 restart

If required, you can also restart jenkins from the url
userver:8080/jenkins/restart

You should be able to login as normal now from url: userver:8080/jenkins

Make sure to adjust your scripts for any new directory structres that might be different from your old server.
In my case some backup scripts will need to be modified… as the backup directory locations are different on userver.

Modify the Jenkins location:
Jenkins > Manage Jenkins > Configuration
change Jenkins URL to poin to the new server: http://userver:8080/jenkins/

Execution problems

Running my jenkins backup job gave the following error:
ln builds/lastSuccessfulBuild /usr/share/tomcat7/.jenkins/jobs/backup-jenkins/lastSuccessful failed
java.nio.file.DirectoryNotEmptyException: /usr/share/tomcat7/.jenkins/jobs/backup-jenkins/lastSuccessful
at sun.nio.fs.UnixFileSystemProvider.implDelete(UnixFileSystemProvider.java:242)
at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(AbstractFileSystemProvider.java:108)
at java.nio.file.Files.deleteIfExists(Files.java:1118)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at hudson.Util.createSymlinkJava7(Util.java:1233)
at hudson.Util.createSymlink(Util.java:1151)
at hudson.model.Run.createSymlink(Run.java:1840)
at hudson.model.Run.updateSymlinks(Run.java:1821)
at hudson.model.Run.execute(Run.java:1736)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:410)
ln builds/lastStableBuild /usr/share/tomcat7/.jenkins/jobs/backup-jenkins/lastStable failed
java.nio.file.DirectoryNotEmptyException: /usr/share/tomcat7/.jenkins/jobs/backup-jenkins/lastStable
at sun.nio.fs.UnixFileSystemProvider.implDelete(UnixFileSystemProvider.java:242)
at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(AbstractFileSystemProvider.java:108)
at java.nio.file.Files.deleteIfExists(Files.java:1118)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at hudson.Util.createSymlinkJava7(Util.java:1233)
at hudson.Util.createSymlink(Util.java:1151)
at hudson.model.Run.createSymlink(Run.java:1840)
at hudson.model.Run.updateSymlinks(Run.java:1822)
at hudson.model.Run.execute(Run.java:1736)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:410)

Apparently this is caused by the fact the following directories are not empty (for some reason…):
/usr/share/tomcat7/.jenkins/jobs/your-job-name/lastSuccessful
/usr/share/tomcat7/.jenkins/jobs/your-job-name/lastStable
So clearing them resolved this:
$ cd /usr/share/tomcat7/.jenkins/jobs/your-job-name/lastSuccessful
$ sudo rm *
$ cd /usr/share/tomcat7/.jenkins/jobs/your-job-name/lastStable
$ sudo rm *

Leave a Reply

Your email address will not be published. Required fields are marked *