Skip to main content

Backup and Restore Tomcat

Hands-On Lab

 

Photo of

Training Architect

Length

00:45:00

Difficulty

Intermediate

Welcome to this hands-on lab. Here, we are using Apache's Tomcat, a Java-based application platform built around deploying and managing Java Web Applications. The server provided in this lab environment is running Red Hat Enterprise 8 and has Tomcat 9 pre-installed for you, available on port 8080.

In this lab you will back up your Tomcat instance. Then you will break it by deleting one of the management GUI applications.

Afterward, you will restore the application from the backup you took earlier.

The Tomcat instance is located under /usr/local/tomcat9, and the files are owned by the tomcat user.

Your task is to restore the missing files from that backup, and get the Manager GUI application working again.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Backup and Restore Tomcat

Introduction

Welcome to this hands-on lab. Here, we are using Apache's Tomcat, a Java-based application platform built around deploying and managing Java Web Applications. The server provided in this lab environment is running Red Hat Enterprise 8 and has Tomcat 9 pre-installed for you, available on port 8080.

In this lab you will back up your Tomcat instance. Then you will break it by deleting one of the management GUI applications.

Afterward, you will restore the application from the backup you took earlier.

The Tomcat instance is located under /usr/local/tomcat9, and the files are owned by the tomcat user.

Your task is to restore the missing files from that backup, and get the Manager GUI application working again.

The Scenario

While working with your Tomcat instance, a staff member accidentally removed the wrong application. They removed one of the apps provided by Tomcat to manage the server.

You have been tasked with restoring that management application from a backup. Restore only that application, not everything from the backup.

Once the manager application has been restored, test you can see the application on your Tomcat server and are able to log into it.

For this hands-on lab, you will create a backup of the Tomcat server. Then you will break it by deleting the Management application. Afterward, you will bring it back to operational status by extracting the backup you made, and restoring the deleted application.

Your Tomcat instance can be found under /usr/local/tomcat9.

You will complete the hands-on lab by performing the tasks as mentioned in the tasks list.

If the hands-on lab is successful, you will be able to log into the manager GUI application.

Note: It may take 3 to 10 minutes after the hands-on lab has started before you are able to log into the server. This is fine and will not affect the performance of the tasks for the hands-on lab.

Logging In

Use the credentials provided on the hands-on lab overview page, and log in to the provided server as cloud_user. Note that this is for connecting to the server and also for the Tomcat GUI.

Back up Your Tomcat Application to a Folder Called /backups

Log into the Tomcat server, via SSH, and gain root privileges:

sudo su -

Now get into the the directory where the Tomcat web apps reside:

cd /usr/local/tomcat9/webapps/

Run a quick ll just to see what's in here. Note the manager directory. That is the one we'll be breaking.

Create the folder we will back up to:

mkdir /backups

Now get into that directory:

cd /backups

Now we'll create the actual backup:

tar -czf tomcat-backup-`date +%Y%m%d`-.tar.gz /usr/local/tomcat9

When the command is finished running, run a quick ll to make sure the backup was created.

Test to Ensure You Can Access the Preconfigured Tomcat Instances on Port 8080

Using the Tomcat server's public IP address and port 8080, connect to the Tomcat GUI and log in. For example, if an IP address shown was 3.92.152.3, then you would put the following into your web browser:

http://3.92.152.3:8080

You should see the GUI. This is to test that Tomcat is up and running. If we click on the Manager app, we should get a login prompt. Press Cancel.

Delete the Manager Application from /usr/local/tomcat9/webapps

Back in the terminal, we'll head into the the Tomcat directory again:

cd /usr/local/tomcat9/webapps/

Now let's delete the Manager app:

rm -rf manager

Test in the web browser again by refreshing and clicking on the Manager app again. We'll get a 404 error this time, because we broke it.

Restore the Entire Manager Folder to Your Tomcat Instance's webapps Folder, Then Test to Make Sure It Works

To fix it, we need to get back to the terminal, and into the backups directory:

cd /backups

Extract the backup file:

tar -xvf tomcat-backup-`date +%Y%m%d`-.tar.gz

Note that date +%Y%m%d is a command that outputs the date in yyyymmdd format. Your file will actually be called something like tomcat-backup-20200510-.tar.gz, tomcat-backup-20200830-.tar.gz, etc., depending on the current date when this command is run.

Now get into the webapps directory of the extracted backup:

cd usr/local/tomcat9/webapps

Notice there's no preceeding forward slash there. This usr directory is in our backups directory (so it's /backups/usr), and is not the /usr where Tomcat lives.

If we run an ll, we'll see a manager directory. Let's move this back to the Tomcat installation:

mv manager /usr/local/tomcat9/webapps/

If we go back to Tomcat's webapps directory, we should see that the manager folder made the trip. Let's move:

cd /usr/local/tomcat9/webapps

Now run a directory listing (ll) and we should see these:

docs
examples
host-manager
manager
ROOT

It's safe to test in the web app again. Get back to the main Tomcat page, and click on the Manager App button. We should get a login prompt. Remember, the username and password are the same as for your lab username and password.

Conclusion

We backed up our Tomcat instance, faked accidentally deleting an application, and then were able to restore it from the backup. Congratulations on completing this hands-on lab.