阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Java实现定时备份文件教程

34次阅读
没有评论

共计 2340 个字符,预计需要花费 6 分钟才能阅读完成。

导读 这篇文章主要为大家详细介绍了 Java 实现定时备份文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了 Java 如何定时备份文件的具体实现代码,供大家参考,具体内容如下

程序思路:

1. 空目录不备份,但非空目录都备份

2. 源目录 source 要递归他下面所有的文件和目录 存入 List

3. 循环这个 list,创建每个文件的目录

4. 开始复制

以下代码实现了定时备份路径为 e:\\a 的文件,每 30 秒进行一次备份,时间可修改。

public class Test12 {public static void main(String[] args) throws InterruptedException {Timer t = new Timer();
        t.scheduleAtFixedRate(new MyTask(),new Date(),1000*30);
 
        for(int i = 0;i<10000;i++){Thread.sleep(1000);
            System.out.println("warning");
        }
 
 
    }
 
}
 
class MyTask extends TimerTask{
 
    static final String SOURCE = "e:\\a";
    static String DEST;
 
    @Override
    public void run() {Date d = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        DEST = "e:\\dest_" + df.format(d);
        File file = new File(SOURCE);
        File dest = new File(DEST);
 
        if(dest.exists()){deleteAll(dest);
        }
        System.out.println("创建备份目录?"+dest.mkdirs());
        List list = new ArrayList();
 
        getAllFile(file,list);
 
        backUp(list,dest);
    }
 
    // 备份
    private static void backUp(List list, File dest) {if (list == null || list.size()  list) {if (source.isDirectory()) {// 查看子目录 listFile()
            File[] fs = source.listFiles();
            if (fs != null && fs.length > 0) {
                // 说明有子目录或子文件
                for (File ff : fs) {getAllFile(ff, list);
                }
            }
 
        }
        list.add(source);
 
    }
 
    // 递归删除
    private static void deleteAll(File f) {if (f.isDirectory()) {File[] fs = f.listFiles();
            if (fs != null && fs.length > 0) {for (File file : fs) {deleteAll(file);
                }
            }
        }
        System.out.println(f + "删除成功?" + f.delete());
    }
 
}

再为大家补充一段:

import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.IOException;
import java.io.PrintStream;
  
public class Backup 
{public static void main(String[] args) 
 {Runtime runtime = Runtime.getRuntime();
  Calendar calendar = Calendar.getInstance();
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
  String currentTime = dateFormat.format(calendar.getTime());
  Process p = null;
  PrintStream print = null;
  StringBuilder buf = new StringBuilder();
  for(String a : args){buf.append(a);
   buf.append(" ");
  }
  String databases = buf.toString();
   
  try{p = runtime.exec("cmd /c mysqldump -uroot -p1234 -B"+databases+">"+currentTime+".sql.bak");
  }catch (IOException e){if( p != null){p.destroy();
   }
   try{print = new PrintStream(currentTime+"_backup_err.log");
    dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
    currentTime = dateFormat.format(calendar.getTime());
    print.println(currentTime+"backup failed.");
    e.printStackTrace(print);
    print.flush();}catch (IOException e2){ }finally{if(print!=null){print.close();
    }
   }
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计2340字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中