基本信息
源码名称:用gradle实现备份
源码大小:4.78KB
文件格式:.gradle
开发语言:Groovy
更新时间:2015-08-18
   源码介绍

用gradle实现备份


import java.text.DateFormat
import java.text.SimpleDateFormat

def getDate() {
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    return df.format(new Date());
}
def atlassianFromDir = file('D:/atlassian-jira-6.3.6-standalone')
def jirahomeFromDir = file('D:/jira_home')
def atlassianToDir = file('e:/atlassian-jira-6.3.6-standalone_Bak')
def jirahomeToDir = file('e:/jira_home_Bak')
def logfile = file('run.log')

GradleLog logger = new GradleLog(true)

task initDir{
	logger.info("---初始化备份目录")
	delete(atlassianToDir)
	atlassianToDir.mkdirs()
	delete(jirahomeToDir)
	jirahomeToDir.mkdirs()
}

task copyAtlassian(type: Copy, dependsOn: initDir) {
	logger.info('---复制JIRA目录---')
	from atlassianFromDir
	into atlassianToDir
	exclude { details -> details.file.name.endsWith('.tmp') ||
                         details.file.name.endsWith('.log') }
}
task zipAtlassian(type: Zip, dependsOn: copyAtlassian) {
	logger.info('---压缩JIRA目录---')
	from atlassianToDir
	baseName = 'atlassian' getDate()
}
task copyAtlassianZip(type: Copy, dependsOn: zipAtlassian) {
	logger.info('---备份JIRA目录Zip---')
	from 'atlassian' getDate() '.zip'
	into 'y:/'
}

task copyJirahome(type: Copy, dependsOn: copyAtlassianZip) {
	logger.info('---复制JIRAHOME目录---')
	from jirahomeFromDir
	into jirahomeToDir
	exclude { details -> details.file.name.endsWith('.lock') ||
                         details.file.name.endsWith('.log') }
}
task zipJirahome(type: Zip, dependsOn: copyJirahome) {
	logger.info('---压缩JIRAHOME目录---')
	from jirahomeToDir
	baseName = 'jirahome' getDate()
}
task copyJirahomeZip(type: Copy, dependsOn: zipJirahome) {
	logger.info('---备份JIRAHOME目录Zip---')
	from 'jirahome' getDate() '.zip'
	into 'y:/'
}

task mysqldump(type:Exec, dependsOn: copyJirahomeZip) {
	logger.info('---导出JIRA数据库---')
	commandLine 'cmd', '/c', 'D:/MySQL/bin/mysqldump -uroot -phello jiradb > jiradb.sql'
}
task zipJiradb(type: Zip, dependsOn: mysqldump) {
	logger.info('---压缩JIRA数据库Dump文件---')
	from 'jiradb.sql'
	baseName = 'jiradb' getDate()
}
task copyJiradbZip(type: Copy, dependsOn: zipJiradb) {
	logger.info('---备份JIRA数据库Zip---')
	from 'jiradb' getDate() '.zip'
	into 'y:/'
}

task sendMail(type: JavaExec) {
	def mailhost = 'smtp.qq.com'
	def user = '694240994'
	def password = 'rinor123456'
	def from = '694240994@qq.com'
	def to = 'edp045@sohu.com,405257595@qq.com'
	def subject = "JIRA备份"
	def argsList = " -M ${mailhost} -U ${user} -P ${password} -o ${from} -s ${subject} -a run.log ${to}"
    main = 'com.eagle.common.sendmail'
    classpath = files('javamailtool.jar')
    classpath files('lib/mail.jar')
    args = argsList.split().toList()
}


task backup{
	def subject = "JIRA备份"
	try{
		dependsOn copyAtlassianZip
		dependsOn copyJirahomeZip
		dependsOn copyJiradbZip
		logger.info('---备份完成---')
	}catch(ex){
		subject = "JIRA备份失败"
		logger.info(ex.getMessage())
	}finally{
		def mailhost = 'smtp.qq.com'
		def user = '694240994'
		def password = 'rinor123456'
		def from = '694240994@qq.com'
		def to = '405257595@qq.com,yyqu@hugetech.com.cn,lliu@hugetech.com.cn'
		def argsList = " -M ${mailhost} -U ${user} -P ${password} -o ${from} -s ${subject} -a run.log ${to}"
		javaexec{
		    main = 'com.eagle.common.sendmail'
		    classpath = files('javamailtool.jar')
		    classpath files('lib/mail.jar')
		    args = argsList.split().toList()
		}
	}
}

import java.util.List;
import java.util.ArrayList;
public class GradleLog {
 private List messages = new ArrayList();
 def logFile = "run.log" //定义日志文件名
 private boolean enable = false; //定义一个日志开关
 public GradleLog(boolean enable){
 this.enable = enable
 }
 public boolean isEnable() {
 return enable;
 }
 public List getMessages() {
 return messages;
 }
 public void info(String message){
 getMessages().add(message)
 println message//打印当前消息
 output(message) //输出消息
 }
 public void error(String message){
 getMessages().add(message)
 output(message)
 output("Build Failed")
 throw new GradleException(message) //Error级别默认抛异常
 }
 private void output(String message){
   if(isEnable()){ //开关开启状态才会输出日志
     def file = new File(logFile)
     if(!file.exists()){
       file.createNewFile(); //日志文件如果不存在,则新建一个
     }
     def ln = System.getProperty('line.separator') //获取当前系统分隔符
     file << getDateTime() message ln //将消息追加到日志文件中去,并追加一个换行分隔符
   }
 }
 private String getDateTime() {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS ");
    return df.format(new Date());
 }
}