需要导入ganymed-ssh2-build210.jar包。
package com.linux.backup;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import java.io.*;
import org.apache.commons.lang3.StringUtils;
public class BackupLinux {
//字符编码默认是utf-8
private static String DEFAULTCHART="UTF-8";
private static Connection conn;
private String ip;
private String userName;
private String userPwd;
public BackupLinux() {
super();
}
public BackupLinux(String ip,String userName,String userPwd) {
this.ip = ip;
this.userName = userName;
this.userPwd = userPwd;
}
public static void setCharset(String charset) {
DEFAULTCHART = charset;
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
/**
* 远程登录linux的主机
* @return
* 登录成功返回true,否则返回false
*/
public Boolean login(){
boolean flg=false;
try {
conn = new Connection(ip);
conn.connect();//连接
flg=conn.authenticateWithPassword(userName, userPwd);//认证
if (flg){
System.out.println("认证成功!");
}
} catch (IOException e) {
e.printStackTrace();
}
return flg;
}
/**
* 远程执行shll脚本或者命令
* @param cmd
* 即将执行的命令
* @return
* 命令执行成功后返回的结果值,如果命令执行失败,返回空字符串,不是null
* @since V0.1
*/
public String executeSuccess(String cmd){
String result="";
try {
if(login()){
Session session= conn.openSession();//打开一个会话
session.execCommand(cmd);//执行命令
//获取指令是否成功,成功--0,不成功--非0
if(session.getExitStatus()==0){
System.out.println("备份成功");
result=processStdout(session.getStderr(),DEFAULTCHART);
System.out.println(result);
session.close();
}
else {
System.out.println("备份不成功");
result=processStdout(session.getStderr(),DEFAULTCHART);
System.out.println(result);
session.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 解析脚本执行返回的结果集
* @param in 输入流对象
* @param charset 编码
* @return
* 以纯文本的格式返回
*/
public static String processStdout(InputStream in, String charset){
InputStream stdout = new StreamGobbler(in);
StringBuffer buffer = new StringBuffer();;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stdout,charset));
String line=null;
while((line=br.readLine()) != null){
buffer.append(line+"\n");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer.toString();
}
public static void main(String[] args) {
BackupLinux rec=new BackupLinux("192.168.124.45", "root","123456");
//数据库备份
try {
if(rec.login()){
Session session= conn.openSession();//打开一个会话,执行备份语句
//TODO:多条命令,修改自己的IP,用户名和密码,--result-file==(备份生成的文件存储地址,是存在Linux系统下的)最后的user是要备份的数据库名
session.execCommand("mysqldump --opt -h 192.168.124.45 --user=root --password=123456 --lock-all-tables=true --result-file=/usr/local/mysql/new1.sql --default-character-set=utf8 user");//执行命令
String result=processStdout(session.getStdout(),DEFAULTCHART);
//获取指令是否成功,成功--0,不成功--非0
if(session.getExitStatus()==0){
System.out.println("备份成功");
result=processStdout(session.getStderr(),DEFAULTCHART);
System.out.println(result);
session.close();
}
else {
System.out.println("备份不成功");
result=processStdout(session.getStderr(),DEFAULTCHART);
System.out.println(result);
session.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
//数据库恢复
if(rec.login()){
try {
Session session2 = conn.openSession();
session2.execCommand("mysql -uroot -p123456 user </usr/local/mysql/new1.sql");
String result=processStdout(session2.getStdout(),DEFAULTCHART);
if(session2.getExitStatus()==0){
System.out.println("恢复成功");
result=processStdout(session2.getStderr(),DEFAULTCHART);
System.out.println(result);
session2.close();
}
else {
System.out.println("恢复不成功");
result=processStdout(session2.getStderr(),DEFAULTCHART);
System.out.println(result);
session2.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}