This guide shows how to back up and restore an
Apache Derby database in a
Spring Boot project to protect and recover data easily.
1. Configure Derby
In
application.properties
:
spring.datasource.url=jdbc:derby:/app/data/derbyDB;create=true
spring.datasource.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver
spring.jpa.hibernate.ddl-auto=update
This stores Derby data in a file-based directory
/app/data/derbyDB
.
2. Create Entities and Repositories
For example, a simple
Customer
entity:
import jakarta.persistence.*;
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// getters and setters
}
Repository:
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}
3. Backup Derby Database
Derby provides a backup command. Run inside
IJ tool or programmatically:
CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE('/backup/derby_backup');
This copies the current database files into
/backup/derby_backup
.
4. Restore Derby Database
To restore, point Derby to the backup directory:
spring.datasource.url=jdbc:derby:/backup/derby_backup;create=true
Your Spring Boot app will now use the restored database.
5. Automate Backup with Spring
You can also run backups programmatically:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class DerbyBackup {
public static void backup() throws Exception {
try (Connection conn = DriverManager.getConnection(
"jdbc:derby:/app/data/derbyDB")) {
Statement stmt = conn.createStatement();
stmt.executeUpdate(
"CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE('/backup/derby_backup')");
}
}
}
6. Test Backup and Restore
- Insert data via your Spring Boot app.
- Run backup procedure.
- Change DB URL to
/backup/derby_backup
.
- Restart app and confirm restored data.
image quote pre code