├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── src ├── test │ └── java │ │ └── com │ │ └── itning │ │ └── mysql_backup_to_sqlite │ │ └── MysqlBackupToSqliteApplicationTests.java └── main │ ├── java │ └── com │ │ └── itning │ │ └── mysql_backup_to_sqlite │ │ ├── source │ │ ├── Source.java │ │ └── MySQLSource.java │ │ ├── entry │ │ ├── ColumnInfo.java │ │ ├── DataEntry.java │ │ ├── RowData.java │ │ └── TargetResult.java │ │ ├── MysqlBackupToSqliteApplication.java │ │ ├── target │ │ ├── Target.java │ │ ├── SqlFileTarget.java │ │ └── SqliteTarget.java │ │ ├── plugin │ │ ├── Plugin.java │ │ ├── SendMailPlugin.java │ │ └── TencentCloudCOSPlugin.java │ │ ├── config │ │ ├── BootBeanGetter.java │ │ ├── DynamicSchedulingConfig.java │ │ └── BackupProperties.java │ │ └── engine │ │ └── JobEngine.java │ └── resources │ └── application.properties ├── README.md ├── .github ├── dependabot.yml └── workflows │ └── compile_test.yml ├── .gitignore ├── pom.xml ├── mvnw.cmd └── mvnw /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itning/mysql_backup_to_sqlite/main/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /src/test/java/com/itning/mysql_backup_to_sqlite/MysqlBackupToSqliteApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class MysqlBackupToSqliteApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mysql_backup_to_sqlite MySQL数据库备份 2 | 3 | [![compile](https://github.com/itning/mysql_backup_to_sqlite/actions/workflows/compile_test.yml/badge.svg)](https://github.com/itning/mysql_backup_to_sqlite/actions/workflows/compile_test.yml) 4 | 5 | 支持数据源: 6 | 7 | - [x] MySQL 8 | 9 | 支持目标源 10 | 11 | - [x] SQLite 12 | - [x] SQL File 13 | - [x] 腾讯云COS 14 | 15 | 支持CRON表达式 定时备份到目标源中 16 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/source/Source.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.source; 2 | 3 | import com.itning.mysql_backup_to_sqlite.entry.DataEntry; 4 | 5 | import java.util.Set; 6 | 7 | /** 8 | * @author ning.wang 9 | * @since 2023/4/3 10:13 10 | */ 11 | public interface Source { 12 | DataEntry start(String mysqlUrl, String mysqlUsername, String mysqlPassword, Set tables) throws Exception; 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/entry/ColumnInfo.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.entry; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @author ning.wang 7 | * @since 2023/4/3 12:16 8 | */ 9 | @Data 10 | public class ColumnInfo { 11 | private int index; 12 | private String columnName; 13 | private String columnTypeName; 14 | /** 15 | * @see java.sql.Types 16 | */ 17 | private int columnType; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/MysqlBackupToSqliteApplication.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MysqlBackupToSqliteApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(MysqlBackupToSqliteApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/target/Target.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.target; 2 | 3 | import com.itning.mysql_backup_to_sqlite.entry.DataEntry; 4 | import com.itning.mysql_backup_to_sqlite.entry.TargetResult; 5 | 6 | import java.io.File; 7 | import java.util.List; 8 | 9 | /** 10 | * @author ning.wang 11 | * @since 2023/4/3 10:13 12 | */ 13 | public interface Target { 14 | TargetResult start(List outPutDir, String jobName, DataEntry dataEntry) throws Exception; 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/entry/DataEntry.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.entry; 2 | 3 | import lombok.Data; 4 | 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * @author ning.wang 11 | * @since 2023/4/3 10:32 12 | */ 13 | @Data 14 | public class DataEntry { 15 | 16 | private final Map> columnInfoMap = new HashMap<>(); 17 | private final Map> dataInfoMap = new HashMap<>(); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/entry/RowData.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.entry; 2 | 3 | import lombok.Data; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | /** 9 | * @author ning.wang 10 | * @since 2023/4/3 12:16 11 | */ 12 | @Data 13 | public class RowData { 14 | 15 | private List itemData = new ArrayList<>(); 16 | 17 | @Data 18 | public static class ItemData { 19 | private int columnIndex; 20 | private Object value; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "maven" 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "daily" 12 | open-pull-requests-limit: 50 13 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/entry/TargetResult.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.entry; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | import java.io.File; 7 | import java.util.List; 8 | 9 | /** 10 | * @author ning.wang 11 | * @since 2023/4/3 15:09 12 | */ 13 | @AllArgsConstructor 14 | @Data 15 | public class TargetResult { 16 | private List files; 17 | private TargetType targetType; 18 | 19 | public enum TargetType { 20 | SQLITE, 21 | SQL_FILE, 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/plugin/Plugin.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.plugin; 2 | 3 | import com.itning.mysql_backup_to_sqlite.config.BackupProperties; 4 | import com.itning.mysql_backup_to_sqlite.entry.DataEntry; 5 | import com.itning.mysql_backup_to_sqlite.entry.TargetResult; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author ning.wang 11 | * @since 2023/4/3 12:42 12 | */ 13 | public interface Plugin { 14 | void start(String name, BackupProperties.Job job, DataEntry dataEntry, List results) throws Exception; 15 | } 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | application-local.properties 35 | application-nas.properties 36 | *.db 37 | *.sql 38 | -------------------------------------------------------------------------------- /.github/workflows/compile_test.yml: -------------------------------------------------------------------------------- 1 | name: compile 2 | 3 | on: 4 | workflow_dispatch: 5 | pull_request: 6 | push: 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Set up JDK 17 15 | uses: actions/setup-java@v1 16 | with: 17 | java-version: 17 18 | 19 | - name: Cache local Maven repository 20 | uses: actions/cache@v4 21 | with: 22 | path: ~/.m2/repository 23 | key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} 24 | restore-keys: | 25 | ${{ runner.os }}-maven- 26 | - name: compile with Maven 27 | run: mvn -B compile -DskipTests --file pom.xml 28 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8799 2 | spring.application.name=mysql_backup_to_sqlite 3 | spring.task.scheduling.pool.size=5 4 | 5 | backup.job.yunshu_nas.cron= 6 | backup.job.yunshu_nas.mysql-source.mysql-url= 7 | backup.job.yunshu_nas.mysql-source.mysql-username= 8 | backup.job.yunshu_nas.mysql-source.mysql-password= 9 | backup.job.yunshu_nas.mysql-source.mysql-tables= 10 | backup.job.yunshu_nas.sqlite-target.out-put-dir= 11 | backup.job.yunshu_nas.sql-file-target.out-put-dir= 12 | backup.job.yunshu_nas.tencent-cloud-cos-plugin.secret-id= 13 | backup.job.yunshu_nas.tencent-cloud-cos-plugin.secret-key= 14 | backup.job.yunshu_nas.tencent-cloud-cos-plugin.region-name= 15 | backup.job.yunshu_nas.tencent-cloud-cos-plugin.bucket-name= 16 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.7/apache-maven-3.8.7-bin.zip 18 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar 19 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/config/BootBeanGetter.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.config; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.ApplicationContextAware; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * @author itning 10 | * @since 2023/4/6 22:22 11 | */ 12 | 13 | @Component 14 | public class BootBeanGetter implements ApplicationContextAware { 15 | private static ApplicationContext applicationContext; 16 | 17 | @Override 18 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 19 | setApplicationContextStatic(applicationContext); 20 | } 21 | 22 | public static T getClassBean(Class requiredType) { 23 | return applicationContext.getBean(requiredType); 24 | } 25 | 26 | public static Object getBean(String name) { 27 | return applicationContext.getBean(name); 28 | } 29 | 30 | public static ApplicationContext getApplicationContext() { 31 | return applicationContext; 32 | } 33 | 34 | public static void setApplicationContextStatic(final ApplicationContext applicationContext) { 35 | BootBeanGetter.applicationContext = applicationContext; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/config/DynamicSchedulingConfig.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.config; 2 | 3 | import com.itning.mysql_backup_to_sqlite.engine.JobEngine; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.scheduling.annotation.EnableScheduling; 8 | import org.springframework.scheduling.annotation.SchedulingConfigurer; 9 | import org.springframework.scheduling.config.ScheduledTaskRegistrar; 10 | 11 | import java.util.Map; 12 | import java.util.Objects; 13 | 14 | /** 15 | * @author ning.wang 16 | * @since 2023/4/3 10:26 17 | */ 18 | @Slf4j 19 | @Configuration 20 | @EnableScheduling 21 | public class DynamicSchedulingConfig implements SchedulingConfigurer { 22 | 23 | private final BackupProperties backupProperties; 24 | 25 | @Autowired 26 | public DynamicSchedulingConfig(BackupProperties backupProperties) { 27 | this.backupProperties = backupProperties; 28 | } 29 | 30 | @Override 31 | public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { 32 | if (Objects.isNull(backupProperties.getJob())) { 33 | return; 34 | } 35 | for (Map.Entry item : backupProperties.getJob().entrySet()) { 36 | taskRegistrar.addCronTask(new JobEngine(item.getKey(), item.getValue()), item.getValue().getCron()); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/config/BackupProperties.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.config; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.io.File; 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.Set; 11 | 12 | /** 13 | * @author ning.wang 14 | * @since 2023/4/3 10:04 15 | */ 16 | @ConfigurationProperties(prefix = "backup") 17 | @Component 18 | @Data 19 | public class BackupProperties { 20 | private Map job; 21 | 22 | @Data 23 | public static class Job { 24 | private String cron; 25 | private MySQLSource mysqlSource; 26 | private SqliteTarget sqliteTarget; 27 | private SqlFileTarget sqlFileTarget; 28 | private TencentCloudCOSPlugin tencentCloudCosPlugin; 29 | } 30 | 31 | @Data 32 | public static class MySQLSource { 33 | private String mysqlUrl; 34 | private String mysqlUsername; 35 | private String mysqlPassword; 36 | private Set mysqlTables; 37 | } 38 | 39 | @Data 40 | public static class SqliteTarget { 41 | private List outPutDir; 42 | } 43 | 44 | @Data 45 | public static class SqlFileTarget { 46 | private List outPutDir; 47 | } 48 | 49 | @Data 50 | public static class TencentCloudCOSPlugin { 51 | /** 52 | * SECRETID和SECRETKEY请登录访问管理控制台进行查看和管理 53 | */ 54 | private String secretId; 55 | 56 | /** 57 | * SECRETID和SECRETKEY请登录访问管理控制台进行查看和管理 58 | */ 59 | private String secretKey; 60 | 61 | /** 62 | * 设置 bucket 的地域, COS 地域的简称请参照这里 63 | */ 64 | private String regionName; 65 | 66 | /** 67 | * 腾讯COS BucketName 68 | */ 69 | private String bucketName; 70 | 71 | private String dirName; 72 | 73 | private boolean uploadSqliteFile = true; 74 | private boolean uploadSqlFile = true; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/plugin/SendMailPlugin.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.plugin; 2 | 3 | import com.itning.mysql_backup_to_sqlite.config.BackupProperties; 4 | import com.itning.mysql_backup_to_sqlite.config.BootBeanGetter; 5 | import com.itning.mysql_backup_to_sqlite.entry.DataEntry; 6 | import com.itning.mysql_backup_to_sqlite.entry.RowData; 7 | import com.itning.mysql_backup_to_sqlite.entry.TargetResult; 8 | import lombok.extern.slf4j.Slf4j; 9 | import org.springframework.beans.BeansException; 10 | import org.springframework.boot.autoconfigure.mail.MailProperties; 11 | import org.springframework.mail.SimpleMailMessage; 12 | import org.springframework.mail.javamail.JavaMailSender; 13 | import org.springframework.util.PropertyPlaceholderHelper; 14 | 15 | import java.util.List; 16 | import java.util.Map; 17 | import java.util.Objects; 18 | import java.util.Properties; 19 | 20 | /** 21 | * @author itning 22 | * @since 2023/4/6 21:52 23 | */ 24 | @Slf4j 25 | public class SendMailPlugin implements Plugin { 26 | 27 | private static final PropertyPlaceholderHelper PROPERTY_PLACEHOLDER_HELPER = new PropertyPlaceholderHelper("${", "}"); 28 | private JavaMailSender emailSender; 29 | private MailProperties mailProperties; 30 | 31 | public SendMailPlugin() { 32 | try { 33 | this.emailSender = BootBeanGetter.getClassBean(JavaMailSender.class); 34 | this.mailProperties = BootBeanGetter.getClassBean(MailProperties.class); 35 | } catch (BeansException e) { 36 | log.info("ignore send email plugin:{}", e.getMessage()); 37 | } 38 | } 39 | 40 | @Override 41 | public void start(String name, BackupProperties.Job job, DataEntry dataEntry, List results) throws Exception { 42 | if (Objects.isNull(emailSender) || Objects.isNull(mailProperties)) { 43 | return; 44 | } 45 | long count = 0; 46 | Map> dataInfoMap = dataEntry.getDataInfoMap(); 47 | for (Map.Entry> item : dataInfoMap.entrySet()) { 48 | count += item.getValue().size(); 49 | } 50 | 51 | Properties properties = new Properties(); 52 | properties.setProperty("name", name); 53 | properties.setProperty("database", String.valueOf(dataEntry.getColumnInfoMap().size())); 54 | properties.setProperty("total", String.valueOf(count)); 55 | String text = """ 56 | 备份结果通知 ${name} 57 | 备份了 ${database} 个数据库 58 | 共计 ${total} 行数据 59 | """; 60 | SimpleMailMessage message = new SimpleMailMessage(); 61 | message.setFrom(mailProperties.getUsername()); 62 | message.setTo("itning@itning.top"); 63 | message.setSubject("备份结果通知"); 64 | message.setText(PROPERTY_PLACEHOLDER_HELPER.replacePlaceholders(text, properties)); 65 | emailSender.send(message); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/engine/JobEngine.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.engine; 2 | 3 | import com.itning.mysql_backup_to_sqlite.config.BackupProperties; 4 | import com.itning.mysql_backup_to_sqlite.entry.DataEntry; 5 | import com.itning.mysql_backup_to_sqlite.entry.TargetResult; 6 | import com.itning.mysql_backup_to_sqlite.plugin.SendMailPlugin; 7 | import com.itning.mysql_backup_to_sqlite.plugin.TencentCloudCOSPlugin; 8 | import com.itning.mysql_backup_to_sqlite.source.MySQLSource; 9 | import com.itning.mysql_backup_to_sqlite.target.SqlFileTarget; 10 | import com.itning.mysql_backup_to_sqlite.target.SqliteTarget; 11 | import lombok.extern.slf4j.Slf4j; 12 | 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | import java.util.Objects; 16 | 17 | /** 18 | * @author ning.wang 19 | * @since 2023/4/3 13:35 20 | */ 21 | @Slf4j 22 | public class JobEngine implements Runnable { 23 | 24 | private static final MySQLSource MYSQL_SOURCE = new MySQLSource(); 25 | private static final SqliteTarget SQLITE_TARGET = new SqliteTarget(); 26 | private static final SqlFileTarget SQL_FILE_TARGET = new SqlFileTarget(); 27 | private static final TencentCloudCOSPlugin TENCENT_CLOUD_COS_PLUGIN = new TencentCloudCOSPlugin(); 28 | 29 | private final String name; 30 | private final BackupProperties.Job job; 31 | private final SendMailPlugin sendMailPlugin; 32 | 33 | public JobEngine(String name, BackupProperties.Job job) { 34 | this.name = name; 35 | this.job = job; 36 | sendMailPlugin = new SendMailPlugin(); 37 | } 38 | 39 | @Override 40 | public void run() { 41 | log.info("start job {}", name); 42 | try { 43 | DataEntry dataEntry = handleSource(); 44 | List results = handleTarget(dataEntry); 45 | handlePlugin(dataEntry, results); 46 | } catch (Throwable e) { 47 | log.error("handler job {} exception", name, e); 48 | } finally { 49 | log.info("end job {}", name); 50 | } 51 | } 52 | 53 | private DataEntry handleSource() throws Exception { 54 | BackupProperties.MySQLSource mysqlSource = job.getMysqlSource(); 55 | return MYSQL_SOURCE.start(mysqlSource.getMysqlUrl(), mysqlSource.getMysqlUsername(), mysqlSource.getMysqlPassword(), mysqlSource.getMysqlTables()); 56 | } 57 | 58 | private List handleTarget(DataEntry data) throws Exception { 59 | List results = new ArrayList<>(2); 60 | BackupProperties.SqliteTarget sqliteTarget = job.getSqliteTarget(); 61 | if (Objects.nonNull(sqliteTarget)) { 62 | results.add(SQLITE_TARGET.start(sqliteTarget.getOutPutDir(), name, data)); 63 | } 64 | BackupProperties.SqlFileTarget sqlFileTarget = job.getSqlFileTarget(); 65 | if (Objects.nonNull(sqlFileTarget)) { 66 | results.add(SQL_FILE_TARGET.start(sqlFileTarget.getOutPutDir(), name, data)); 67 | } 68 | return results; 69 | } 70 | 71 | private void handlePlugin(DataEntry dataEntry, List files) throws Exception { 72 | TENCENT_CLOUD_COS_PLUGIN.start(name, job, dataEntry, files); 73 | sendMailPlugin.start(name, job, dataEntry, files); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 3.5.7 9 | 10 | 11 | com.itning 12 | mysql_backup_to_sqlite 13 | 0.0.1-SNAPSHOT 14 | mysql_backup_to_sqlite 15 | mysql_backup_to_sqlite 16 | 17 | 17 18 | true 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.projectlombok 27 | lombok 28 | true 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | 37 | mysql 38 | mysql-connector-java 39 | 8.0.32 40 | 41 | 42 | 43 | org.xerial 44 | sqlite-jdbc 45 | 46 | 47 | 48 | com.qcloud 49 | cos_api 50 | 5.6.259 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-configuration-processor 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-starter-mail 59 | 60 | 61 | 62 | 63 | 64 | 65 | org.springframework.boot 66 | spring-boot-maven-plugin 67 | 68 | 69 | 70 | org.projectlombok 71 | lombok 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/plugin/TencentCloudCOSPlugin.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.plugin; 2 | 3 | import com.itning.mysql_backup_to_sqlite.config.BackupProperties; 4 | import com.itning.mysql_backup_to_sqlite.entry.DataEntry; 5 | import com.itning.mysql_backup_to_sqlite.entry.TargetResult; 6 | import com.qcloud.cos.COSClient; 7 | import com.qcloud.cos.ClientConfig; 8 | import com.qcloud.cos.auth.BasicCOSCredentials; 9 | import com.qcloud.cos.auth.COSCredentials; 10 | import com.qcloud.cos.http.HttpProtocol; 11 | import com.qcloud.cos.model.ObjectMetadata; 12 | import com.qcloud.cos.model.PutObjectRequest; 13 | import com.qcloud.cos.model.UploadResult; 14 | import com.qcloud.cos.region.Region; 15 | import com.qcloud.cos.transfer.TransferManager; 16 | import com.qcloud.cos.transfer.Upload; 17 | import lombok.extern.slf4j.Slf4j; 18 | 19 | import java.io.File; 20 | import java.util.List; 21 | import java.util.Objects; 22 | 23 | /** 24 | * @author ning.wang 25 | * @since 2023/4/3 14:41 26 | */ 27 | @Slf4j 28 | public class TencentCloudCOSPlugin implements Plugin { 29 | 30 | @Override 31 | public void start(String name, BackupProperties.Job job, DataEntry dataEntry, List results) throws Exception { 32 | BackupProperties.TencentCloudCOSPlugin tencentCloudCOSPlugin = job.getTencentCloudCosPlugin(); 33 | if (Objects.isNull(tencentCloudCOSPlugin) || Objects.isNull(tencentCloudCOSPlugin.getBucketName()) || tencentCloudCOSPlugin.getBucketName().isBlank()) { 34 | return; 35 | } 36 | COSCredentials cred = new BasicCOSCredentials(tencentCloudCOSPlugin.getSecretId(), tencentCloudCOSPlugin.getSecretKey()); 37 | Region region = new Region(tencentCloudCOSPlugin.getRegionName()); 38 | ClientConfig clientConfig = new ClientConfig(region); 39 | clientConfig.setHttpProtocol(HttpProtocol.https); 40 | COSClient cosClient = new COSClient(cred, clientConfig); 41 | 42 | TransferManager transferManager = new TransferManager(cosClient); 43 | try { 44 | for (TargetResult result : results) { 45 | List files = result.getFiles(); 46 | TargetResult.TargetType targetType = result.getTargetType(); 47 | 48 | if (!tencentCloudCOSPlugin.isUploadSqliteFile() && targetType == TargetResult.TargetType.SQLITE) { 49 | continue; 50 | } 51 | if (!tencentCloudCOSPlugin.isUploadSqlFile() && targetType == TargetResult.TargetType.SQL_FILE) { 52 | continue; 53 | } 54 | 55 | for (File file : files) { 56 | String key = Objects.isNull(tencentCloudCOSPlugin.getDirName()) ? file.getName() : tencentCloudCOSPlugin.getDirName() + "/" + file.getName(); 57 | PutObjectRequest putObjectRequest = new PutObjectRequest(tencentCloudCOSPlugin.getBucketName(), key, file); 58 | ObjectMetadata objectMetadata = new ObjectMetadata(); 59 | putObjectRequest.setMetadata(objectMetadata); 60 | Upload upload = transferManager.upload(putObjectRequest); 61 | UploadResult uploadResult = upload.waitForUploadResult(); 62 | log.info("upload cos result: {}", uploadResult.getKey()); 63 | } 64 | } 65 | } finally { 66 | transferManager.shutdownNow(true); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/source/MySQLSource.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.source; 2 | 3 | import com.itning.mysql_backup_to_sqlite.entry.ColumnInfo; 4 | import com.itning.mysql_backup_to_sqlite.entry.DataEntry; 5 | import com.itning.mysql_backup_to_sqlite.entry.RowData; 6 | 7 | import java.sql.*; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.Map; 11 | import java.util.Set; 12 | 13 | /** 14 | * @author ning.wang 15 | * @since 2023/4/3 10:29 16 | */ 17 | public class MySQLSource implements Source { 18 | 19 | static { 20 | try { 21 | Class.forName("com.mysql.cj.jdbc.Driver"); 22 | } catch (ClassNotFoundException e) { 23 | throw new RuntimeException(e); 24 | } 25 | } 26 | 27 | @Override 28 | public DataEntry start(String mysqlUrl, String mysqlUsername, String mysqlPassword, Set tables) throws Exception { 29 | try (Connection connection = DriverManager.getConnection(mysqlUrl, mysqlUsername, mysqlPassword)) { 30 | DatabaseMetaData metaData = connection.getMetaData(); 31 | ResultSet tablesResultSet = metaData.getTables(null, null, null, new String[]{"TABLE"}); 32 | 33 | DataEntry dataEntry = new DataEntry(); 34 | Map> columnInfoMap = dataEntry.getColumnInfoMap(); 35 | Map> rowDataMap = dataEntry.getDataInfoMap(); 36 | while (tablesResultSet.next()) { 37 | String tableName = tablesResultSet.getString("TABLE_NAME"); 38 | if (!tables.contains(tableName)) { 39 | continue; 40 | } 41 | 42 | List columnInfos = new ArrayList<>(); 43 | columnInfoMap.put(tableName, columnInfos); 44 | 45 | try (Statement mysqlStatement = connection.createStatement()) { 46 | ResultSet mysqlResult = mysqlStatement.executeQuery("SELECT * FROM " + tableName); 47 | ResultSetMetaData mysqlMetaData = mysqlResult.getMetaData(); 48 | int columnCount = mysqlMetaData.getColumnCount(); 49 | for (int i = 1; i <= columnCount; i++) { 50 | ColumnInfo columnInfo = new ColumnInfo(); 51 | columnInfo.setIndex(i); 52 | columnInfo.setColumnName(mysqlMetaData.getColumnName(i)); 53 | columnInfo.setColumnType(mysqlMetaData.getColumnType(i)); 54 | columnInfo.setColumnTypeName(mysqlMetaData.getColumnTypeName(i)); 55 | columnInfos.add(columnInfo); 56 | } 57 | 58 | List rowDataList = new ArrayList<>(); 59 | rowDataMap.put(tableName, rowDataList); 60 | 61 | while (mysqlResult.next()) { 62 | RowData rowData = new RowData(); 63 | rowDataList.add(rowData); 64 | 65 | for (int i = 1; i <= columnCount; i++) { 66 | RowData.ItemData itemData = new RowData.ItemData(); 67 | itemData.setColumnIndex(i); 68 | itemData.setValue(mysqlResult.getObject(i)); 69 | rowData.getItemData().add(itemData); 70 | } 71 | } 72 | } 73 | } 74 | return dataEntry; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/target/SqlFileTarget.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.target; 2 | 3 | import com.itning.mysql_backup_to_sqlite.entry.ColumnInfo; 4 | import com.itning.mysql_backup_to_sqlite.entry.DataEntry; 5 | import com.itning.mysql_backup_to_sqlite.entry.RowData; 6 | import com.itning.mysql_backup_to_sqlite.entry.TargetResult; 7 | 8 | import java.io.File; 9 | import java.io.FileWriter; 10 | import java.io.IOException; 11 | import java.nio.charset.StandardCharsets; 12 | import java.text.MessageFormat; 13 | import java.text.SimpleDateFormat; 14 | import java.util.ArrayList; 15 | import java.util.Date; 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | /** 20 | * @author ning.wang 21 | * @since 2023/4/3 13:49 22 | */ 23 | public class SqlFileTarget implements Target { 24 | @Override 25 | public TargetResult start(List outPutDir, String jobName, DataEntry dataEntry) throws Exception { 26 | Map> columnInfoMap = dataEntry.getColumnInfoMap(); 27 | Map> dataInfoMap = dataEntry.getDataInfoMap(); 28 | 29 | List result = new ArrayList<>(columnInfoMap.size()); 30 | for (Map.Entry> tableItem : columnInfoMap.entrySet()) { 31 | 32 | String tableName = tableItem.getKey(); 33 | List columnInfos = tableItem.getValue(); 34 | 35 | StringBuilder sql = new StringBuilder(MessageFormat.format(""" 36 | -- DATABASE TABLE BACKUP 37 | -- JOB NAME {0} 38 | -- TABLE NAME {1} 39 | \n 40 | """, jobName, tableName)); 41 | 42 | StringBuilder createTableSqlBuilder = new StringBuilder("-- DDL start\nCREATE TABLE IF NOT EXISTS '" + tableName + "'("); 43 | 44 | columnInfos.forEach(columnInfo -> createTableSqlBuilder 45 | .append("'") 46 | .append(columnInfo.getColumnName()) 47 | .append("' ") 48 | .append(columnInfo.getColumnTypeName()) 49 | .append(",")); 50 | String createTableSql = createTableSqlBuilder.toString(); 51 | sql.append(createTableSql, 0, createTableSql.length() - 1).append(");\n-- DDL end\n\n-- DML start\n"); 52 | 53 | List rowData = dataInfoMap.get(tableName); 54 | for (RowData row : rowData) { 55 | StringBuilder insertSqlBuilder = new StringBuilder("INSERT INTO '" + tableName + "' VALUES ("); 56 | for (RowData.ItemData itemData : row.getItemData()) { 57 | Object value = itemData.getValue(); 58 | if (value instanceof Number) { 59 | insertSqlBuilder.append(value).append(","); 60 | continue; 61 | } 62 | 63 | boolean containsApostrophe = value.toString().contains("'"); 64 | if (containsApostrophe) { 65 | insertSqlBuilder.append("'").append(value.toString().replace("'", "''")).append("',"); 66 | } else { 67 | insertSqlBuilder.append("'").append(value).append("',"); 68 | } 69 | } 70 | String insertSql = insertSqlBuilder.toString(); 71 | sql.append(insertSql, 0, insertSql.length() - 1).append(");\n"); 72 | } 73 | 74 | String sqlContent = sql.append("-- DML end\n").toString(); 75 | for (int i = 0; i < outPutDir.size(); i++) { 76 | if (i == 0) { 77 | result.add(write2File(outPutDir.get(i), jobName, tableName, sqlContent)); 78 | } else { 79 | write2File(outPutDir.get(i), jobName, tableName, sqlContent); 80 | } 81 | } 82 | } 83 | return new TargetResult(result, TargetResult.TargetType.SQL_FILE); 84 | } 85 | 86 | private File write2File(File outPutDir, String jobName, String tableName, String sql) throws IOException { 87 | String time = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 88 | File outFile = new File(outPutDir, time + "_" + jobName + "_" + tableName + ".sql"); 89 | try (FileWriter fileWriter = new FileWriter(outFile, StandardCharsets.UTF_8, false)) { 90 | fileWriter.write(sql); 91 | fileWriter.flush(); 92 | } 93 | return outFile; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/itning/mysql_backup_to_sqlite/target/SqliteTarget.java: -------------------------------------------------------------------------------- 1 | package com.itning.mysql_backup_to_sqlite.target; 2 | 3 | import com.itning.mysql_backup_to_sqlite.entry.ColumnInfo; 4 | import com.itning.mysql_backup_to_sqlite.entry.DataEntry; 5 | import com.itning.mysql_backup_to_sqlite.entry.RowData; 6 | import com.itning.mysql_backup_to_sqlite.entry.TargetResult; 7 | 8 | import java.io.File; 9 | import java.nio.file.Files; 10 | import java.nio.file.StandardCopyOption; 11 | import java.sql.Connection; 12 | import java.sql.DriverManager; 13 | import java.sql.PreparedStatement; 14 | import java.sql.Statement; 15 | import java.text.SimpleDateFormat; 16 | import java.util.Collections; 17 | import java.util.Date; 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | import static java.sql.Types.*; 22 | 23 | /** 24 | * @author ning.wang 25 | * @since 2023/4/3 12:42 26 | */ 27 | public class SqliteTarget implements Target { 28 | static { 29 | try { 30 | Class.forName("org.sqlite.JDBC"); 31 | } catch (ClassNotFoundException e) { 32 | throw new RuntimeException(e); 33 | } 34 | } 35 | 36 | @Override 37 | public TargetResult start(List outPutDir, String jobName, DataEntry dataEntry) throws Exception { 38 | File dbFile = getDbFile(outPutDir.get(0), jobName); 39 | try (Connection connection = DriverManager.getConnection("jdbc:sqlite:" + getDbFile(outPutDir.get(0), jobName))) { 40 | Map> columnInfoMap = dataEntry.getColumnInfoMap(); 41 | Map> dataInfoMap = dataEntry.getDataInfoMap(); 42 | for (Map.Entry> tableItem : columnInfoMap.entrySet()) { 43 | String tableName = tableItem.getKey(); 44 | List columnInfos = tableItem.getValue(); 45 | 46 | StringBuilder createTableSqlBuilder = new StringBuilder("CREATE TABLE IF NOT EXISTS '" + tableName + "'("); 47 | columnInfos.forEach(columnInfo -> createTableSqlBuilder 48 | .append("'") 49 | .append(columnInfo.getColumnName()) 50 | .append("' ") 51 | .append(getSqliteColumnType(columnInfo.getColumnType())) 52 | .append(",")); 53 | 54 | String createTableSql = createTableSqlBuilder.toString(); 55 | createTableSql = createTableSql.substring(0, createTableSql.length() - 1) + ")"; 56 | try (Statement sqliteStatement = connection.createStatement()) { 57 | sqliteStatement.executeUpdate(createTableSql); 58 | } 59 | 60 | List rowData = dataInfoMap.get(tableName); 61 | 62 | StringBuilder insertSqlBuilder = new StringBuilder("INSERT INTO '" + tableName + "' VALUES ("); 63 | columnInfos.forEach(columnInfo -> insertSqlBuilder.append(" ? ,")); 64 | String insertSql = insertSqlBuilder.toString(); 65 | insertSql = insertSql.substring(0, insertSql.length() - 1) + ")"; 66 | 67 | for (RowData row : rowData) { 68 | try (PreparedStatement preparedStatement = connection.prepareStatement(insertSql)) { 69 | for (RowData.ItemData itemData : row.getItemData()) { 70 | preparedStatement.setObject(itemData.getColumnIndex(), itemData.getValue()); 71 | } 72 | preparedStatement.executeUpdate(); 73 | } 74 | } 75 | } 76 | } 77 | 78 | for (int i = 1; i < outPutDir.size(); i++) { 79 | Files.copy(dbFile.toPath(), new File(outPutDir.get(i), dbFile.getName()).toPath(), StandardCopyOption.REPLACE_EXISTING); 80 | } 81 | return new TargetResult(Collections.singletonList(dbFile), TargetResult.TargetType.SQLITE); 82 | } 83 | 84 | private File getDbFile(File outPutDir, String jobName) { 85 | String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 86 | return new File(outPutDir, name + "_" + jobName + "_backup.db"); 87 | } 88 | 89 | private String getSqliteColumnType(int columnType) { 90 | switch (columnType) { 91 | case BIT, TINYINT, SMALLINT, INTEGER, BIGINT -> { 92 | return "INTEGER"; 93 | } 94 | case FLOAT, REAL, DOUBLE, NUMERIC, DECIMAL -> { 95 | return "REAL"; 96 | } 97 | case BINARY, VARBINARY, LONGVARBINARY, BLOB -> { 98 | return "BLOB"; 99 | } 100 | default -> { 101 | return "TEXT"; 102 | } 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM https://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* 50 | if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 124 | 125 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 127 | ) 128 | 129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 131 | if exist %WRAPPER_JAR% ( 132 | if "%MVNW_VERBOSE%" == "true" ( 133 | echo Found %WRAPPER_JAR% 134 | ) 135 | ) else ( 136 | if not "%MVNW_REPOURL%" == "" ( 137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 138 | ) 139 | if "%MVNW_VERBOSE%" == "true" ( 140 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 141 | echo Downloading from: %DOWNLOAD_URL% 142 | ) 143 | 144 | powershell -Command "&{"^ 145 | "$webclient = new-object System.Net.WebClient;"^ 146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 148 | "}"^ 149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ 150 | "}" 151 | if "%MVNW_VERBOSE%" == "true" ( 152 | echo Finished downloading %WRAPPER_JAR% 153 | ) 154 | ) 155 | @REM End of extension 156 | 157 | @REM Provide a "standardized" way to retrieve the CLI args that will 158 | @REM work with both Windows and non-Windows executions. 159 | set MAVEN_CMD_LINE_ARGS=%* 160 | 161 | %MAVEN_JAVA_EXE% ^ 162 | %JVM_CONFIG_MAVEN_PROPS% ^ 163 | %MAVEN_OPTS% ^ 164 | %MAVEN_DEBUG_OPTS% ^ 165 | -classpath %WRAPPER_JAR% ^ 166 | "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ 167 | %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 168 | if ERRORLEVEL 1 goto error 169 | goto end 170 | 171 | :error 172 | set ERROR_CODE=1 173 | 174 | :end 175 | @endlocal & set ERROR_CODE=%ERROR_CODE% 176 | 177 | if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost 178 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 179 | if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" 180 | if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" 181 | :skipRcPost 182 | 183 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 184 | if "%MAVEN_BATCH_PAUSE%"=="on" pause 185 | 186 | if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% 187 | 188 | cmd /C exit /B %ERROR_CODE% 189 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # https://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /usr/local/etc/mavenrc ] ; then 40 | . /usr/local/etc/mavenrc 41 | fi 42 | 43 | if [ -f /etc/mavenrc ] ; then 44 | . /etc/mavenrc 45 | fi 46 | 47 | if [ -f "$HOME/.mavenrc" ] ; then 48 | . "$HOME/.mavenrc" 49 | fi 50 | 51 | fi 52 | 53 | # OS specific support. $var _must_ be set to either true or false. 54 | cygwin=false; 55 | darwin=false; 56 | mingw=false 57 | case "`uname`" in 58 | CYGWIN*) cygwin=true ;; 59 | MINGW*) mingw=true;; 60 | Darwin*) darwin=true 61 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 62 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 63 | if [ -z "$JAVA_HOME" ]; then 64 | if [ -x "/usr/libexec/java_home" ]; then 65 | export JAVA_HOME="`/usr/libexec/java_home`" 66 | else 67 | export JAVA_HOME="/Library/Java/Home" 68 | fi 69 | fi 70 | ;; 71 | esac 72 | 73 | if [ -z "$JAVA_HOME" ] ; then 74 | if [ -r /etc/gentoo-release ] ; then 75 | JAVA_HOME=`java-config --jre-home` 76 | fi 77 | fi 78 | 79 | if [ -z "$M2_HOME" ] ; then 80 | ## resolve links - $0 may be a link to maven's home 81 | PRG="$0" 82 | 83 | # need this for relative symlinks 84 | while [ -h "$PRG" ] ; do 85 | ls=`ls -ld "$PRG"` 86 | link=`expr "$ls" : '.*-> \(.*\)$'` 87 | if expr "$link" : '/.*' > /dev/null; then 88 | PRG="$link" 89 | else 90 | PRG="`dirname "$PRG"`/$link" 91 | fi 92 | done 93 | 94 | saveddir=`pwd` 95 | 96 | M2_HOME=`dirname "$PRG"`/.. 97 | 98 | # make it fully qualified 99 | M2_HOME=`cd "$M2_HOME" && pwd` 100 | 101 | cd "$saveddir" 102 | # echo Using m2 at $M2_HOME 103 | fi 104 | 105 | # For Cygwin, ensure paths are in UNIX format before anything is touched 106 | if $cygwin ; then 107 | [ -n "$M2_HOME" ] && 108 | M2_HOME=`cygpath --unix "$M2_HOME"` 109 | [ -n "$JAVA_HOME" ] && 110 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 111 | [ -n "$CLASSPATH" ] && 112 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 113 | fi 114 | 115 | # For Mingw, ensure paths are in UNIX format before anything is touched 116 | if $mingw ; then 117 | [ -n "$M2_HOME" ] && 118 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 119 | [ -n "$JAVA_HOME" ] && 120 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 121 | fi 122 | 123 | if [ -z "$JAVA_HOME" ]; then 124 | javaExecutable="`which javac`" 125 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 126 | # readlink(1) is not available as standard on Solaris 10. 127 | readLink=`which readlink` 128 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 129 | if $darwin ; then 130 | javaHome="`dirname \"$javaExecutable\"`" 131 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 132 | else 133 | javaExecutable="`readlink -f \"$javaExecutable\"`" 134 | fi 135 | javaHome="`dirname \"$javaExecutable\"`" 136 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 137 | JAVA_HOME="$javaHome" 138 | export JAVA_HOME 139 | fi 140 | fi 141 | fi 142 | 143 | if [ -z "$JAVACMD" ] ; then 144 | if [ -n "$JAVA_HOME" ] ; then 145 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 146 | # IBM's JDK on AIX uses strange locations for the executables 147 | JAVACMD="$JAVA_HOME/jre/sh/java" 148 | else 149 | JAVACMD="$JAVA_HOME/bin/java" 150 | fi 151 | else 152 | JAVACMD="`\\unset -f command; \\command -v java`" 153 | fi 154 | fi 155 | 156 | if [ ! -x "$JAVACMD" ] ; then 157 | echo "Error: JAVA_HOME is not defined correctly." >&2 158 | echo " We cannot execute $JAVACMD" >&2 159 | exit 1 160 | fi 161 | 162 | if [ -z "$JAVA_HOME" ] ; then 163 | echo "Warning: JAVA_HOME environment variable is not set." 164 | fi 165 | 166 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 167 | 168 | # traverses directory structure from process work directory to filesystem root 169 | # first directory with .mvn subdirectory is considered project base directory 170 | find_maven_basedir() { 171 | 172 | if [ -z "$1" ] 173 | then 174 | echo "Path not specified to find_maven_basedir" 175 | return 1 176 | fi 177 | 178 | basedir="$1" 179 | wdir="$1" 180 | while [ "$wdir" != '/' ] ; do 181 | if [ -d "$wdir"/.mvn ] ; then 182 | basedir=$wdir 183 | break 184 | fi 185 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 186 | if [ -d "${wdir}" ]; then 187 | wdir=`cd "$wdir/.."; pwd` 188 | fi 189 | # end of workaround 190 | done 191 | echo "${basedir}" 192 | } 193 | 194 | # concatenates all lines of a file 195 | concat_lines() { 196 | if [ -f "$1" ]; then 197 | echo "$(tr -s '\n' ' ' < "$1")" 198 | fi 199 | } 200 | 201 | BASE_DIR=`find_maven_basedir "$(pwd)"` 202 | if [ -z "$BASE_DIR" ]; then 203 | exit 1; 204 | fi 205 | 206 | ########################################################################################## 207 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 208 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 209 | ########################################################################################## 210 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 211 | if [ "$MVNW_VERBOSE" = true ]; then 212 | echo "Found .mvn/wrapper/maven-wrapper.jar" 213 | fi 214 | else 215 | if [ "$MVNW_VERBOSE" = true ]; then 216 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 217 | fi 218 | if [ -n "$MVNW_REPOURL" ]; then 219 | jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 220 | else 221 | jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 222 | fi 223 | while IFS="=" read key value; do 224 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 225 | esac 226 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 227 | if [ "$MVNW_VERBOSE" = true ]; then 228 | echo "Downloading from: $jarUrl" 229 | fi 230 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 231 | if $cygwin; then 232 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` 233 | fi 234 | 235 | if command -v wget > /dev/null; then 236 | if [ "$MVNW_VERBOSE" = true ]; then 237 | echo "Found wget ... using wget" 238 | fi 239 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 240 | wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 241 | else 242 | wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 243 | fi 244 | elif command -v curl > /dev/null; then 245 | if [ "$MVNW_VERBOSE" = true ]; then 246 | echo "Found curl ... using curl" 247 | fi 248 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 249 | curl -o "$wrapperJarPath" "$jarUrl" -f 250 | else 251 | curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f 252 | fi 253 | 254 | else 255 | if [ "$MVNW_VERBOSE" = true ]; then 256 | echo "Falling back to using Java to download" 257 | fi 258 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 259 | # For Cygwin, switch paths to Windows format before running javac 260 | if $cygwin; then 261 | javaClass=`cygpath --path --windows "$javaClass"` 262 | fi 263 | if [ -e "$javaClass" ]; then 264 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 265 | if [ "$MVNW_VERBOSE" = true ]; then 266 | echo " - Compiling MavenWrapperDownloader.java ..." 267 | fi 268 | # Compiling the Java class 269 | ("$JAVA_HOME/bin/javac" "$javaClass") 270 | fi 271 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 272 | # Running the downloader 273 | if [ "$MVNW_VERBOSE" = true ]; then 274 | echo " - Running MavenWrapperDownloader.java ..." 275 | fi 276 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 277 | fi 278 | fi 279 | fi 280 | fi 281 | ########################################################################################## 282 | # End of extension 283 | ########################################################################################## 284 | 285 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 286 | if [ "$MVNW_VERBOSE" = true ]; then 287 | echo $MAVEN_PROJECTBASEDIR 288 | fi 289 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 290 | 291 | # For Cygwin, switch paths to Windows format before running java 292 | if $cygwin; then 293 | [ -n "$M2_HOME" ] && 294 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 295 | [ -n "$JAVA_HOME" ] && 296 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 297 | [ -n "$CLASSPATH" ] && 298 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 299 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 300 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 301 | fi 302 | 303 | # Provide a "standardized" way to retrieve the CLI args that will 304 | # work with both Windows and non-Windows executions. 305 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 306 | export MAVEN_CMD_LINE_ARGS 307 | 308 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 309 | 310 | exec "$JAVACMD" \ 311 | $MAVEN_OPTS \ 312 | $MAVEN_DEBUG_OPTS \ 313 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 314 | "-Dmaven.home=${M2_HOME}" \ 315 | "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 316 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 317 | --------------------------------------------------------------------------------