Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ flate2 = "1"
log = "0.4"
tar = "0.4"
temp-dir = "0.1"
tokio = { version = "1", features = ["rt", "macros"] }
tokio = { version = "1", features = ["rt", "macros", "signal"] }
uuid = { version = "1", features = ["v4", "fast-rng"] }

[profile.release]
Expand Down
25 changes: 22 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ use tokio::{task, time};
use backup::backup;
use config::parse_config;

use crate::prelude::*;

mod aws;
mod backup;
mod config;
mod prelude;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
dotenv().ok();
dotenv().ok(); // load .env file if present
// set default logging level. we ignore info logs from aws
if env::var("RUST_LOG").is_err() {
env::set_var(
"RUST_LOG",
Expand All @@ -29,32 +32,48 @@ async fn main() -> Result<()> {
};
env_logger::init();

// parse config from env and/or cli
let params = Arc::new(parse_config().await?);

// check if we run the backup once, or periodically forever
match params.interval {
Some(interval) => {
info!(
"Will backup \"{}\" every {interval} seconds",
params.folder.to_string_lossy()
);
// spawn a routine that will run the backup periodically
let task = task::spawn(async move {
let shared_params = Arc::clone(&params);
let mut interval = time::interval(Duration::from_secs(interval));
loop {
interval.tick().await;
interval.tick().await; // the first tick completes immediately, triggering the backup
match backup(&shared_params).await {
Ok(_) => {
info!("Backup succeeded");
}
Err(e) => {
// we handle errors here to keep the loop running
error!("Backup error: {e:#}");
}
}
}
});
task.await?;
let ctrl_c = tokio::spawn(async move {
tokio::signal::ctrl_c().await.or_panic();
});
// loop forever, unless ctrl-c is called
tokio::select! {
_ = ctrl_c => {
info!("Ctrl-C received, exiting");
}
_ = task => {
info!("Backup task exited, exiting");
}
}
}
None => {
// run backup only once, immediately
info!("Backuping \"{}\" once", params.folder.to_string_lossy());
backup(&params)
.await
Expand Down