1. 程式人生 > >技術實現(2)之資料庫備份恢復的設計與實現

技術實現(2)之資料庫備份恢復的設計與實現

APDPlat提供了web介面的資料庫備份與恢復支援手工操作和定時排程,可下載備份檔案到本地,也可把備份檔案傳送到異地容錯,極大地簡化了資料庫的維護工作。

設計目標:

1、多資料庫支援

2、橫切關注點隔離

3、異地容錯

下面闡述具體的設計及實現:

1、為了支援多資料庫,統一的介面是不可避免的,如下所示:

Java程式碼 複製程式碼 收藏程式碼
  1. /** 
  2.  * 備份恢復資料庫介面 
  3.  * @author 楊尚川 
  4.  */
  5. publicinterface BackupService {  
  6.     /** 
  7.      * 備份資料庫 
  8.      * @return 是否備份成功 
  9.      */
  10.     publicboolean
     backup();  
  11.     /** 
  12.      * 恢復資料庫 
  13.      * @param date 
  14.      * @return 是否恢復成功 
  15.      */
  16.     publicboolean restore(String date);  
  17.     /** 
  18.      * 獲取已經存在的備份檔名稱列表 
  19.      * @return  備份檔名稱列表 
  20.      */
  21.     public List<String> getExistBackupFileNames();      
  22.     /** 
  23.      * 獲取備份檔案存放的本地檔案系統路徑 
  24.      * @return 備份檔案存放路徑 
  25.      */
  26.     public String getBackupFilePath();  
  27.     /** 
  28.      * 獲取最新的備份檔案 
  29.      * @return 最新的備份檔案 
  30.      */
  31.     public File getNewestBackupFile();}  
/**
 * 備份恢復資料庫介面
 * @author 楊尚川
 */
public interface BackupService {
    /**
     * 備份資料庫
     * @return 是否備份成功
     */
    public boolean backup();
    /**
     * 恢復資料庫
     * @param date
     * @return 是否恢復成功
     */
    public boolean restore(String date);
    /**
     * 獲取已經存在的備份檔名稱列表
     * @return  備份檔名稱列表
     */
    public List<String> getExistBackupFileNames();    
    /**
     * 獲取備份檔案存放的本地檔案系統路徑
     * @return 備份檔案存放路徑
     */
    public String getBackupFilePath();
    /**
     * 獲取最新的備份檔案
     * @return 最新的備份檔案
     */
    public File getNewestBackupFile();}
 

    對於各個不同的資料庫來說,有一些通用的操作,如對加密的資料庫使用者名稱和密碼的解密操作,還有介面定義的備份檔案存放的本地檔案系統路徑,用一個抽象類來實現介面中的通用方法以及其他通用方法如decrypt

