Posted By

octaviannitagmailcom on 06/02/13


Tagged

email backup groovy


Versions (?)

Back Up A Directory To Your Email


 / Published in: Groovy
 

Including a nice utility to detect the code base of a class...

  1. #!/usr/bin/env groovy
  2.  
  3. import javax.activation.*
  4. import javax.mail.*
  5. import javax.mail.internet.*
  6.  
  7. @Grapes ([
  8. @Grab (group = 'javax.activation', module = 'activation', version = '1.1.1'),
  9. @Grab (group = 'javax.mail', module = 'mail', version = '1.4.5')
  10. ])
  11.  
  12. def dirToBackup = location(getClass()).absolutePath
  13. def archiveName = dirToBackup + '.zip'
  14.  
  15. def host = 'smtp.googlemail.com'
  16. def port = '465'
  17. def user = '[email protected]'
  18. def pass = '*****'
  19.  
  20. def props = new Properties() ;
  21. props.put('mail.smtp.host', host)
  22. props.put('mail.smtp.user', user)
  23. props.put('mail.smtp.port', port)
  24. props.put('mail.smtp.auth', 'true')
  25. props.put('mail.smtp.starttls.enable','true')
  26.  
  27. def transport = null
  28. try {
  29. def ant = new AntBuilder()
  30.  
  31. // Delete the archive:
  32. ant.delete(file: archiveName)
  33.  
  34. // Archive the directory to backup:
  35. ant.zip(destfile: archiveName, basedir: dirToBackup, excludes: '**/*.bat')
  36.  
  37. // Email (using Google) the newly created archive:
  38. def session = Session.getInstance(props, null)
  39. def message = new MimeMessage(session)
  40.  
  41. message.subject = '[bkp] ' + location(getClass()).absolutePath
  42. message.from = new InternetAddress(user)
  43. message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(user))
  44.  
  45. multipart = new MimeMultipart()
  46.  
  47. messageBodyPart = new MimeBodyPart()
  48. messageBodyPart.text = 'Backup for ' + archiveName
  49. multipart.addBodyPart(messageBodyPart)
  50.  
  51. messageBodyPart = new MimeBodyPart()
  52. messageBodyPart.dataHandler = new DataHandler(new FileDataSource(archiveName))
  53. messageBodyPart.fileName = 'attachment.zip'
  54. multipart.addBodyPart(messageBodyPart)
  55.  
  56. message.content = multipart
  57.  
  58. transport = session.getTransport('smtps');
  59.  
  60. println(' [***] Connecting to Gmail...')
  61. transport.connect(host, port.toInteger(), user, pass)
  62.  
  63. println(' [***] Sending message...')
  64. transport.sendMessage(message, message.allRecipients)
  65. println(' [***] Message sent!')
  66. } catch (e) {
  67. e.printStackTrace()
  68. if (transport != null) {
  69. println(' [***] Closing Gmail connection...')
  70. try {
  71. transport.close()
  72. } catch (e) {
  73. e.printStackTrace()
  74. }
  75. }
  76. }
  77. println(' [***] Done.')
  78.  
  79. // --- Helpers
  80.  
  81. def location(klass) {
  82. if (klass == null) {
  83. }
  84.  
  85. def codeBase = null
  86. def codeSource = null
  87. def protectionDomain = klass.protectionDomain
  88. if (protectionDomain != null && (codeSource = protectionDomain.codeSource) != null) {
  89. codeBase = codeSource.location.toString()
  90. } else {
  91. def classLoader = klass.classLoader
  92. if (classLoader == null) {
  93. }
  94.  
  95. def classRelativePath = klass.name.replace('.', '/') + '.class'
  96. def classURL = classLoader.getResource(classRelativePath)
  97. if (classURL == null) {
  98. }
  99.  
  100. codeBase = classURL.toString()
  101. if ('jar'.equalsIgnoreCase(classURL.protocol)) {
  102. codeBase = codeBase.substring(4).replace('!/' + classRelativePath, '')
  103. } else {
  104. codeBase = codeBase.replace(classRelativePath, '')
  105. }
  106. }
  107.  
  108. if (codeBase.startsWith('file:')) {
  109. codeBase = codeBase.substring(5)
  110. }
  111.  
  112. def codeBaseFile = new File(codeBase)
  113. if (!codeBaseFile.exists()) {
  114. }
  115. if (codeBaseFile.isFile()) {
  116. return codeBaseFile.parentFile.absoluteFile // assume classFile is actually a .jar file
  117. }
  118. codeBaseFile.absoluteFile
  119. }

Report this snippet  

You need to login to post a comment.