-
Notifications
You must be signed in to change notification settings - Fork 2
/
hab_recover.m
79 lines (79 loc) · 2.83 KB
/
hab_recover.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function hab_recover(user,account,matresult,matname,varargin)
d.pw_ssh = [];
d.scratch_dir = '/rigel';%on remote host
d.remotehost = 'habanero.rcs.columbia.edu';
d.remote_subdir = 'Local';
%%
v = inputParser;
addParameter(v,'pw_ssh', d.pw_ssh);
addParameter(v,'scratch_dir', d.scratch_dir);
addParameter(v,'remotehost', d.remotehost);
addParameter(v,'remote_subdir', d.remote_subdir);
parse(v,varargin{:});
v = v.Results;clear d;
%%
auxf_dir = mfilename('fullpath');
auxf_dir = fullfile(fileparts(auxf_dir),'auxf');
addpath(genpath(auxf_dir));
%%
filesep_unix = '/';
if ispc
warning('Untested on Windows');
end
%%
remotePath = [v.scratch_dir, filesep_unix, account, filesep_unix, 'users'];
remotePath_user = [remotePath, filesep_unix, user];
remotePath_user_workdir = [remotePath_user, filesep_unix, v.remote_subdir];
%%
if isempty(v.pw_ssh)
v.pw = get_password;
else
v.pw = v.pw_ssh;
end
%%
tar_file = [matname '_' matresult '.tar'];
localPath = tempname;mkdir(localPath);
locaPath_subdir = fullfile(localPath,[matname '_' matresult]);mkdir(locaPath_subdir);
%%
if not(exist('ssh2_struct','var')==1)
pw_clear = v.pw.ssh;
if not(ischar(pw_clear)),error('Password should be a string here - maybe it is nested in a struc?');end
fprintf('Setting up SSH connection...\n');
ssh2_struct = ssh2_config(v.remotehost, user, pw_clear, 22);
ssh2_struct = ssh2(ssh2_struct);
clear pw;
end
try
%%
positive_exist_string = 'yes';
command = sprintf('[ -d "%s" ] && echo "%s"',...
[remotePath_user_workdir, filesep_unix, matname], positive_exist_string);
[ssh2_struct, command_result] = ssh2_command(ssh2_struct, command, false);
target_exists = strcmpi(command_result{1}, positive_exist_string);
if not(target_exists),ssh2_struct = ssh2_close(ssh2_struct);error('Folder does not exist remotely.');end
%%
fprintf('Creating archive remotely...\n');
command = sprintf('tar -C %s -cvf %s %s',...
[remotePath_user_workdir, filesep_unix, matname], tar_file, matresult);
[ssh2_struct, command_result] = ssh2_command(ssh2_struct, command, false);
%%
fprintf('Transferring archive.\n');
ssh2_struct = scp_get(ssh2_struct, tar_file, localPath, '');
%% Get the matresult directory back and as [matresult '_x']
f = untar(fullfile(localPath, tar_file), localPath);
if not(exist(fullfile(pwd,[matresult '_x']),'dir')==7)
mkdir(fullfile(pwd,[matresult '_x']));
end
copyfile(fullfile(localPath, matresult), fullfile(pwd, [matresult '_x']));
%%
fprintf('Deleting remote archive...\n');
command = sprintf('rm %s', tar_file);
[ssh2_struct, command_result] = ssh2_command(ssh2_struct, command, false);
%%
ssh2_struct = hab_cleanup(ssh2_struct, localPath, auxf_dir);
%%
catch reerr
ssh2_struct = hab_cleanup(ssh2_struct, localPath, auxf_dir);
rethrow(reerr);
end
end