Java程式碼 複製程式碼 收藏程式碼
  1. /** 
  2.  *備份恢復資料庫抽象類,抽象出了針對各個資料庫來說通用的功能 
  3.  * @author 楊尚川 
  4.  */
  5. publicabstractclass AbstractBackupService implements BackupService{    
  6.     protectedfinal APDPlatLogger LOG = new APDPlatLogger(getClass());  
  7.     protectedstaticfinal StandardPBEStringEncryptor encryptor;  
  8.     protectedstaticfinal String username;  
  9.     protectedstaticfinal String password;  
  10.     //從配置檔案中獲取資料庫使用者名稱和密碼,如果使用者名稱和密碼被加密,則解密
  11.     static{  
  12.             EnvironmentStringPBEConfig config=new EnvironmentStringPBEConfig();  
  13.             config.setAlgorithm("PBEWithMD5AndDES");  
  14.             config.setPassword("config");  
  15.             encryptor=new StandardPBEStringEncryptor();  
  16.             encryptor.setConfig(config);  
  17.             String uname=PropertyHolder.getProperty("db.username");  
  18.             String pwd=PropertyHolder.getProperty("db.password");  
  19.             if(uname!=null && uname.contains("ENC(") && uname.contains(")")){  
  20.                 uname=uname.substring(4,uname.length()-1);  
  21.                 username=decrypt(uname);  
  22.             }else{  
  23.                 username=uname;  
  24.             }  
  25.             if(pwd!=null && pwd.contains("ENC(") && pwd.contains(")")){  
  26.                 pwd=pwd.substring(4,pwd.length()-1);  
  27.                 password=decrypt(pwd);  
  28.             }else{  
  29.                 password=pwd;  
  30.             }  
  31.     }  
  32.     @Override
  33.     public String getBackupFilePath(){  
  34.         String path="/WEB-INF/backup/"+PropertyHolder.getProperty("jpa.database")+"/";  
  35.         path=FileUtils.getAbsolutePath(path);  
  36.         File file=new File(path);  
  37.         if(!file.exists()){  
  38.             file.mkdirs();  
  39.         }  
  40.         return path;  
  41.     }  
  42.     @Override
  43.     public File getNewestBackupFile(){  
  44.         Map<String,File> map = new HashMap<>();  
  45.         List<String> list = new ArrayList<>();  
  46.         String path=getBackupFilePath();  
  47.         File dir=new File(path);  
  48.         File[] files=dir.listFiles();  
  49.         for(File file : files){  
  50.             String name=file.getName();  
  51.             if(!name.contains("bak")) {  
  52.                 continue;  
  53.             }  
  54.             map.put(name, file);  
  55.             list.add(name);  
  56.         }  
  57.         if(list.isEmpty()){  
  58.             returnnull;  
  59.         }  
  60.         //按備份時間排序
  61.         Collections.sort(list);  
  62.         //最新備份的在最前面
  63.         Collections.reverse(list);  
  64.         String name = list.get(0);  
  65.         File file = map.get(name);  
  66.         //加速垃圾回收
  67.         list.clear();  
  68.         map.clear();  
  69.         return file;  
  70.     }    @Override
  71.     public List<String> getExistBackupFileNames(){  
  72.         List<String> result=new ArrayList<>();  
  73.         String path=getBackupFilePath();  
  74.         File dir=new File(path);  
  75.         File[] files=dir.listFiles();  
  76.         for(File file : files){  
  77.             String name=file.getName();  
  78.             if(!name.contains("bak")) {  
  79.                 continue;  
  80.             }  
  81.             name=name.substring(0, name.length()-4);  
  82.             String[] temp=name.split("-");  
  83.             String y=temp[0];  
  84.             String m=temp[1];  
  85.             String d=temp[2];  
  86.             String h=temp[3];  
  87.             String mm=temp[4];  
  88.             String s=temp[5];  
  89.             name=y+"-"+m+"-"+d+" "+h+":"+mm+":"+s;  
  90.             result.add(name);  
  91.         }  
  92.         //按備份時間排序
  93.         Collections.sort(result);  
  94.         //最新備份的在最前面
  95.         Collections.reverse(result);  
  96.         return result;  
  97.     }  
  98.     /** 
  99.      * 解密使用者名稱和密碼 
  100.      * @param encryptedMessage 加密後的使用者名稱或密碼 
  101.      * @return 解密後的使用者名稱或密碼 
  102.      */
  103.     protectedstatic String decrypt(String encryptedMessage){  
  104.         String plain=encryptor.decrypt(encryptedMessage);  
  105.         return plain;  
  106.     }    
  107. }  
/**
 *備份恢復資料庫抽象類,抽象出了針對各個資料庫來說通用的功能
 * @author 楊尚川
 */
public abstract class AbstractBackupService implements BackupService{  
    protected final APDPlatLogger LOG = new APDPlatLogger(getClass());
    
