核心代碼
主要實(shí)現(xiàn):遠(yuǎn)程、定時(shí)翅帜、多數(shù)據(jù)庫(kù)的備份
核心代碼:
private static final String formatSql = "mysqldump -h%s -P%s -u%s -p%s --database %s > %s";
@Value("${project.ip}")
private String ip;
@Value("${project.port}")
private String port;
@Value("${project.username}")
private String username;
@Value("${project.password}")
private String password;
@Value("${project.database}")
private String database;
@Value("${project.location}")
private String location;
/**
* 定時(shí)備份sql.
*/
// @Scheduled(initialDelay = 2000, fixedDelay = 50000) // 測(cè)試使用
@Scheduled(cron = "0 0 9 * * ?") // 每天早晨9點(diǎn)
public void scheduleBackupSql() {
String sql = String.format(formatSql,
ip,
port,
username,
password,
database,
location);
StringBuilder sqlStr = new StringBuilder(sql);
sqlStr.append("operation_").append(new SimpleDateFormat("yyyy-MM-dd").format(new Date())).append(".sql");
logger.info(sqlStr.toString());
try {
// 區(qū)分系統(tǒng),不同系統(tǒng)執(zhí)行命令有所不同
String os = System.getProperty("os.name"); // 系統(tǒng)名稱
if (os.toLowerCase().startsWith("win")) {
Process process = Runtime.getRuntime().exec(new String[]{"cmd", "/c", sqlStr.toString()}); // windows
process.waitFor();
} else if (os.toLowerCase().startsWith("linux")) {
Process process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", sqlStr.toString()}); // linux
process.waitFor();
}
logger.info("備份數(shù)據(jù)成功");
} catch (IOException e) {
logger.error("備份數(shù)據(jù)失敗", e);
} catch (InterruptedException e) {
logger.error("備份數(shù)據(jù)失敗", e);
}
}
源碼地址:https://github.com/lbshold/springboot/tree/master/operation-backup-sql
注意:linux 區(qū)分大小寫,mysqldump -hip -Pport 這里的P指定端口號(hào),要大寫纠永。windows下不區(qū)分大小寫,linux下區(qū)分大小寫谒拴。
SpringBoot應(yīng)用注冊(cè)為windows服務(wù)
SpringBoot應(yīng)用可以打包成可運(yùn)行的jar包運(yùn)行尝江,用java -jar myapp.jar
命令就可啟動(dòng)項(xiàng)目,但是在生產(chǎn)環(huán)境下英上,不可能啟動(dòng)項(xiàng)目就要開一個(gè)命令窗口炭序,一般的方案是將應(yīng)用設(shè)置成服務(wù)啟動(dòng),如果打包成war
包形式苍日,放在tomcat等web服務(wù)器里啟動(dòng)惭聂,則將tomcat設(shè)為服務(wù)即可,如果是jar
包易遣,則該如何設(shè)置彼妻,Spring官方文檔里推薦的是用winsw這款工具嫌佑。
winsw
winsw
是一款可將可執(zhí)行程序安裝成Windows Service
的開源小工具豆茫,
官網(wǎng)地址:https://github.com/kohsuke/winsw
使用 (四步)
- 為了方便,將打包的項(xiàng)目jar和winsw放在同一目錄屋摇,我這里放在D盤
- 將winsw-1.19-bin.exe重命名為和myapp.jar同名:myapp.exe
- 在當(dāng)前目錄下新建一個(gè)xml文件:myapp.xml,內(nèi)容如下:
<service>
<id>myapp</id>
<name>myapp</name>
<description>This service runs myapp project.</description>
<executable>java</executable>
<arguments>-jar "myapp.jar"</arguments>
<logmode>rotate</logmode>
</service>
相關(guān)參數(shù)說(shuō)明如下:
- id:id是安裝成windows服務(wù)后的服務(wù)名揩魂,id必須是唯一的。
- name:name是服務(wù)的簡(jiǎn)寫名字炮温,name也必須是唯一的火脉,這里我設(shè)為和id相同。
- description:服務(wù)的文字說(shuō)明柒啤。
- executable:執(zhí)行的命令倦挂,因?yàn)閱?dòng)springboot應(yīng)用的命令是
java -jar myapp.jar
,所以這里寫java,如果java沒(méi)有安裝到系統(tǒng)path里担巩,則這里必須全路徑方援。 - arguments:命令執(zhí)行參數(shù), 如果端口號(hào)要在這里設(shè)置涛癌,可以在后面添上:
--server.port=8080
- 當(dāng)然還有其它屬性犯戏,請(qǐng)參考官網(wǎng)上的說(shuō)明。
- 運(yùn)行以下命令拳话,將應(yīng)用注冊(cè)為服務(wù):
myapp.exe install
- 運(yùn)行上面的命令后先匪,在windows的服務(wù)里面能看到名為myapp的服務(wù),到此安裝成功弃衍。
- 除了install命令外呀非,還有其它命令:
- uninstall:刪除服務(wù)
- start:?jiǎn)?dòng)服務(wù)
- stop:停止服務(wù)
- restart:重啟服務(wù)
- status:輸出當(dāng)前服務(wù)的狀態(tài)