Example: E-mail backup file first day of the month from LogicMachine

Task

Create Scheduled script which will automatically create and send via email the backup file of LogicMachine.

Scheduled program

email_backup_scheduled_script

Source code    
  1. --Gmail (smtp) username !IMPORTANT!
  2. user = 'yourgmailaddress'
  3.  
  4. --Gmail (smtp) password !IMPORTANT!
  5. password = 'yourpassword'
  6.  
  7. --Sender for e-mail
  8. from = ''
  9. alias_from = 'Sender Name'
  10.  
  11. --Recipient for e-mail
  12. to = ''
  13. alias_to = 'Recipient name'
  14.  
  15. --Subject for e-mail
  16. subjectpart1 = 'Back-up file'
  17. subjectpart2 = 'automaticlly send by LogicMachine'
  18.  
  19. image_description = 'Backup from LogicMachine'
  20.  
  21. --Message on bottom of email (will only be showed when client don't understand attachment)
  22. epilogue = 'End of message'
  23.  
  24. --***********************************************************--
  25. --******************** End of parameters ********************--
  26. --***********************************************************--
  27. --********** DON'T CHANGE ANYTHING UNDER THIS LINE **********--
  28. --***********************************************************--
  29.  
  30. --Create table to include mail settings
  31. local settings = {
  32. from = from,
  33. rcpt = to,
  34. user = user,
  35. password = password,
  36. server = 'smtp.gmail.com',
  37. port = 465,
  38. secure = 'sslv23',
  39. }
  40.  
  41. --Create attachment
  42. src = '/tmp/lm-backup.tar.gz'
  43. dst = 'LM-' .. os.date('%Y.%m.%d') .. '.tar.gz'
  44. os.execute('sh /lib/genohm-scada/web/general/backup.sh')
  45.  
  46. --Create subject
  47. subject = subjectpart1 .. ": " .. dst .. " " .. subjectpart2
  48.  
  49. --Load required modules to send email with attachment
  50. local smtp = require("socket.smtp")
  51. local mime = require("mime")
  52. local ltn12 = require("ltn12")
  53.  
  54. --Create e-mail header
  55. settings.source = smtp.message{
  56. headers = {
  57. from = '' .. alias_from .. ' ' .. from .. '',
  58. to = '' .. alias_to .. ' ' .. to .. '',
  59. subject = subject
  60. },
  61.  
  62. --Load attachment inside body
  63. body = {
  64. preamble = "",
  65. [1] = {
  66. headers = {
  67. ["content-type"] = 'application/x-7z-compressed',
  68. ["content-disposition"] = 'attachment; filename="'..dst..'"',
  69. ["content-description"] = '.. dst ..',
  70. ["content-transfer-encoding"] = "BASE64",
  71. },
  72.  
  73. body = ltn12.source.chain(
  74. ltn12.source.file(io.open(src, "rb")),
  75. ltn12.filter.chain(
  76. mime.encode("base64"),
  77. mime.wrap()
  78. )
  79. )
  80. },
  81. epilogue = epilogue
  82. }
  83. }
  84.  
  85. --Send the email
  86. r, e = smtp.send(settings)
  87.  
  88. --Create alert when sending gives an error with error message
  89. if (e) then
  90. log (e)
  91. log (r)
  92. alert("Could not send email: ", e, "\n")
  93. end
  94.  
  95. --Delete created backup file from tmp folder inside LM
  96. os.remove(src)

Created by Erwin van der Zwart from Schneider Electric