当前位置: 首页 > news >正文

winserver备份到miniio

  1. winserver 安装openssh

  2. 备份代码
    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. 直接从远程流上传到 MinIOuploadRemoteZipToMinIO();// 4. 可选:删除远程 zipdeleteRemoteZip();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();}}}

}

http://www.hskmm.com/?act=detail&tid=33364

相关文章:

  • 软考中级 学习总结*(1)
  • 【黑马python】基础 6.Python 容器:列表 元组 字符串 集合 字典
  • 刷题日记—数字读取与判断
  • Linux Mint -- LMDE6升级到LMDE7
  • OI 笑传 #19
  • CF1133 合集
  • 10/18
  • 实验一 现代C++基础编程
  • CF1824D 题解
  • 单目深度估计 - MKT
  • CF1059 Codeforces Round 1059 (Div. 3) 游记
  • newDay12
  • PyTorch与卷积神经网络读书报告
  • QOJ857 Social Distancing
  • 142. 环形链表 II
  • 10.17日学习笔记
  • KV缓存(Key-Value Cache)
  • 模型验证
  • Transformer
  • 面试题 02.07. 链表相交
  • 10月17日记
  • 带高度多边形,生成3D建筑模型,支持多种颜色或纹理的OBJ、GLTF、3DTiles格式
  • aaaaaa
  • 突然发现,越研究越没意思
  • 无需重新训练即可为语音识别器添加新词
  • 思科关键漏洞警报:TACACS+认证缺陷可导致网络完全暴露
  • ysyx学习:移植rt-thread
  • 综合性题目
  • 实用指南:从入门到精通:Django的深度探索之旅
  • UML中9中数据流图总结