Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
git_borg_linker
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LBMC
Hub
git_borg_linker
Commits
e1744280
Commit
e1744280
authored
May 7, 2024
by
nfontrod
Browse files
Options
Downloads
Patches
Plain Diff
src/pull.rs: update pull as a new methode to import tar
parent
cacdc607
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/pull.rs
+258
-241
258 additions, 241 deletions
src/pull.rs
with
258 additions
and
241 deletions
src/pull.rs
+
258
−
241
View file @
e1744280
...
@@ -2,254 +2,271 @@
...
@@ -2,254 +2,271 @@
//
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-License-Identifier: AGPL-3.0-or-later
// use crate::commit;
use
crate
::
commit
;
// use crate::mount::file_diff;
use
crate
::
mount
;
// use crate::push;
use
crate
::
push
;
// use colored::Colorize;
use
colored
::
Colorize
;
// use std::path::PathBuf;
use
std
::
fs
;
// use std::process::{exit, Command, Stdio};
use
std
::
path
::
PathBuf
;
use
std
::
process
::
exit
;
use
std
::
process
::
Command
;
use
std
::
process
::
Stdio
;
// /// Function that gets the backup borg folder path used to restore or .borg
/// Function used to import a tar archive in the remote folder of the current
// /// folder in case that the pull command fails
/// filsesystem
// ///
///
// /// # Arguments
/// # Arguments
// /// - ` borg_folder` : Path to .brg folder of the current project
/// - `borg_folder`: Path to borg folder
// /// # Return
/// - `remote_file`: Path where the archive file will be stored
// /// backup borg folder path used to restore or .borg
/// - `archive`: The archive name
// /// folder in case that the pull command fails. It will be located in
fn
import_tar_locally
(
borg_folder
:
&
PathBuf
,
remote_file
:
&
PathBuf
,
archive
:
&
str
)
{
// /// .tmp/<PROJECT_DIR>_bkp folder. Where <PROJECT_DIR> is the folder name
let
borg_archive
=
format!
(
// /// containging our project
"{}::{}"
,
// pub(crate) fn get_savefolder(borg_folder: &PathBuf) -> PathBuf {
borg_folder
.canonicalize
()
.unwrap
()
.to_str
()
.unwrap
(),
// let mut save_dir = file_diff::get_tmp_folder(borg_folder);
archive
// let folder_name = borg_folder
);
// .parent()
let
args
=
vec!
[
"import-tar"
,
&
borg_archive
,
remote_file
.to_str
()
.unwrap
()];
// .unwrap()
let
mut
output
:
std
::
process
::
Child
=
Command
::
new
(
"borg"
)
// .file_name()
.args
(
&
args
)
// .unwrap()
.stdout
(
Stdio
::
inherit
())
// .to_str()
.stdin
(
Stdio
::
inherit
())
// .unwrap();
.spawn
()
// save_dir.push(format!("{}_bkp", folder_name));
.unwrap
();
// save_dir
let
ecode
=
match
output
.wait
()
{
// }
Err
(
e
)
=>
{
eprintln!
(
"{}: borg import-tar returned an error: {} !"
,
"error"
.red
(),
e
);
exit
(
1
);
}
Ok
(
r
)
=>
r
,
};
match
ecode
.code
()
.unwrap
()
{
0
=>
(),
num
=>
{
exit
(
num
);
}
};
}
// /// Function that removes the backup folder (`save_dir`) in the tmp directory if it exists
/// Function used to import a tar archive in the remote folder in a remote
// /// # Argument
/// file-system
// /// - `save_dir`: The backup folder inside the .tmp directory
///
// fn check_and_remove_dir(save_dir: &PathBuf) {
/// # Arguments
// if !save_dir.is_dir() {
/// - `borg_folder`: Path to borg folder
// return ();
/// - `remote_file`: Path where the archive file will be stored
// }
/// - `archive`: The archive name
// let anwser = push::user_question(&format!(
/// - `adress`: The server location
// "The backup folder {} already exists, Erase it with you're current .borg folder for backup",
fn
import_tar_remotely
(
borg_folder
:
&
PathBuf
,
remote_file
:
&
PathBuf
,
archive
:
&
str
,
adress
:
&
str
)
{
// save_dir.display()
let
tmp_folder
=
mount
::
file_diff
::
get_tmp_folder
(
borg_folder
);
// ));
let
tmp_file
=
tmp_folder
.join
(
remote_file
.file_name
()
.unwrap
());
// if !anwser {
let
remote_args
=
format!
(
"{}:{}"
,
adress
,
remote_file
.to_str
()
.unwrap
());
// println!("{}: Exiting !", "info".blue());
let
args
=
vec!
[
&
remote_args
,
tmp_file
.to_str
()
.unwrap
()];
// exit(0);
let
mut
output
=
Command
::
new
(
"rsync"
)
// }
.args
(
&
args
)
// match std::fs::remove_dir_all(save_dir) {
.stdout
(
Stdio
::
inherit
())
// Err(e) => {
.stdin
(
Stdio
::
inherit
())
// eprintln!(
.spawn
()
// "{}: Unable to remove {}. {}",
.unwrap
();
// "error".red(),
let
ecode
=
match
output
.wait
()
{
// save_dir.display(),
Err
(
e
)
=>
{
// e
eprintln!
(
"{}: rsync returned an error: {} !"
,
"error"
.red
(),
e
);
// );
exit
(
1
);
// exit(1);
}
// }
Ok
(
r
)
=>
r
,
// Ok(_) => (),
};
// };
match
ecode
.code
()
.unwrap
()
{
// }
0
=>
(),
num
=>
{
exit
(
num
);
}
};
import_tar_locally
(
borg_folder
,
&
tmp_file
,
archive
);
fs
::
remove_file
(
tmp_file
)
.unwrap
();
}
// /// Function that will remove almost all the content of the .borg folder
/// Function used to export a tar archive in the remote folder
// /// it only keeps .gblkconfig if it exists
/// # Arguments
// /// # Arguments
/// - `borg_folder`: Path to borg folder
// /// - `borg_folder` : path to the .borg folder
/// - `remote_file`: Path where the archive file will be stored
// fn save_and_clean_borg_folder(borg_folder: &PathBuf) {
/// - `adress`: to location of the `url` path, 'file' for a local file
// let save_dir = get_savefolder(borg_folder);
/// - `archive`: The archive name
// println!(
fn
import_tar
(
borg_folder
:
&
PathBuf
,
remote_file
:
&
PathBuf
,
adress
:
&
str
,
archive
:
&
str
)
->
()
{
// "{}: Saving current .borg repositoy into {} for backup.\n - If everything works, you can remove it with the command {}.\n - If nothing works anymore, you can restore it with {}",
match
adress
{
// "info".blue(),
"file"
=>
import_tar_locally
(
borg_folder
,
remote_file
,
archive
),
// &save_dir.display(),
_
=>
import_tar_remotely
(
borg_folder
,
remote_file
,
archive
,
adress
),
// "gblk clean".green(),
}
// "gblk restore".green()
}
// );
// println!("{}", &save_dir.display());
// // check if the folder exits, removes it with the user permission
// check_and_remove_dir(&save_dir);
// match std::fs::create_dir(&save_dir) {
// Err(e) => {
// eprintln!(
// "{}: Unable to create {}. {}",
// "error".red(),
// &save_dir.display(),
// e
// );
// exit(1);
// }
// Ok(_) => (),
// }
// let paths = std::fs::read_dir(borg_folder).unwrap();
// for p in paths {
// let pa = p.unwrap().path();
// let name = pa.file_name().unwrap().to_str().unwrap();
// let mut destination = save_dir.to_owned();
// destination.push(name);
// match name {
// ".gblkconfig" | "config" => {
// std::fs::copy(&pa, &destination).expect(&format!(
// "{}: Unable to copy {} to {}. Your .borg folder may be corrupted, you can restor it wiht the command {}",
// "error".red(),
// &pa.display(),
// &destination.display(),
// "gblk restore".green()
// ));
// }
// _ => {
// std::fs::rename(&pa, &destination).expect(&format!(
// "{}: Unable to move {} to {} Your .borg folder may be corrupted, you can restor it wiht the command {}",
// "error".red(),
// &pa.display(),
// &destination.display(),
// "gblk restore".green()
// ));
// }
// }
// }
// }
// /// Function that check if `path_str` is an existing directory. If it exists,
/// Function used to get all archives id present in .borg folder
// /// return it else raise an error
/// List the content of the borg archive
// /// # Arguments
pub
fn
borg_list
()
->
Vec
<
String
>
{
// /// - `path_str`: A path in string form
let
mut
args
=
Vec
::
new
();
// /// # Return
args
.push
(
String
::
from
(
"list"
));
// /// The pathbuf object of `path_str`
args
.push
(
String
::
from
(
"--short"
));
// fn check_ok(path_str: &str) -> PathBuf {
let
(
borg_path
,
_
)
=
commit
::
check_path
();
// let path = PathBuf::from(path_str);
args
.push
(
String
::
from
(
borg_path
.to_str
()
.unwrap
()));
// if !path.is_dir() {
let
output
=
Command
::
new
(
"borg"
)
.args
(
&
args
)
.output
();
// eprintln!(
let
res
=
match
output
{
// "{}: {} is not a directory !",
Err
(
e
)
=>
{
// "error:".red(),
eprintln!
(
// path.display()
"{}: borg list terminated with an error {}"
,
// );
"error"
.red
(),
// exit(1)
e
// } else {
);
// return path;
exit
(
1
);
// }
}
// }
Ok
(
r
)
=>
r
,
};
match
res
.status
.success
()
{
true
=>
(),
false
=>
{
eprintln!
(
"{}: borg list returned an error"
,
"error"
.red
());
exit
(
1
);
}
};
let
output
=
String
::
from_utf8_lossy
(
&
res
.stdout
)
.to_string
();
let
output
=
output
.split_ascii_whitespace
()
.collect
::
<
Vec
<&
str
>>
();
output
.into_iter
()
.map
(|
x
|
x
.to_string
())
.collect
::
<
Vec
<
String
>>
()
}
//
///
Get the borg base direc
tory
///
Function that will pull one specific archive in the .borg reposi
tory
//
///
///
//
/// #
Description
/// #
Arguments:
//
///
Get the directory defined with BORG_BASE_DIR env variable or the directory
///
- `remote`: The remote folder to use
//
///
defined in HOME variable
///
- `archive`: The archive to pull
// ///
fn
pull_one_archive
(
remote
:
&
str
,
archive
:
&
str
)
{
// /// # Return
let
(
adress
,
url
)
=
push
::
get_adress_and_url
(
remote
);
// /// The borg base directory
let
(
borg_folder
,
_
)
=
commit
::
check_path
();
// fn get_base_dir() -> PathBuf {
let
borg_folder
=
borg_folder
.canonicalize
()
.unwrap
();
//
let
base_dir = std::env::var("BORG_BASE_DIR
");
let
tmp_remote
=
push
::
get_remote_file
(
&
url
,
archive
,
"none
"
);
// match base_dir {
// check if the archive if present locally
//
Ok(dir) => check_ok(&dir),
let
borg_archives
=
borg_list
();
//
Err(_) =>
{
if
borg_archives
.contains
(
&
archive
.to_string
())
{
//
let base_dir = std::env::var("HOME");
eprintln!
(
//
match base_dir {
"{}: The archive {} is already defined in your local .borg folder. Skipping"
,
//
Ok(dir) => check_ok(&dir
),
"warning"
.yellow
(
),
//
Err(_) => {
archive
.yellow
()
//
eprintln!("{}: HOME environment variable not set !", "error".red()
);
);
//
exit(1
);
return
(
);
//
}
}
// }
// check if the remote archive file exists
//
}
let
remote_file
=
push
::
handle_existing_remote_file
(
&
tmp_remote
,
&
adress
,
"pull"
);
//
}
import_tar
(
&
borg_folder
,
&
remote_file
,
&
adress
,
&
archive
)
//
}
}
// /// Get the borg config directory
/// Function that gets all the archives localised in a remote file-system
// ///
///
// /// # Description
/// # Arguments
// /// Get the directory defined with BORG_BASE_DIR env variable or the directory
/// - `url`: The url of the remote folder
// /// defined in HOME variable
/// # Return
// ///
/// A vector of archive
// /// # Return
fn
get_local_archives
(
url
:
&
PathBuf
)
->
Vec
<
String
>
{
// /// The borg base directory
let
mut
filenames
:
Vec
<
String
>
=
vec!
[
String
::
from
(
"*.tar*"
)];
// fn get_config_dir() -> PathBuf {
let
walker
=
globwalk
::
GlobWalkerBuilder
::
from_patterns
(
url
,
&
filenames
)
// let mut config = get_base_dir();
.max_depth
(
1
)
// config.push(".config");
.follow_links
(
false
)
// let new_config = if !std::env::var("BORG_BASE_DIR").is_ok() {
.build
()
// let mut new_config = match std::env::var("XDG_CONFIG_HOME") {
.unwrap
()
// Ok(p) => check_ok(&p),
.into_iter
()
// Err(_) => config,
.filter_map
(
Result
::
ok
)
// };
.map
(|
x
|
x
.path
()
.to_path_buf
())
// new_config.push("borg");
.collect
::
<
Vec
<
_
>>
();
// new_config
if
walker
.len
()
>
0
{
// } else {
let
mut
res
=
walker
// let config_path = match std::env::var("BORG_CONFIG_DIR") {
.into_iter
()
// Ok(p) => check_ok(&p),
.map
(|
x
|
x
.file_stem
()
.unwrap
()
.to_str
()
.unwrap
()
.to_string
())
// Err(_) => {
.collect
::
<
Vec
<
String
>>
();
// config.push("borg");
res
.sort
();
// config
res
.dedup
();
// }
res
// };
}
else
{
// config_path
Vec
::
new
()
// };
}
// if !new_config.is_dir() {
}
// eprintln!("{}: Failed to get borg config dir", &new_config.display());
// exit(1);
// }
// new_config
// }
// /// This function aims to get the borg security dir
/// Function that get archives from a remote file-system
// ///
/// # Arguments
// /// # Description
/// - `url`: The remote path
// /// Get the borg security folder defined by the BORG_SECURITY_DIR env variable
/// - `adress`: The adress of the remote folder
// /// or get the borg_config_dir/security path
/// # Return
// /// # Return
/// The vector of archives
// /// The path to the borg security dir
fn
get_remote_archives
(
url
:
&
PathBuf
,
adress
:
&
str
)
->
Vec
<
String
>
{
// fn get_security_dir() -> PathBuf {
let
remote_files
=
url
.join
(
"*.tar*"
);
// let security_dir = std::env::var("BORG_SECURITY_DIR");
let
args
=
vec!
[
adress
,
"ls"
,
remote_files
.to_str
()
.unwrap
()];
// match security_dir {
let
output
=
Command
::
new
(
"ssh"
)
// Ok(dir) => check_ok(&dir),
.args
(
&
args
)
// Err(_) => {
.stdout
(
Stdio
::
piped
())
// let mut path = get_config_dir();
.output
()
// path.push("security");
.unwrap
();
// return path;
let
str_paths
=
String
::
from_utf8
(
output
.stdout
)
.unwrap
();
// }
let
paths
=
str_paths
.split_ascii_whitespace
()
.collect
::
<
Vec
<&
str
>>
();
// }
if
paths
.len
()
>
0
{
// }
let
mut
archive_vec
=
paths
.into_iter
()
.map
(|
x
|
x
.to_owned
())
.collect
::
<
Vec
<
String
>>
();
archive_vec
.sort
();
archive_vec
.dedup
();
archive_vec
}
else
{
Vec
::
new
()
}
}
/// Function that get the list of archives stored in the remote directory
///
/// # Arguments
/// - `url`: The path of the remote
/// - `adress`: The adress of the remote folder
///
/// # Return
/// The list of archive found remotely
fn
get_archive_list
(
url
:
&
PathBuf
,
adress
:
&
str
)
->
Vec
<
String
>
{
if
adress
==
"file"
{
get_local_archives
(
url
)
}
else
{
get_remote_archives
(
url
,
adress
)
}
}
// /// Function that removes .borg cache
/// Function that perform the pull for all remote archive
// /// # Arguments
///
// /// - `borg_folder`: The .borg folder of the current project
/// # Argument:
// fn borg_delete_cache(borg_folder: &PathBuf) {
/// - `remote`: The name of the remote
// let args = vec!["delete", "--cache-only", borg_folder.to_str().unwrap()];
fn
pull_all_archives
(
remote
:
&
str
)
{
// let mut output = Command::new("borg")
let
(
adress
,
url
)
=
push
::
get_adress_and_url
(
remote
);
// .args(&args)
let
archives
=
get_archive_list
(
&
url
,
&
adress
);
// .stdout(Stdio::inherit())
for
archive
in
archives
{
// .stdin(Stdio::inherit())
pull_one_archive
(
remote
,
&
archive
);
// .spawn()
}
// .unwrap();
}
// match output.wait() {
// Err(e) => {
// eprintln!("{}: borg terminaned with an error: {}", "error".red(), e);
// exit(1);
// }
// Ok(_) => (),
// }
// }
// /// Create a push the tar achive on the selected remote path
/// Create a push the tar achive on the selected remote path
// /// # Arguments
/// # Arguments
// /// - `remote`: The name of a remote
/// - `remote`: The name of a remote
// pub fn pull(remote: &str) -> () {
/// - `archive`: The name of the archive to use (optional)
// let (adress, url) = push::get_adress_and_url(remote);
/// - `all`: A bollean indicating whether we want to push all new archive into
// let (borg_folder, _) = commit::check_path();
/// our .borg repository or not
// let borg_folder = borg_folder.canonicalize().unwrap();
pub
fn
pull
(
remote
:
&
str
,
archive
:
&
Option
<
String
>
,
all
:
bool
)
->
()
{
// push::check_dest_and_copy(&borg_folder, &url, &adress);
if
!
all
{
// // let remote_dir = push::get_remote_dir(&url);
match
archive
{
// // push::handle_existing_remote_dir(&remote_dir, &adress, "pull");
None
=>
{
// // save_and_clean_borg_folder(&borg_folder);
eprintln!
(
"{}: Nothing to do !"
,
"warning"
.yellow
());
// // push::export_tar(&borg_folder, &remote_dir, &adress, "pull");
return
();
// // remove_manifest_timestamp();
}
// // borg_delete_cache(&borg_folder);
Some
(
arc
)
=>
pull_one_archive
(
remote
,
arc
),
// }
}
}
else
{
match
archive
{
Some
(
_
)
=>
eprintln!
(
"{}: ignoring archive given in input and pulling all archives from remote repository."
,
"warning"
.yellow
()),
None
=>
()
}
pull_all_archives
(
remote
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment