import java.util.Timer;
public class SqlBackUp {
public void startUp() {
new Timer().schedule(new dbBackTimer(), 0, Util.time1);
new Timer().schedule(new DiffAndTrnTimer(), 0, Util.time2);
}
}
class dbBackTimer extends java.util.TimerTask {
public void run() {
Util.dbBack();
}
}
class DiffAndTrnTimer extends java.util.TimerTask {
public void run() {
Util.dbDiffAndTrn();
}
}
package sqlserverBackUp;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class Util {
static String url = "";
static String user = "";
static String password = "";
static String driver = "";
static long time1 = 0;
static long time2 = 0;
static{
String fileName="pro.properties";
Properties p = new Properties();
InputStream in = Util.class.getResourceAsStream(fileName);
try {
p.load(in);
in.close();
driver = p.getProperty("driver");
url = p.getProperty("url");
user = p.getProperty("user");
password = p.getProperty("password");
time1 = Long.parseLong(p.getProperty("time1"));
time2 = Long.parseLong(p.getProperty("time2"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getDate() {
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}
public static void handBackUp(){
dbBack();
dbDiffAndTrn();
}
public static int dbBack(){//备份整个数据库的方法
int result = 0;
Connection con = null;
Statement pstmt = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
String dbName = url.substring(url.lastIndexOf("=")+1, url.length());
String date = getDate();
String sql = "backup database "+dbName+" to disk='C:\\BackUp\\"+dbName+"("+date+").bak'";
pstmt = con.createStatement();
pstmt.execute(sql);
System.out.println("数据库备份成功!");
result = 1;
} catch (Exception e) {
System.out.println("数据库备份失败!");
e.printStackTrace();
}finally{
try {
pstmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
public static int dbDiffAndTrn(){//增量备份和日志备份
int result = 0;
Connection con = null;
Statement pstmt = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
String dbName = url.substring(url.lastIndexOf("=")+1, url.length());
String date = getDate();
pstmt = con.createStatement();
String sql = "backup database "+dbName+" to disk = 'C:\\BackUp\\"+dbName+"("+date+").diff' with differential";
pstmt.execute(sql);
System.out.println("数据库增量备份成功!");
sql = "backup log "+dbName+" to disk = 'C:\\BackUp\\"+dbName+"("+date+")_log.trn'";
pstmt.execute(sql);
System.out.println("数据库日志备份成功!");
result = 1;
} catch (Exception e) {
System.out.println("增量备份或备份日志时出错!");
e.printStackTrace();
}finally{
try {
pstmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
}
配置文件(pro.properties):
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;databaseName=BESTJ2EE
user=sa
password=111
time1=2592000000
time2=86400000