Skip to content

Commit cca7f8b

Browse files
author
Fastace
committed
云端S3账户配置初步集成Restic仓库初始化,已经可以正常备份,TODO:后续增加S3初始化库的判断,在初始化前先使用密码进行一次探测,之后再决定是否使用或者删除并初始化新的,S3账户保存页面的条件判断必须是全部初始化成功之后才可以报错,否则退出之后不保存,后续增加S3云端恢复页逻辑,待构思,目前Restic的HTTP协议没有与设置页面的协议联动,待修改,公网内网选项目前已经失效(主要是旧的用法的并发),预计调整为更为详细的S3类型,比如AWS、MINIO、COS、OSS等
1 parent e47ee40 commit cca7f8b

1 file changed

Lines changed: 54 additions & 7 deletions

File tree

source/core/restic/src/main/kotlin/com/xayah/core/restic/ResticRepository.kt

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,28 @@ class ResticRepository @Inject constructor(
5151
)
5252
defaultEnv.putAll(env)
5353

54-
// 构建带环境变量的命令
54+
// 详细日志记录
55+
Log.d(TAG, "=== Restic Command Debug ===")
56+
Log.d(TAG, "Command: restic ${args.joinToString(" ")}")
57+
Log.d(TAG, "Environment: ${defaultEnv.entries.joinToString(", ") { "${it.key}=${it.value}" }}")
58+
5559
val envExports = defaultEnv.map { "export ${it.key}=\"${it.value}\"" }
5660
val command = envExports.joinToString(" && ") + " && $resticPath ${args.joinToString(" ")}"
5761

58-
Log.d(TAG, "Executing Root Command: $command")
62+
Log.d(TAG, "Full command: $command")
5963

6064
val result = Shell.cmd(command).exec()
61-
logger.logCommandResult(result.code, result.out.joinToString("\n"))
65+
66+
// 详细输出日志
67+
Log.d(TAG, "Exit code: ${result.code}")
68+
if (result.out.isNotEmpty()) {
69+
Log.d(TAG, "STDOUT:\n${result.out.joinToString("\n")}")
70+
}
71+
if (result.err.isNotEmpty()) {
72+
Log.e(TAG, "STDERR:\n${result.err.joinToString("\n")}")
73+
}
74+
Log.d(TAG, "==============================")
75+
6276
result
6377
}
6478

@@ -360,22 +374,55 @@ class ResticRepository @Inject constructor(
360374
): Result<String> {
361375
val repoUrl = buildS3ResticUrl(extra, remotePath)
362376

363-
// 设置环境变量
377+
// 完整的环境变量设置
364378
val env = mutableMapOf(
365379
"AWS_ACCESS_KEY_ID" to extra.accessKeyId,
366380
"AWS_SECRET_ACCESS_KEY" to extra.secretAccessKey,
367381
"RESTIC_PASSWORD" to password
368382
)
369383

370-
// 对于非 AWS 的 S3 兼容存储,可能需要额外设置
384+
// 根据文档添加必要的配置
385+
val options = mutableListOf<String>()
386+
371387
if (extra.endpoint.isNotEmpty()) {
372-
// 设置自定义 region(如果 endpoint 不是 AWS)
388+
// 自定义endpoint的S3兼容存储
373389
if (extra.region.isNotEmpty()) {
374390
env["AWS_DEFAULT_REGION"] = extra.region
391+
options.add("-o")
392+
options.add("s3.region=${extra.region}")
375393
}
394+
395+
// 对于非AWS存储,可能需要指定bucket-lookup模式
396+
if (!extra.endpoint.contains("amazonaws.com")) {
397+
options.add("-o")
398+
options.add("s3.bucket-lookup=dns")
399+
}
400+
} else {
401+
// AWS S3特定配置
402+
env["AWS_DEFAULT_REGION"] = extra.region
376403
}
377404

378-
return initRepository(repoUrl, password)
405+
// 构建完整的命令参数
406+
val args = mutableListOf("init", "--repo", "\"$repoUrl\"")
407+
args.addAll(options)
408+
409+
// 直接调用executeRestic而不是initRepository
410+
val result = executeRestic(
411+
*args.toTypedArray(),
412+
env = env
413+
)
414+
415+
val output = if (result.isSuccess) {
416+
result.out.joinToString("\n")
417+
} else {
418+
result.err.joinToString("\n")
419+
}
420+
421+
return if (result.isSuccess) {
422+
Result.success(output)
423+
} else {
424+
Result.failure(Exception(output.ifEmpty { "Unknown error during restic init" }))
425+
}
379426
}
380427

381428
/**

0 commit comments

Comments
 (0)