    protected static final StandardPBEStringEncryptor encryptor;
    protected static final String username;
    protected static final String password;
    //從配置檔案中獲取資料庫使用者名稱和密碼,如果使用者名稱和密碼被加密,則解密
    static{
            EnvironmentStringPBEConfig config=new EnvironmentStringPBEConfig();
            config.setAlgorithm("PBEWithMD5AndDES");
            config.setPassword("config");

            encryptor=new StandardPBEStringEncryptor();
            encryptor.setConfig(config);
            String uname=PropertyHolder.getProperty("db.username");
            String pwd=PropertyHolder.getProperty("db.password");
            if(uname!=null && uname.contains("ENC(") && uname.contains(")")){
                uname=uname.substring(4,uname.length()-1);
                username=decrypt(uname);
            }else{
                username=uname;
            }
            if(pwd!=null && pwd.contains("ENC(") && pwd.contains(")")){
                pwd=pwd.substring(4,pwd.length()-1);
                password=decrypt(pwd);
            }else{
                password=pwd;
            }
    }
    @Override
    public String getBackupFilePath(){
        String path="/WEB-INF/backup/"+PropertyHolder.getProperty("jpa.database")+"/";
        path=FileUtils.getAbsolutePath(path);
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        return path;
    }
    @Override
    public File getNewestBackupFile(){
        Map<String,File> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        String path=getBackupFilePath();
        File dir=new File(path);
        File[] files=dir.listFiles();
        for(File file : files){
            String name=file.getName();
            if(!name.contains("bak")) {
                continue;
            }
            map.put(name, file);
            list.add(name);
        }
        if(list.isEmpty()){
            return null;
        }
        //按備份時間排序
        Collections.sort(list);
        //最新備份的在最前面
        Collections.reverse(list);
        
        String name = list.get(0);
        File file = map.get(name);
        //加速垃圾回收
        list.clear();
        map.clear();
        
        return file;
    }    @Override
    public List<String> getExistBackupFileNames(){
        List<String> result=new ArrayList<>();
        String path=getBackupFilePath();
        File dir=new File(path);
        File[] files=dir.listFiles();
        for(File file : files){
            String name=file.getName();
            if(!name.contains("bak")) {
                continue;
            }
            name=name.substring(0, name.length()-4);
            String[] temp=name.split("-");
            String y=temp[0];
            String m=temp[1];
            String d=temp[2];
            String h=temp[3];
            String mm=temp[4];
            String s=temp[5];
            name=y+"-"+m+"-"+d+" "+h+":"+mm+":"+s;
            result.add(name);
        }
        //按備份時間排序
        Collections.sort(result);
        //最新備份的在最前面
        Collections.reverse(result);

        return result;
    }
    /**
     * 解密使用者名稱和密碼
     * @param encryptedMessage 加密後的使用者名稱或密碼
     * @return 解密後的使用者名稱或密碼
     */
    protected static String decrypt(String encryptedMessage){
        String plain=encryptor.decrypt(encryptedMessage);
        return plain;
    }  
}

    下面來看一個MySQL資料庫的實現:

Java程式碼 複製程式碼 收藏程式碼
  1. /** 
  2.  *MySQL備份恢復實現 
  3.  * @author 楊尚川 
  4.  */
  5. @Service("MYSQL")  
  6. publicclass MySQLBackupService extends AbstractBackupService{  
  7.     /** 
  8.      * MySQL備份資料庫實現 
  9.      * @return  
  10.      */
  11.     @Override
  12.     publicboolean backup() {  
  13.         try {  
  14.             String path=getBackupFilePath()+DateTypeConverter.toFileName(new Date())+".bak";  
  15.             String command=PropertyHolder.getProperty("db.backup.command");  
  16.             command=command.replace("${db.username}", username);  
  17.             command=command.replace("${db.password}", password);  
  18.             command=command.replace("${module.short.name}", PropertyHolder.getProperty("module.short.name"));  
  19.             Runtime runtime = Runtime.getRuntime();  
  20.             Process child = runtime.exec(command);  
  21.             InputStream in = child.getInputStream();  
  22.             try(OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path), "utf8");BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf8"))){  
  23.                 String line=reader.readLine();  
  24.                 while (line != null) {  
  25.                     writer.write(line+"\n");  
  26.                     line=reader.readLine();  
  27.                 }  
  28.                 writer.flush();  
  29.             }  
  30.             LOG.debug("備份到:"+path);  
  31.             returntrue;  
  32.         } catch (Exception e) {  
  33.             LOG.error("備份出錯",e);  
  34.         }  
  35.         returnfalse;  
  36.     }  
  37.     /** 
  38.      * MySQL恢復資料庫實現 
  39.      * @param date 
  40.      * @return  
  41.      */
  42.     @Override
  43.     publicboolean restore(String date) {  
  44.         try {  
  45.             String path=getBackupFilePath()+date+".bak";  
  46.             String command=PropertyHolder.getProperty("db.restore.command");  
  47.             command=command.replace("${db.username}", username);  
  48.             command=command.replace("${db.password}", password);  
  49.             command=command.replace("${module.short.name}", PropertyHolder.getProperty("module.short.name"));  
  50.             Runtime runtime = Runtime.getRuntime();  
  51.             Process child = runtime.exec(command);  
  52.             try(OutputStreamWriter writer = new OutputStreamWriter(child.getOutputStream(), "utf8");BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "utf8"))){  
  53.                 String line=reader.readLine();  
  54.                 while (line != null) {  
  55.                     writer.write(line+"\n");  
  56.                     line=reader.readLine();  
  57.                 }  
  58.                 writer.flush();  
  59.             }  
  60.             LOG.debug("從 "+path+" 恢復");  
  61.             returntrue;  
  62.         } catch (Exception e) {  
  63.             LOG.error("恢復出錯",e);  
  64.         }  
  65.         returnfalse;  
  66.     }  
  67. }  
