winserver备份到miniio
-
winserver 安装openssh
-
备份代码
package org.hf.ywyt_minio.openssh;
import com.jcraft.jsch.*;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.hf.ywyt_minio.denum.DateFmt;
import org.hf.ywyt_minio.util.DateFmtUtil;
import java.io.InputStream;
import java.util.Properties;
//远程上传目录demo
public class SshDirToMinio {
/* --- MinIO --- */
private static final String MINIO_URL = "http://192.168.10.12:9000";
private static final String MINIO_AK = "H";
private static final String MINIO_SK = "7g";
private static final String BUCKET = "rpt";
private static final String OBJECT_KEY = "rpt"+ DateFmtUtil.getDateFmt(DateFmt.FULL_minio) +".zip";
private static final String SSH_HOST = "192.168.10.2"; // WinServer IP
private static final int SSH_PORT = 22;
private static final String SSH_USER = "administrator";
private static final String SSH_PASS = "Hxxx";
private static final String REMOTE_DIR = "D:\\tomcat-win64\\webapps\\webroot"; // 要压缩的目录
private static final String REMOTE_ZIP = "D:\\temp\\"+ OBJECT_KEY; // 远程 zip 路径
private static final String REMOTE_ZIP_UPLOAD = '/'+REMOTE_ZIP.replace('\\', '/');
private static final MinioClient minio = MinioClient.builder()
.endpoint(MINIO_URL)
.credentials(MINIO_AK, MINIO_SK)
.build();
private static Session session;
static {
try {
JSch jsch = new JSch();
session = jsch.getSession(SSH_USER, SSH_HOST, SSH_PORT);
session.setPassword(SSH_PASS);
Properties cfg = new Properties();
cfg.put("StrictHostKeyChecking", "no");
session.setConfig(cfg);
session.connect(10_000);
} catch (JSchException e) {
session.disconnect();
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
try {
// 1. 桶存在检查
if (!minio.bucketExists(BucketExistsArgs.builder().bucket(BUCKET).build())) {
minio.makeBucket(MakeBucketArgs.builder().bucket(BUCKET).build());
}
// 2. 远程压缩
zipOnWindows();
// 3. 直接从远程流上传到 MinIO
uploadRemoteZipToMinIO();
// 4. 可选:删除远程 zip
deleteRemoteZip();
System.out.println("✅ 远程压缩 → 流式上传 MinIO 完成!");
} catch (Exception e){
throw new RuntimeException(e);
} finally {
if(minio != null) {
try {
minio.close();
} catch (Exception e2) {
throw new RuntimeException(e2);
}
}
}
}
/* 1. 远程压缩 */
private static void zipOnWindows() {
ChannelExec exec=null;
try {
// String cmd = String.format(
// "powershell -command "if (Test-Path '%s') { Remove-Item '%s' -Force }; " +
// "Compress-Archive -Path '%s\*' -DestinationPath '%s'"",
// REMOTE_ZIP, REMOTE_ZIP, REMOTE_DIR, REMOTE_ZIP);
String cmd = String.format(
"powershell -command \"Compress-Archive -Path '%s' -DestinationPath '%s'\"",
REMOTE_DIR, REMOTE_ZIP);
//System.out.println(">>> 远程命令:\n" + cmd);
exec = (ChannelExec) session.openChannel("exec");
exec.setCommand(cmd);
exec.setErrStream(System.err);
exec.connect();
while (!exec.isClosed()) Thread.sleep(500);
int exit = exec.getExitStatus();
if (exit != 0) throw new RuntimeException("远程压缩失败,exit=" + exit);
System.out.println("✅ 远程压缩完成 -> " + REMOTE_ZIP);
}catch (JSchException | InterruptedException je){
if(session != null) {
session.disconnect();
}
throw new RuntimeException("远程压缩失败"+je.getMessage());
} finally {
if (exec != null) {
exec.disconnect();
}
}
}
/* 2. 从远程流直接上传 MinIO */
private static void uploadRemoteZipToMinIO() {
ChannelSftp sftp=null;
try{
sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
// 获取远程文件大小
long size = sftp.lstat(REMOTE_ZIP_UPLOAD).getSize();
// 获取远程输入流
try ( InputStream in = sftp.get(REMOTE_ZIP_UPLOAD) ) {
minio.putObject(
PutObjectArgs.builder()
.bucket(BUCKET)
.object(OBJECT_KEY) // 对象名
.stream(in, size, -1) // 大小已知,分片自动64m
.contentType("application/zip")
.build());
}
System.out.println("✅ 已流式上传到 MinIO: " + BUCKET );
}catch (Exception je){
if(session != null) {
session.disconnect();
}
throw new RuntimeException("上传失败"+je.getMessage());
} finally {
if (sftp != null) {
sftp.disconnect();
}
}
}
/* 3. 删除远程 zip */
private static void deleteRemoteZip() throws Exception {
ChannelExec exec =null;
try{
exec = (ChannelExec) session.openChannel("exec");
exec.setCommand("powershell -command \"Remove-Item '" + REMOTE_ZIP + "' -Force\"");
exec.connect();
while (!exec.isClosed()) Thread.sleep(200);
System.out.println("✅ 已清理远程 zip");
}catch (Exception je){
throw new RuntimeException("删除"+je.getMessage());
} finally {
if (exec != null) {
exec.disconnect();
}
if(session != null) {
session.disconnect();
}
}
}
}

浙公网安备 33010602011771号