-
-
Notifications
You must be signed in to change notification settings - Fork 175
Backing up database file via the cli #704 #712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
xav-db
merged 11 commits into
HelixDB:dev
from
swaploard:Backing-up-database-file-via-the-CLI-#704
Dec 8, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fb65265
Added new CLI command helix backup to make a backup of your instance.
swaploard 7c99022
Added read and write permissions for backup cli
swaploard 2f00e04
gracefully stop backup when permission check fails
swaploard 47cbdcc
feedback fix
swaploard c931449
lock file existence check before using
swaploard 4e14ef7
String formatting fix for warning message
swaploard 2e63b57
Merge branch 'HelixDB:main' into Backing-up-database-file-via-the-CLI…
swaploard c6d430c
Implemented a consistent transactional snapshot.
swaploard e78e56b
clippy fix
swaploard eadb9dc
Merge branch 'dev' into Backing-up-database-file-via-the-CLI-#704
xav-db bbf7c1e
Merge branch 'dev' into Backing-up-database-file-via-the-CLI-#704
xav-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.