/**
 *MySQL備份恢復實現
 * @author 楊尚川
 */
@Service("MYSQL")
public class MySQLBackupService extends AbstractBackupService{
 
    /**
     * MySQL備份資料庫實現
     * @return 
     */
    @Override
    public boolean backup() {
        try {
            String path=getBackupFilePath()+DateTypeConverter.toFileName(new Date())+".bak";
            String command=PropertyHolder.getProperty("db.backup.command");
            command=command.replace("${db.username}", username);
            command=command.replace("${db.password}", password);
            command=command.replace("${module.short.name}", PropertyHolder.getProperty("module.short.name"));

            Runtime runtime = Runtime.getRuntime();
            Process child = runtime.exec(command);
            InputStream in = child.getInputStream();

            try(OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path), "utf8");BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf8"))){
                String line=reader.readLine();
                while (line != null) {
                    writer.write(line+"\n");
                    line=reader.readLine();
                }
                writer.flush();
            }
            LOG.debug("備份到:"+path);
            return true;
        } catch (Exception e) {
            LOG.error("備份出錯",e);
        }
        return false;
    }

    /**
     * MySQL恢復資料庫實現
     * @param date
     * @return 
     */
    @Override
    public boolean restore(String date) {
        try {
            String path=getBackupFilePath()+date+".bak";
            String command=PropertyHolder.getProperty("db.restore.command");
            command=command.replace("${db.username}", username);
            command=command.replace("${db.password}", password);
            command=command.replace("${module.short.name}", PropertyHolder.getProperty("module.short.name"));
            
            Runtime runtime = Runtime.getRuntime();
            Process child = runtime.exec(command);
            try(OutputStreamWriter writer = new OutputStreamWriter(child.getOutputStream(), "utf8");BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "utf8"))){
                String line=reader.readLine();
                while (line != null) {
                    writer.write(line+"\n");
                    line=reader.readLine();
                }
                writer.flush();
            }
            LOG.debug("從 "+path+" 恢復");
            return true;
        } catch (Exception e) {
            LOG.error("恢復出錯",e);
        }
        return false;
    }
}

    這裡的關鍵有兩點,從配置檔案db.properties或db.local.properties中獲取指定的命令進行備份和恢復操作,為實現類指定註解@Service("MYSQL"),這裡服務名稱必須和配置檔案db.properties或db.local.properties中jpa.database的值一致,jpa.database的值指定了當前使用哪一種資料庫,如下所示:

Java程式碼 複製程式碼 收藏程式碼
  1. #mysql  
  2. db.driver=com.mysql.jdbc.Driver  
  3. db.url=jdbc:mysql://localhost:3306/${module.short.name}?useUnicode=true&characterEncoding=UTF-8&createDatabaseIfNotExist=true&autoReconnect=true
  4. db.username=ENC(i/TOu44AD6Zmz0fJwC32jQ==)  
  5. db.password=ENC(i/TOu44AD6Zmz0fJwC32jQ==)  
  6. jpa.database=MYSQL  
  7. db.backup.command=mysqldump  -u${db.username} -p${db.password} ${module.short.name}  
  8. db.restore.command=mysql -u${db.username} -p${db.password} ${module.short.name}  
