{"id":29987867,"url":"https://github.com/SeunMatt/mysql-backup4j","last_synced_at":"2025-08-04T23:04:15.895Z","repository":{"id":37269934,"uuid":"122863190","full_name":"SeunMatt/mysql-backup4j","owner":"SeunMatt","description":"mysql-backup4j is a library for programmatically exporting mysql databases and sending the zipped dump to email, Amazon S3, Google Drive or any other cloud storage of choice","archived":false,"fork":false,"pushed_at":"2024-07-31T09:54:56.000Z","size":169,"stargazers_count":133,"open_issues_count":9,"forks_count":74,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-07-26T23:23:38.176Z","etag":null,"topics":["database","database-management","java","mysql","mysql-backup4j"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SeunMatt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-25T18:23:14.000Z","updated_at":"2025-06-26T06:23:38.000Z","dependencies_parsed_at":"2024-07-31T08:48:55.728Z","dependency_job_id":"ad583b77-3e29-4d11-87a5-5a1310a5e5a3","html_url":"https://github.com/SeunMatt/mysql-backup4j","commit_stats":{"total_commits":72,"total_committers":6,"mean_commits":12.0,"dds":"0.23611111111111116","last_synced_commit":"5f12bdfd0c2af5ce57e4b1caeab8093f208cd4bb"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/SeunMatt/mysql-backup4j","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SeunMatt%2Fmysql-backup4j","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SeunMatt%2Fmysql-backup4j/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SeunMatt%2Fmysql-backup4j/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SeunMatt%2Fmysql-backup4j/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SeunMatt","download_url":"https://codeload.github.com/SeunMatt/mysql-backup4j/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SeunMatt%2Fmysql-backup4j/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268800112,"owners_count":24309397,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","database-management","java","mysql","mysql-backup4j"],"created_at":"2025-08-04T23:01:29.092Z","updated_at":"2025-08-04T23:04:15.884Z","avatar_url":"https://github.com/SeunMatt.png","language":"Java","funding_links":[],"categories":["数据库开发"],"sub_categories":[],"readme":"mysql-backup4j\r\n==============\r\n\r\n[![SeunMatt](https://circleci.com/gh/SeunMatt/request-validator.svg?style=svg)](https://github.com/SeunMatt/mysql-backup4j)\r\n\r\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.smattme/mysql-backup4j/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.smattme/mysql-backup4j/badge.svg)\r\n\r\nmysql-backup4j is a library for programmatically exporting mysql databases \r\nand sending the zipped dump to email, Amazon S3, Google Drive or any other cloud storage of choice\r\n\r\n**It gives the developer access to the generated zip file and the generated SQL query string**\r\n for use in other part of the application. \r\n\r\n**It also provides a method for importing the SQL exported by the tool - programmatically.**\r\n\r\nInstallation\r\n============\r\nThe artifact is available on Maven Central and can be added to the project's pom.xml:\r\n\r\n```xml\r\n\u003cdependency\u003e\r\n    \u003cgroupId\u003ecom.smattme\u003c/groupId\u003e\r\n    \u003cartifactId\u003emysql-backup4j\u003c/artifactId\u003e\r\n    \u003cversion\u003e1.3.0\u003c/version\u003e\r\n\u003c/dependency\u003e\r\n```\r\n\r\nThe latest version can be found [here](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.smattme%22%20a%3A%22mysql-backup4j%22)\r\n\r\nUsage\r\n=====\r\nThe minimum configuration required for the library is the database name, username and password.\r\n\r\nHowever, if you want the backup file to be sent to your email automatically after backup, you must \r\nprovide email configurations as well.\r\n\r\n```java\r\n//required properties for exporting of db\r\nProperties properties = new Properties();\r\nproperties.setProperty(MysqlExportService.DB_NAME, \"database-name\");\r\nproperties.setProperty(MysqlExportService.DB_USERNAME, \"root\");\r\nproperties.setProperty(MysqlExportService.DB_PASSWORD, \"root\");\r\nproperties.setProperty(MysqlExportService.DB_HOST, \"localhost\");\r\nproperties.setProperty(MysqlExportService.DB_PORT, \"3306\");\r\n        \r\n//properties relating to email config\r\nproperties.setProperty(MysqlExportService.EMAIL_HOST, \"smtp.mailtrap.io\");\r\nproperties.setProperty(MysqlExportService.EMAIL_PORT, \"25\");\r\nproperties.setProperty(MysqlExportService.EMAIL_USERNAME, \"mailtrap-username\");\r\nproperties.setProperty(MysqlExportService.EMAIL_PASSWORD, \"mailtrap-password\");\r\nproperties.setProperty(MysqlExportService.EMAIL_FROM, \"test@smattme.com\");\r\nproperties.setProperty(MysqlExportService.EMAIL_TO, \"backup@smattme.com\");\r\n\r\n//optional email configs\r\nproperties.setProperty(MysqlExportService.EMAIL_SSL_PROTOCOLS, \"TLSv1.2\");\r\nproperties.setProperty(MysqlExportService.EMAIL_SMTP_AUTH_ENABLED, \"true\");\r\nproperties.setProperty(MysqlExportService.EMAIL_START_TLS_ENABLED, \"true\");\r\n\r\n//set the outputs temp dir\r\nproperties.setProperty(MysqlExportService.TEMP_DIR, new File(\"external\").getPath());\r\n\r\nMysqlExportService mysqlExportService = new MysqlExportService(properties);\r\nmysqlExportService.export();\r\n```\r\n\r\nCalling `mysqlExportService.export();` will export the database and save the dump temporarily in the configured `TEMP_DIR`\r\n\r\nIf an email config is supplied, the dump will be sent as an attachment. Finally, when all operations are completed the \r\ntemporary dir is cleared and deleted.\r\n\r\nIf you want to get the generated backup file as a Java `File` object, you need to specify this property as part of the \r\nconfiguration:\r\n\r\n```java\r\n//...\r\nproperties.setProperty(MysqlExportService.PRESERVE_GENERATED_ZIP, \"true\");\r\nproperties.setProperty(MysqlExportService.PRESERVE_GENERATED_SQL_FILE, \"true\");\r\n```\r\n\r\nand then you can call this method:\r\n\r\n```java\r\nFile file = mysqlExportService.getGeneratedZipFile();\r\n```\r\n\r\nFinally, let's say for some reason you want the generated SQL string you can do this:\r\n\r\n```java\r\nString generatedSql = mysqlExportService.getGeneratedSql();\r\n```\r\n\r\nOther parameters are:\r\n\r\n```java\r\nproperties.setProperty(MysqlExportService.ADD_IF_NOT_EXISTS, \"true\");\r\nproperties.setProperty(MysqlExportService.JDBC_DRIVER_NAME, \"com.mysql.cj.jdbc.Driver\");\r\nproperties.setProperty(MysqlExportService.JDBC_CONNECTION_STRING, \"jdbc:mysql://localhost:3306/database-name?useUnicode=true\u0026useJDBCCompliantTimezoneShift=true\u0026useLegacyDatetimeCode=false\u0026serverTimezone=UTC\u0026useSSL=false\");\r\n```\r\n\r\nThey are explained in a detailed manner in this [tutorial](https://smattme.com/blog/technology/how-to-backup-mysql-database-programmatically-using-mysql-backup4j)\r\n\r\nImporting a Database\r\n--------------------\r\nTo import a database, you need to use the ImportService like so:\r\n\r\n```java\r\nString sql = new String(Files.readAllBytes(Paths.get(\"path/to/sql/dump/file.sql\")));\r\n\r\nboolean res = MysqlImportService.builder()\r\n        .setDatabase(\"database-name\")\r\n        .setSqlString(sql)\r\n        .setUsername(\"root\")\r\n        .setPassword(\"root\")\r\n        .setHost(\"localhost\")\r\n        .setPort(\"3306\")\r\n        .setDeleteExisting(true)\r\n        .setDropExisting(true)\r\n        .importDatabase();\r\n        \r\nassertTrue(res);\r\n```\r\n\r\nFirst get SQL as a String and then pass it to the import service with the right configurations.\r\n\r\nAlternatively, you can also use the `.setJdbcConnString(jdbcURL)` method on the import service.\r\n\r\ne.g. \r\n```java\r\nboolean res = MysqlImportService.builder()\r\n                .setSqlString(generatedSql)\r\n                .setJdbcConnString(\"jdbc:mysql://localhost:3306/backup4j_test?useUnicode=true\u0026useJDBCCompliantTimezoneShift=true\u0026useLegacyDatetimeCode=false\u0026serverTimezone=UTC\u0026useSSL=false\")\r\n                .setUsername(\"db-username\")\r\n                .setPassword(\"db-password\")\r\n                .setDeleteExisting(true)\r\n                .setDropExisting(true)\r\n                .importDatabase();\r\n```\r\n\r\n`setDeleteExisting(true)` will **delete all data** from existing tables in the target database. \r\n\r\nWhile `setDropExisting(true)` will **drop** the table. \r\n\r\nSupplying `false` to these functions will disable their respective actions.\r\n\r\n\r\n**NOTE: The import service is only guaranteed to work with SQL files generated by the export service of this library**\r\n\r\nCHANGELOG\r\n=========\r\nv1.2.1\r\n    - Raises a new runtime exception `MysqlBackup4JException` if the required properties are not configured\r\n\r\nAuthor\r\n======\r\nSeun Matt [smattme.com](https://smattme.com) with :green_heart:\r\n\r\nContributions and Support\r\n=========================\r\nIf you want to create a new feature, though not compulsory, but it will be helpful to reach out to me first before proceeding.\r\n\r\nTo avoid a scenario where you submit a PR for an issue that someone else is working on already.\r\n\r\n\r\nTutorials / Articles\r\n====================\r\n- [https://smattme.com/blog/technology/how-to-backup-mysql-database-programmatically-using-mysql-backup4j](https://smattme.com/blog/technology/how-to-backup-mysql-database-programmatically-using-mysql-backup4j)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSeunMatt%2Fmysql-backup4j","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSeunMatt%2Fmysql-backup4j","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSeunMatt%2Fmysql-backup4j/lists"}