@@ -31,15 +31,16 @@ class Application {
3131 private fun ensureNotLocked (repo : File ) = File (repo, " locks" ).listFiles().isNullOrEmpty()
3232
3333 suspend fun backup () {
34- val duration = measureTime {
35- checkBinaries()
36- forgetHosts()
37- downloadAll()
38- backupAll()
39- }
34+ val duration =
35+ measureTime {
36+ checkBinaries()
37+ forgetHosts()
38+ downloadAll()
39+ backupAll()
40+ }
4041 WebhookUtil .executeWebhook(
4142 BackupConfig .config.webhookUrl,
42- " :white_check_mark: バックアップが完了しました(${duration.toLocaleString(DurationLocale .JAPANESE )} )"
43+ " :white_check_mark: バックアップが完了しました(${duration.toLocaleString(DurationLocale .JAPANESE )} )" ,
4344 )
4445 }
4546
@@ -85,48 +86,53 @@ class Application {
8586 command.add(info.from)
8687 command.add(info.to)
8788 var exitCode: Int
88- val duration = measureTime {
89- exitCode = ProcessExecutor .executeCommandStreamedOutput(File (" ." ), * command.toTypedArray())
90- logger.info(" rsync process exited with code $exitCode " )
91- }
89+ val duration =
90+ measureTime {
91+ exitCode = ProcessExecutor .executeCommandStreamedOutput(File (" ." ), * command.toTypedArray())
92+ logger.info(" rsync process exited with code $exitCode " )
93+ }
9294 if (exitCode == 0 || exitCode == 23 || exitCode == 24 ) {
9395 successfulDownloads.add(info.from)
9496 successfulDownloads.add(info.to)
9597 WebhookUtil .executeWebhook(
9698 BackupConfig .config.webhookUrl,
97- " :inbox_tray: `${info.webhookName} `のダウンロードが完了(${duration.toLocaleString(DurationLocale .JAPANESE )} )"
99+ " :inbox_tray: `${info.webhookName} `のダウンロードが完了(${duration.toLocaleString(DurationLocale .JAPANESE )} )" ,
98100 )
99101 } else {
100102 WebhookUtil .executeWebhook(
101103 BackupConfig .config.webhookUrl,
102- " ${BackupConfig .config.prefixIfWarning} `${info.webhookName} `のダウンロードに失敗しました(${duration.toLocaleString(DurationLocale .JAPANESE )} ) [$exitCode ]"
104+ " ${BackupConfig .config.prefixIfWarning} `${info.webhookName} `のダウンロードに失敗しました(${duration.toLocaleString(
105+ DurationLocale .JAPANESE ,
106+ )} ) [$exitCode ]" ,
103107 )
104108 }
105109 }
106110
107111 private suspend fun backupAll () {
108- val duration = measureTime {
109- BackupConfig .config.backups.forEach { info ->
110- backup(info)
112+ val duration =
113+ measureTime {
114+ BackupConfig .config.backups.forEach { info ->
115+ backup(info)
116+ }
111117 }
112- }
113118 logger.info(" Backup completed in ${duration.toLocaleString(DurationLocale .ENGLISH )} " )
114119 }
115120
116121 private suspend fun backup (info : BackupInfo ) {
117122 if (! ensureNotLocked(File (info.repo))) {
118123 WebhookUtil .executeWebhook(
119124 BackupConfig .config.webhookUrl,
120- " :warning: `${info.webhookName} `のリポジトリは他のプロセスによってロックされているためバックアップは作成されません。"
125+ " :warning: `${info.webhookName} `のリポジトリは他のプロセスによってロックされているためバックアップは作成されません。" ,
121126 )
122127 return
123128 }
124129 val effectiveDepends = info.depend.filter { ! it.startsWith(" #" ) }
125- if ((info.dependOp == DependOp .OR && ! effectiveDepends.any { successfulDownloads.contains(it) })
126- || (info.dependOp == DependOp .AND && ! effectiveDepends.all { successfulDownloads.contains(it) })) {
130+ if ((info.dependOp == DependOp .OR && ! effectiveDepends.any { successfulDownloads.contains(it) }) ||
131+ (info.dependOp == DependOp .AND && ! effectiveDepends.all { successfulDownloads.contains(it) })
132+ ) {
127133 WebhookUtil .executeWebhook(
128134 BackupConfig .config.webhookUrl,
129- " :warning: `${info.webhookName} `はデータのダウンロードに失敗しているためバックアップは作成されません。"
135+ " :warning: `${info.webhookName} `はデータのダウンロードに失敗しているためバックアップは作成されません。" ,
130136 )
131137 return
132138 }
@@ -145,56 +151,62 @@ class Application {
145151 command.add(" backup" )
146152 val file = File (info.path)
147153 val duration: Duration
148- val success = if (info.backupChildDirectoriesOnly) {
149- if (! file.exists()) {
150- logger.warn(" ${info.path} does not exist" )
151- WebhookUtil .executeWebhook(
152- BackupConfig .config.webhookUrl,
153- " ${BackupConfig .config.prefixIfWarning} `${info.webhookName} `のバックアップに失敗しました(`${info.path} `が見つかりません)"
154- )
155- return
156- }
157- if (! file.isDirectory) {
158- logger.warn(" ${info.path} is not a directory" )
159- WebhookUtil .executeWebhook(
160- BackupConfig .config.webhookUrl,
161- " ${BackupConfig .config.prefixIfWarning} `${info.webhookName} `のバックアップに失敗しました(`${info.path} `はディレクトリではありません)"
162- )
163- return
164- }
165- val result: Boolean
166- duration = measureTime {
167- result = file.listFiles(FileFilter { it.isDirectory })!! .all { child ->
168- try {
169- command.add(child.absolutePath)
170- val exitCode =
171- ProcessExecutor .executeCommandStreamedOutput(File (" ." ), * command.toTypedArray())
172- logger.info(" restic process exited with code $exitCode " )
173- return @all exitCode == 0
174- } finally {
175- command.removeLast()
176- }
154+ val success =
155+ if (info.backupChildDirectoriesOnly) {
156+ if (! file.exists()) {
157+ logger.warn(" ${info.path} does not exist" )
158+ WebhookUtil .executeWebhook(
159+ BackupConfig .config.webhookUrl,
160+ " ${BackupConfig .config.prefixIfWarning} `${info.webhookName} `のバックアップに失敗しました(`${info.path} `が見つかりません)" ,
161+ )
162+ return
177163 }
164+ if (! file.isDirectory) {
165+ logger.warn(" ${info.path} is not a directory" )
166+ WebhookUtil .executeWebhook(
167+ BackupConfig .config.webhookUrl,
168+ " ${BackupConfig .config.prefixIfWarning} `${info.webhookName} `のバックアップに失敗しました(`${info.path} `はディレクトリではありません)" ,
169+ )
170+ return
171+ }
172+ val result: Boolean
173+ duration =
174+ measureTime {
175+ result =
176+ file.listFiles(FileFilter { it.isDirectory })!! .all { child ->
177+ try {
178+ command.add(child.absolutePath)
179+ val exitCode =
180+ ProcessExecutor .executeCommandStreamedOutput(File (" ." ), * command.toTypedArray())
181+ logger.info(" restic process exited with code $exitCode " )
182+ return @all exitCode == 0
183+ } finally {
184+ command.removeLast()
185+ }
186+ }
187+ }
188+ result
189+ } else {
190+ command.add(file.absolutePath)
191+ val exitCode: Int
192+ duration =
193+ measureTime {
194+ exitCode = ProcessExecutor .executeCommandStreamedOutput(File (" ." ), * command.toTypedArray())
195+ }
196+ logger.info(" restic process exited with code $exitCode " )
197+ exitCode == 0
178198 }
179- result
180- } else {
181- command.add(file.absolutePath)
182- val exitCode: Int
183- duration = measureTime {
184- exitCode = ProcessExecutor .executeCommandStreamedOutput(File (" ." ), * command.toTypedArray())
185- }
186- logger.info(" restic process exited with code $exitCode " )
187- exitCode == 0
188- }
189199 if (success) {
190200 WebhookUtil .executeWebhook(
191201 BackupConfig .config.webhookUrl,
192- " :pencil: `${info.webhookName} `のバックアップが完了(${duration.toLocaleString(DurationLocale .JAPANESE )} )"
202+ " :pencil: `${info.webhookName} `のバックアップが完了(${duration.toLocaleString(DurationLocale .JAPANESE )} )" ,
193203 )
194204 } else {
195205 WebhookUtil .executeWebhook(
196206 BackupConfig .config.webhookUrl,
197- " ${BackupConfig .config.prefixIfWarning} `${info.webhookName} `のバックアップに失敗しました(${duration.toLocaleString(DurationLocale .JAPANESE )} )"
207+ " ${BackupConfig .config.prefixIfWarning} `${info.webhookName} `のバックアップに失敗しました(${duration.toLocaleString(
208+ DurationLocale .JAPANESE ,
209+ )} )" ,
198210 )
199211 }
200212 }
@@ -204,7 +216,10 @@ class Application {
204216 BackupConfig .config.forgetHosts.forEach { forgetHost(it, BackupConfig .config.knownHostsFile) }
205217 }
206218
207- private fun forgetHost (host : String , file : String? ) {
219+ private fun forgetHost (
220+ host : String ,
221+ file : String? ,
222+ ) {
208223 val command = mutableListOf<String >()
209224 if (BackupConfig .config.runAsSudo) {
210225 command.add(" sudo" )
0 commit comments