1
- use anyhow:: { anyhow, Result } ;
2
- use humansize:: { FormatSize , DECIMAL } ;
3
- use opendal:: Operator ;
4
1
use s_backup:: {
5
2
calc_hash_mmap_rayon,
6
3
config:: { read_config, Backup } ,
7
4
init_tracing,
8
5
s3:: init_s3,
9
- CpsdFileName ,
6
+ Args , Commands , CpsdFileName ,
10
7
} ;
11
8
9
+ use anyhow:: { anyhow, Result } ;
10
+ use clap:: Parser ;
11
+ use humansize:: { FormatSize , DECIMAL } ;
12
+ use opendal:: Operator ;
12
13
use std:: { os:: linux:: fs:: MetadataExt , path:: Path , process:: Stdio } ;
13
14
use tempfile:: TempDir ;
14
15
use tokio:: {
@@ -22,28 +23,11 @@ use tokio::{
22
23
async fn main ( ) -> Result < ( ) > {
23
24
init_tracing ( ) ;
24
25
25
- let default_config_path = if cfg ! ( debug_assertions) {
26
- "./tests/config.toml"
27
- } else {
28
- "./config.toml"
29
- } ;
26
+ let args = Args :: parse ( ) ;
30
27
31
- let config =
32
- read_config ( std:: env:: var ( "SB_CONFIG_PATH" ) . unwrap_or ( default_config_path. to_string ( ) ) )
33
- . await
34
- . map_err ( |e| {
35
- tracing:: error!( "read config failed: {}" , e) ;
36
- e
37
- } ) ?;
38
-
39
- let s3_op = init_s3 ( & config. s3 ) . await ?;
40
-
41
- for b in & config. backup {
42
- let b_clone = b. clone ( ) ;
43
- let s3_op_clone = s3_op. clone ( ) ;
44
- let _handle = tokio:: spawn ( async move {
45
- period_backup ( & b_clone, & s3_op_clone) . await . unwrap ( ) ;
46
- } ) ;
28
+ match & args. command {
29
+ Commands :: Run { config } => run ( config) . await ?,
30
+ Commands :: Test { config } => test ( config) . await ?,
47
31
}
48
32
49
33
#[ cfg( unix) ]
@@ -73,13 +57,43 @@ async fn main() -> Result<()> {
73
57
Ok ( ( ) )
74
58
}
75
59
60
+ async fn run ( config_path : & str ) -> Result < ( ) > {
61
+ let config = read_config ( config_path) . await . map_err ( |e| {
62
+ tracing:: error!( "read config failed at `{}`: {}" , config_path, e) ;
63
+ e
64
+ } ) ?;
65
+
66
+ let s3_op = init_s3 ( & config. s3 ) . await ?;
67
+
68
+ for b in & config. backup {
69
+ let b_clone = b. clone ( ) ;
70
+ let s3_op_clone = s3_op. clone ( ) ;
71
+ let _handle = tokio:: spawn ( async move {
72
+ period_backup ( & b_clone, & s3_op_clone) . await . unwrap ( ) ;
73
+ } ) ;
74
+ }
75
+
76
+ Ok ( ( ) )
77
+ }
78
+
79
+ async fn test ( config_path : & str ) -> Result < ( ) > {
80
+ tracing:: warn!( "test unimplemented" ) ;
81
+ Ok ( ( ) )
82
+ }
83
+
76
84
async fn period_backup ( b : & Backup , s3_oprator : & Operator ) -> Result < ( ) > {
77
85
loop {
78
86
let _ = backup ( b, s3_oprator) . await . map_err ( |e| {
79
87
tracing:: error!( "backup failed: {}" , e) ;
80
88
e
81
89
} ) ;
82
90
91
+ let next_run_date = chrono:: Utc :: now ( ) + chrono:: Duration :: seconds ( b. interval as i64 ) ;
92
+ tracing:: info!(
93
+ "The next backup will be run at {}" ,
94
+ next_run_date. to_rfc2822( )
95
+ ) ;
96
+
83
97
tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( b. interval as u64 ) ) . await
84
98
}
85
99
}
@@ -115,8 +129,7 @@ async fn backup(b: &Backup, s3_oprator: &Operator) -> Result<()> {
115
129
. to_string ( ) ,
116
130
) ;
117
131
118
- cmd. args ( args)
119
- . kill_on_drop ( true ) ;
132
+ cmd. args ( args) . kill_on_drop ( true ) ;
120
133
121
134
cmd. stdin ( Stdio :: piped ( ) ) ;
122
135
cmd. stdout ( Stdio :: null ( ) ) ;
@@ -129,7 +142,6 @@ async fn backup(b: &Backup, s3_oprator: &Operator) -> Result<()> {
129
142
tracing:: warn!( "{}" , line) ;
130
143
}
131
144
if !child. wait ( ) . await ?. success ( ) {
132
- tracing:: error!( "failed to compress" ) ;
133
145
return Err ( anyhow ! ( "failed to compress" ) ) ;
134
146
} ;
135
147
@@ -173,6 +185,7 @@ async fn backup(b: &Backup, s3_oprator: &Operator) -> Result<()> {
173
185
. unwrap ( ) ;
174
186
175
187
// start upload
188
+ tracing:: info!( "start uploading..." , ) ;
176
189
match tokio:: io:: copy ( & mut reader, & mut writer) . await {
177
190
Ok ( _) => writer. close ( ) . await ?,
178
191
Err ( _e) => {
0 commit comments