2023-11-19 02:12:44 +03:00
|
|
|
package Visual_DVM_2021.Passes.All;
|
2023-09-17 22:13:42 +03:00
|
|
|
import Common.Global;
|
|
|
|
|
import Common.Utils.Utils;
|
|
|
|
|
import GlobalData.Machine.Machine;
|
|
|
|
|
import GlobalData.RemoteFile.RemoteFile;
|
|
|
|
|
import GlobalData.User.User;
|
2024-01-08 20:37:16 +03:00
|
|
|
import Visual_DVM_2021.Passes.SSH.ConnectionPass;
|
2023-09-17 22:13:42 +03:00
|
|
|
import com.jcraft.jsch.ChannelSftp;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.nio.file.Paths;
|
2024-08-18 01:08:56 +03:00
|
|
|
import java.util.Comparator;
|
2023-09-17 22:13:42 +03:00
|
|
|
import java.util.Vector;
|
|
|
|
|
public class ArchivesBackupPass extends ConnectionPass<File> {
|
|
|
|
|
File src;
|
|
|
|
|
@Override
|
|
|
|
|
protected boolean canStart(Object... args) throws Exception {
|
|
|
|
|
machine = (Machine) args[0];
|
|
|
|
|
user = (User) args[1];
|
|
|
|
|
src = (File) args[2];
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
protected void ServerAction() throws Exception {
|
2024-01-08 20:37:16 +03:00
|
|
|
String workspace_path = Utils.toU(Paths.get(user.connection.sftpChannel.getHome(), Global.properties.BackupWorkspace).toString());
|
2023-09-17 22:13:42 +03:00
|
|
|
RemoteFile workspace = new RemoteFile(workspace_path, true);
|
2024-01-08 20:37:16 +03:00
|
|
|
user.connection.MKDIR(workspace);
|
2023-09-17 22:13:42 +03:00
|
|
|
RemoteFile dst = new RemoteFile(workspace.full_name, src.getName());
|
2024-01-08 20:37:16 +03:00
|
|
|
user.connection.putSingleFile(src, dst);
|
2023-09-17 22:13:42 +03:00
|
|
|
//-теперь, удалить старые файлы.
|
2024-01-08 20:37:16 +03:00
|
|
|
Vector<ChannelSftp.LsEntry> raw_files = user.connection.sftpChannel.ls(workspace.full_name);
|
2023-09-17 22:13:42 +03:00
|
|
|
Vector<RemoteFile> files = new Vector<>();
|
|
|
|
|
for (ChannelSftp.LsEntry file : raw_files) {
|
|
|
|
|
if (!file.getAttrs().isDir()) {
|
|
|
|
|
RemoteFile rfile = new RemoteFile(workspace.full_name, file.getFilename(), false);
|
|
|
|
|
rfile.updateTime = RemoteFile.convertUpdateTime(file.getAttrs().getMTime());
|
|
|
|
|
files.add(rfile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//сортируем по времени обновления. по убыванию.
|
2024-08-18 01:08:56 +03:00
|
|
|
files.sort(new Comparator<RemoteFile>() {
|
|
|
|
|
@Override
|
|
|
|
|
public int compare(RemoteFile o1, RemoteFile o2) {
|
|
|
|
|
return Long.compare(o1.updateTime, o2.updateTime);
|
|
|
|
|
}
|
|
|
|
|
}.reversed()
|
|
|
|
|
);
|
2023-09-17 22:13:42 +03:00
|
|
|
for (int i = 2; i < files.size(); ++i) {
|
2024-01-08 20:37:16 +03:00
|
|
|
user.connection.sftpChannel.rm(files.get(i).full_name);
|
2023-09-17 22:13:42 +03:00
|
|
|
}
|
|
|
|
|
//--------------
|
|
|
|
|
}
|
|
|
|
|
}
|