Java application and database backup

In the world of web it is important to take backup of our site and database frequently. We don’t know when we’d come accross unexpected error which causes our site stop working. Here I have developed an application in Java, Servlet and JSP which will help you to take backup as per your wish whenever you want to take backup. This application takes backup from your workspace for the current web application. While taking backup it creates zip files in the workspace and you will be given choice whether you want to download or delete once the backup is successfully done. You can select what type of backup you want to take like site, database or site & database together. You can also see the backup files are saved to the physical workspace location of your hard drive.

If you have any query please feel free to send a query in comment section of the article or you can also contact me through Contact page.

Prerequisites

Eclipse or any JEE based IDE
Tomcat v7
JDK 1.6
MySQL 5.x
Jar files – jstl-1.2.jar, mysql-connector-java-5.1.23-bin.jar

Final output

Home page

Java application and database backup

Select option for which you want backup – database, site or both

Java application and database backup

Database backup

Java application and database backup

Site Backup

Java application and database backup

Database and Site backup

Java application and database backup

 

This tutorial shows step by step how you can write code and make it works. Put the jar files under WEB-INF/lib directory.

Step 1. Create a Dynamic Web Project or Web Application according to your IDE. All the required project structure gets created.

Step 2. Now we will modify our deployment descriptor – web.xml as below. We won’t make any entry for Servlets in this file because Tomcat v7 supports Servlet 3.x version and we will get benefits of using annotations. We have only welcome file in web.xml file for our home page.

 

Step 3. We need a properties file or you may also hardcode the configuration values in Java class itself but it’s a good practice to put the configuration related properties in the property files.

application.properties – put this under src dirctory

 

Step 4. We will have some constants which will be used in our application.

Constants.java

 

Step 5. We will have one class AppUtils.java for various utility methods like getting all the files from a directory recursively, adding files one by one to ZipEntry, writing to zip file. This file basically takes backup for application.

 

Step 6. Next we will have one class DBUtils.java for various utility methods like database connection, querying database, getting database dump, writing to zip file. This file basically takes backup for database.

 

Step 7. We need to read the property file, so for this we have one class PropertiesUtils.java

 

Step 8. We want that our properties get read automatically when the application starts up so that we can avoid overhead for manual reading of the key/value pairs from the property file. So for this we need a Servlet Listener class. You can also achieve the same thing using Servlet by using configuration.

 

Step 9. We need some servlet classes for backup the application and download or delete the backup files. So here we will create one BackupServlet for taking the backup.

 

Step 10. We need another Servlet for downloading the backup.

DownloadServlet.java

 

Step 11. We need one more servlet for deleting the backup from user interface.

DeleteServlet.java

 

Step 12. View part – index.jsp

 

I am done. If you have better idea or if you feel that you want to improve the article please let me know.

Thanks for your reading. Please do not forget to leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.