E-mail backup file once a month from LM
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
- --Gmail (smtp) username !IMPORTANT!
- user = 'yourgmailaddress'
-
- --Gmail (smtp) password !IMPORTANT!
- password = 'yourpassword'
-
- --Sender for e-mail
- from = ''
- alias_from = 'Sender Name'
-
- --Recipient for e-mail
- to = ''
- alias_to = 'Recipient name'
-
- --Subject for e-mail
- subjectpart1 = 'Back-up file'
- subjectpart2 = 'automaticlly send by LogicMachine'
-
- image_description = 'Backup from LogicMachine'
-
- --Message on bottom of email (will only be showed when client don't understand attachment)
- epilogue = 'End of message'
-
- --***********************************************************--
- --******************** End of parameters ********************--
- --***********************************************************--
- --********** DON'T CHANGE ANYTHING UNDER THIS LINE **********--
- --***********************************************************--
-
- --Create table to include mail settings
- local settings = {
- from = from,
- rcpt = to,
- user = user,
- password = password,
- server = 'smtp.gmail.com',
- port = 465,
- secure = 'sslv23',
- }
-
- --Create attachment
- src = '/tmp/lm-backup.tar.gz'
- dst = 'LM-' .. os.date('%Y.%m.%d') .. '.tar.gz'
- os.execute('sh /lib/genohm-scada/web/general/backup.sh')
-
- --Create subject
- subject = subjectpart1 .. ": " .. dst .. " " .. subjectpart2
-
- --Load required modules to send email with attachment
- local smtp = require("socket.smtp")
- local mime = require("mime")
- local ltn12 = require("ltn12")
-
- --Create e-mail header
- settings.source = smtp.message{
- headers = {
- from = '' .. alias_from .. ' ' .. from .. '',
- to = '' .. alias_to .. ' ' .. to .. '',
- subject = subject
- },
-
- --Load attachment inside body
- body = {
- preamble = "",
- [1] = {
- headers = {
- ["content-type"] = 'application/x-7z-compressed',
- ["content-disposition"] = 'attachment; filename="'..dst..'"',
- ["content-description"] = '.. dst ..',
- ["content-transfer-encoding"] = "BASE64",
- },
-
- body = ltn12.source.chain(
- ltn12.source.file(io.open(src, "rb")),
- ltn12.filter.chain(
- mime.encode("base64"),
- mime.wrap()
- )
- )
- },
- epilogue = epilogue
- }
- }
-
- --Send the email
- r, e = smtp.send(settings)
-
- --Create alert when sending gives an error with error message
- if (e) then
- log (e)
- log (r)
- alert("Could not send email: ", e, "\n")
- end
-
- --Delete created backup file from tmp folder inside LM
- os.remove(src)
Created by Erwin van der Zwart from Schneider Electric