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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions helix-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dotenvy = "0.15.7"
tokio-tungstenite = "0.27.0"
futures-util = "0.3.31"
regex = "1.11.2"
heed3 = "0.22.0"
open = "5.3"

[dev-dependencies]
Expand Down
110 changes: 110 additions & 0 deletions helix-cli/src/commands/backup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use crate::project::ProjectContext;
use crate::utils::{print_confirm, print_status, print_success, print_warning};
use eyre::Result;
use heed3::{CompactionOption, EnvFlags, EnvOpenOptions};
use std::fs;
use std::fs::create_dir_all;
use std::path::Path;
use std::path::PathBuf;

pub async fn run(output: Option<PathBuf>, instance_name: String) -> Result<()> {
// Load project context
let project = ProjectContext::find_and_load(None)?;

// Get instance config
let _instance_config = project.config.get_instance(&instance_name)?;

print_status("BACKUP", &format!("Backing up instance '{instance_name}'"));

// Get the instance volume
let volumes_dir = project
.root
.join(".helix")
.join(".volumes")
.join(&instance_name)
.join("user");

let data_file = volumes_dir.join("data.mdb");
let lock_file = volumes_dir.join("lock.mdb");

let env_path = Path::new(&volumes_dir);

// Validate existence of environment
if !env_path.exists() {
return Err(eyre::eyre!(
"Instance LMDB environment not found at {:?}",
env_path
));
}

// Check existence of data_file before calling metadata()
if !data_file.exists() {
return Err(eyre::eyre!(
"instance data file not found at {:?}",
data_file
));
}

// Check existence of lock_file before calling metadata()
if !lock_file.exists() {
return Err(eyre::eyre!(
"instance lock file not found at {:?}",
lock_file
));
}

// Get path to backup instance
let backup_dir = match output {
Some(path) => path,
None => {
let ts = chrono::Local::now()
.format("backup-%Y%m%d-%H%M%S")
.to_string();
project.root.join("backups").join(ts)
}
};

create_dir_all(&backup_dir)?;

// Get the size of the data
let total_size = fs::metadata(&data_file)?.len() + std::fs::metadata(&lock_file)?.len();

const TEN_GB: u64 = 10 * 1024 * 1024 * 1024;

// Check and warn if file is greater than 10 GB

if total_size > TEN_GB {
let size_gb = (total_size as f64) / (1024.0 * 1024.0 * 1024.0);
print_warning(&format!(
"Backup size is {:.2} GB. Taking atomic snapshot… this may take time depending on DB size",
size_gb
));
let confirmed = print_confirm("Do you want to continue?");
if !confirmed? {
print_status("CANCEL", "Backup aborted by user");
return Ok(());
}
}

// Open LMDB read-only snapshot environment
let env = unsafe {
EnvOpenOptions::new()
.flags(EnvFlags::READ_ONLY)
.max_dbs(200)
.max_readers(200)
.open(env_path)?
};

println!("Copying {:?} → {:?}", &data_file, &backup_dir);

// backup database to given database
env.copy_to_path(backup_dir.join("data.mdb"), CompactionOption::Disabled)?;
env.copy_to_path(backup_dir.join("lock.mdb"), CompactionOption::Disabled)?;

print_success(&format!(
"Backup for '{instance_name}' created at {:?}",
backup_dir
));

Ok(())
}
1 change: 1 addition & 0 deletions helix-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod add;
pub mod auth;
pub mod backup;
pub mod build;
pub mod check;
pub mod compile;
Expand Down
17 changes: 16 additions & 1 deletion helix-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use clap::{Parser, Subcommand};
use eyre::Result;
use std::path::PathBuf;
use helix_cli::{AuthAction, CloudDeploymentTypeCommand, DashboardAction, MetricsAction};


mod cleanup;
mod commands;
mod config;
Expand Down Expand Up @@ -163,6 +165,16 @@ enum Commands {
#[clap(long)]
no_backup: bool,
},

/// Backup instance at the given path
Backup {
/// Instance name to backup
instance: String,

/// Output directory for the backup. If omitted, ./backups/backup-<ts>/ will be used
#[arg(short, long)]
output: Option<PathBuf>,
},
}

#[tokio::main]
Expand Down Expand Up @@ -212,7 +224,10 @@ async fn main() -> Result<()> {
port,
dry_run,
no_backup,
} => commands::migrate::run(path, queries_dir, instance_name, port, dry_run, no_backup).await,
} => {
commands::migrate::run(path, queries_dir, instance_name, port, dry_run, no_backup).await
}
Commands::Backup { instance, output } => commands::backup::run(output, instance).await,
};

// Shutdown metrics sender
Expand Down
Loading