#mysql
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/${module.short.name}?useUnicode=true&characterEncoding=UTF-8&createDatabaseIfNotExist=true&autoReconnect=true
db.username=ENC(i/TOu44AD6Zmz0fJwC32jQ==)
db.password=ENC(i/TOu44AD6Zmz0fJwC32jQ==)
jpa.database=MYSQL
db.backup.command=mysqldump  -u${db.username} -p${db.password} ${module.short.name}
db.restore.command=mysql -u${db.username} -p${db.password} ${module.short.name}

    有了介面和多個實現,那麼備份和恢復的時候究竟選擇哪一種資料庫實現呢?BackupServiceExecuter充當工廠類(Factory),負責從多個數據庫備份恢復實現類中選擇一個並執行相應的備份和恢復操作,BackupServiceExecuter也實現了BackupService介面,這也是一個典型的外觀(Facade)設計模式,封裝了選擇特定資料庫的邏輯。

    定時排程器和web前端控制器也是使用BackupServiceExecuter來執行備份恢復操作BackupServiceExecuter通過每個實現類以@Service註解指定的名稱以及配置檔案db.properties或db.local.properties中jpa.database的值來做選擇的依據,如下所示:

Java程式碼 複製程式碼 收藏程式碼
  1. /** 
  2.  *執行備份恢復的服務,自動判斷使用的是什麼資料庫,並找到該資料庫備份恢復服務的實現並執行 
  3.  * @author 楊尚川 
  4.  */
  5. @Service
  6. publicclass BackupServiceExecuter extends AbstractBackupService{    
  7.     private BackupService backupService=null;  
  8.     @Resource(name="backupFileSenderExecuter")  
  9.     private BackupFileSenderExecuter backupFileSenderExecuter;  
  10.     /** 
  11.      * 查詢並執行正在使用的資料的備份實現例項 
  12.      * @return  
  13.      */
  14.     @Override
  15.     publicboolean backup() {  
  16.         if(backupService==null){  
  17.             backupService=SpringContextUtils.getBean(PropertyHolder.getProperty("jpa.database"));  
  18.         }  
  19.         boolean result = backupService.backup();  
  20.         //如果備份成功,則將備份檔案發往他處
  21.         if(result){  
  22.             backupFileSenderExecuter.send(getNewestBackupFile());  
  23.         }  
  24.         return result;  
  25.     }  
  26.     /** 
  27.      * 查詢並執行正在使用的資料的恢復實現例項 
  28.      * @param date 
  29.      * @return  
  30.      */
  31.     @Override
  32.     publicboolean restore(String date) {  
  33.         if(backupService==null){  
  34.             backupService=SpringContextUtils.getBean(PropertyHolder.getProperty("jpa.database"));  
  35.         }  
  36.         return backupService.restore(date);  
  37.     }      
  38. }  
/**
 *執行備份恢復的服務,自動判斷使用的是什麼資料庫,並找到該資料庫備份恢復服務的實現並執行
 * @author 楊尚川
 */
@Service
public class BackupServiceExecuter extends AbstractBackupService{  
    private BackupService backupService=null;
    
    @Resource(name="backupFileSenderExecuter")
    private BackupFileSenderExecuter backupFileSenderExecuter;
    /**
     * 查詢並執行正在使用的資料的備份實現例項
     * @return 
     */
    @Override
    public boolean backup() {
        if(backupService==null){
            backupService=SpringContextUtils.getBean(PropertyHolder.getProperty("jpa.database"));
        }
        boolean result = backupService.backup();
        //如果備份成功,則將備份檔案發往他處
        if(result){
            backupFileSenderExecuter.send(getNewestBackupFile());
        }
        return result;
    }
    /**
     * 查詢並執行正在使用的資料的恢復實現例項
     * @param date
     * @return 
     */
    @Override
    public boolean restore(String date) {
        if(backupService==null){
            backupService=SpringContextUtils.getBean(PropertyHolder.getProperty("jpa.database"));
        }
        return backupService.restore(date);
    }    
}

    關鍵是這行程式碼backupService=SpringContextUtils.getBean(PropertyHolder.getProperty("jpa.database"));

    2、在記錄備份恢復日誌的時候,如果每種資料庫的實現類都要貼上複製通用的程式碼到備份和恢復方法的開始和結束位置,那麼四處就飄散著重複的程式碼,對易讀性和可修改性都是極大的破壞。

    AOP是解決這個問題的不二之選,為了AOP能工作,良好設計的包結構、類層級,規範的命名都是非常重要的,尤其是這裡的BackupServiceExecuter和真正執行備份恢復的實現類有共同的方法簽名(都實現了BackupService介面),所以把他們放到不同的包裡有利於AOP。

    使用AOP首先要引入依賴:

Xml程式碼 複製程式碼 收藏程式碼
  1. <dependency>
  2.     <groupId>org.aspectj</groupId>
  3.     <artifactId>aspectjrt</artifactId>
  4.     <version>${aspectj.version}</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.aspectj</groupId>
  8.     <artifactId>aspectjweaver</artifactId>
  9.     <version>${aspectj.version}</version>
  10. </dependency>

    其次是要在spring配置檔案中指定啟用自動代理:

Xml程式碼 複製程式碼 收藏程式碼
  1. <aop:aspectj-autoproxy/>

    最後就可以編寫程式碼實現日誌記錄:

Java程式碼 複製程式碼 收藏程式碼
  1. /** 
  2.  * 備份恢復資料庫日誌Aspect 
  3.  * org.apdplat.module.system.service.backup.impl包下面有多個數據庫的備份恢復實現 
  4.  * 他們實現了BackupService介面的backup方法(備份資料庫)和restore(恢復資料庫)方法 
  5.  * @author 楊尚川 
  6.  */
  7. @Aspect
  8. @Service
  9. publicclass BackupLogAspect {  
  10.     privatestaticfinal APDPlatLogger LOG = new APDPlatLogger(BackupLogAspect.class);  
  11.     privatestaticfinalboolean MONITOR_BACKUP = PropertyHolder.getBooleanProperty("monitor.backup");  
  12.     private BackupLog backupLog = null;  
  13.     static{  
  14.         if(MONITOR_BACKUP){              
  15.             LOG.info("啟用備份恢復日誌");  
  16.             LOG.info("Enable backup restore log", Locale.ENGLISH);  
  17.         }else{  
  18.             LOG.info("禁用備份恢復日誌");  
  19.             LOG.info("Disable backup restore log", Locale.ENGLISH);  
  20.         }  
  21.     }  
  22.     //攔截備份資料庫操作    
  23.     @Pointcut("execution( boolean org.apdplat.module.system.service.backup.impl.*.backup() )")  
  24.     publicvoid backup() {}  
  25.     @Before("backup()")  
  26.     publicvoid beforeBackup(JoinPoint jp) {  
  27.         if(MONITOR_BACKUP){  
  28.             before(BackupLogType.BACKUP);  
  29.         }  
  30.     }     
  31.     @AfterReturning(value="backup()", argNames="result", returning = "result")    
  32.     publicvoid afterBackup(JoinPoint jp, boolean result) {  
  33.         if(MONITOR_BACKUP){  
  34.             after(result);  
  35.         }  
  36.     }  
  37.     //攔截恢復資料庫操作    
  38.     @Before(value="execution( boolean org.apdplat.module.system.service.backup.impl.*.restore(java.lang.String) ) && args(date)",  
  39.             argNames="date")  
  40.     publicvoid beforeRestore(JoinPoint jp, String date) {  
  41.         if(MONITOR_BACKUP){  
  42.             before(BackupLogType.RESTORE);  
  43.         }  
  44.     }         
  45.     @AfterReturning(pointcut="execution( boolean org.apdplat.module.system.service.backup.impl.*.restore(java.lang.String) )",   
  46.             returning = "result")    
  47.     publicvoid afterRestore(JoinPoint jp, boolean result) {  
  48.         if(MONITOR_BACKUP){  
  49.             after(result);  
  50.         }  
  51.     }  
  52.     privatevoid before(String type){  
  53.         LOG.info("準備記錄資料庫"+type+"日誌");  
  54.         User user=UserHolder.getCurrentLoginUser();  
  55.         String ip=UserHolder.getCurrentUserLoginIp();  
  56.         backupLog=new BackupLog();  
  57.         if(user != null){  
  58.             backupLog.setUsername(user.getUsername());  
  59.         }  
  60.         backupLog.setLoginIP(ip);  
  61.         try {  
  62.             backupLog.setServerIP(InetAddress.getLocalHost().getHostAddress());  
  63.         } catch (UnknownHostException e) {  
  64.             LOG.error("無法獲取伺服器IP地址", e);  
  65.             LOG.error("Can't get server's ip address", e, Locale.ENGLISH);  
  66.         }  
  67.         backupLog.setAppName(SystemListener.getContextPath());  
  68.         backupLog.setStartTime(new Date());  
  69.         backupLog.setOperatingType(type);  
  70.     }  
  71.     privatevoid after(boolean result){  
  72.         if(result){  
  73.             backupLog.setOperatingResult(BackupLogResult.SUCCESS);  
  74.         }else{  
  75.             backupLog.setOperatingResult(BackupLogResult.FAIL);  
  76.         }  
  77.         backupLog.setEndTime(new Date());  
  78.         backupLog.setProcessTime(backupLog.getEndTime().getTime()-backupLog.getStartTime().getTime());  
  79.         //將日誌加入記憶體緩衝區
  80.         BufferLogCollector.collect(backupLog);  
  81.         LOG.info("記錄完畢");  
  82.     }  
  83. }  
/**
 * 備份恢復資料庫日誌Aspect
 * org.apdplat.module.system.service.backup.impl包下面有多個數據庫的備份恢復實現
 * 他們實現了BackupService介面的backup方法(備份資料庫)和restore(恢復資料庫)方法
 * @author 楊尚川
 */
@Aspect
@Service
public class BackupLogAspect {
    private static final APDPlatLogger LOG = new APDPlatLogger(BackupLogAspect.class);
    private static final boolean MONITOR_BACKUP = PropertyHolder.getBooleanProperty("monitor.backup");
    private BackupLog backupLog = null;
    
    static{
        if(MONITOR_BACKUP){            
            LOG.info("啟用備份恢復日誌");
            LOG.info("Enable backup restore log", Locale.ENGLISH);
        }else{
            LOG.info("禁用備份恢復日誌");
            LOG.info("Disable backup restore log", Locale.ENGLISH);
        }
    }
    
    //攔截備份資料庫操作    
    @Pointcut("execution( boolean org.apdplat.module.system.service.backup.impl.*.backup() )")
    public void backup() {}
    
    @Before("backup()")
    public void beforeBackup(JoinPoint jp) {
        if(MONITOR_BACKUP){
            before(BackupLogType.BACKUP);
        }
    }   
    
    @AfterReturning(value="backup()", argNames="result", returning = "result")  
    public void afterBackup(JoinPoint jp, boolean result) {
        if(MONITOR_BACKUP){
            after(result);
        }
    }
    
    //攔截恢復資料庫操作    
    @Before(value="execution( boolean org.apdplat.module.system.service.backup.impl.*.restore(java.lang.String) ) && args(date)",
            argNames="date")
    public void beforeRestore(JoinPoint jp, String date) {
        if(MONITOR_BACKUP){
            before(BackupLogType.RESTORE);
        }
    }       
    
    @AfterReturning(pointcut="execution( boolean org.apdplat.module.system.service.backup.impl.*.restore(java.lang.String) )", 
            returning = "result")  
    public void afterRestore(JoinPoint jp, boolean result) {
        if(MONITOR_BACKUP){
            after(result);
        }
    }
    
    private void before(String type){
        LOG.info("準備記錄資料庫"+type+"日誌");
        User user=UserHolder.getCurrentLoginUser();
        String ip=UserHolder.getCurrentUserLoginIp();
        backupLog=new BackupLog();
        if(user != null){
            backupLog.setUsername(user.getUsername());
        }
        backupLog.setLoginIP(ip);
        try {
            backupLog.setServerIP(InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException e) {
            LOG.error("無法獲取伺服器IP地址", e);
            LOG.error("Can't get server's ip address", e, Locale.ENGLISH);
        }
        backupLog.setAppName(SystemListener.getContextPath());
        backupLog.setStartTime(new Date());
        backupLog.setOperatingType(type);
    }
    private void after(boolean result){
        if(result){
            backupLog.setOperatingResult(BackupLogResult.SUCCESS);
        }else{
            backupLog.setOperatingResult(BackupLogResult.FAIL);
        }
        backupLog.setEndTime(new Date());
        backupLog.setProcessTime(backupLog.getEndTime().getTime()-backupLog.getStartTime().getTime());
        //將日誌加入記憶體緩衝區
        BufferLogCollector.collect(backupLog);
        LOG.info("記錄完畢");
    }
}

    3、怎麼樣才能異地容錯呢?將備份檔案儲存到與伺服器處於不同地理位置的機器上,最好能多儲存幾份。除了能自動把備份檔案傳輸到異地伺服器上面,使用者也可以從web介面下載。

    APDPlat使用推模型來發送備份檔案,介面如下: