Send backup via email or FTP

Send LogicMachine backup file via email or FTP every first day of the month #

Send via email #

Create a scheduled script that runs every month.

email_backup img_01

If using GMail you need to enable less secure apps inside your account:
https://www.google.com/settings/security/lesssecureapps

The extension of the attachment will be .pdf otherwise it might be blocked. After downloading backup.pdf you need to change the file extension to backup.zip. Then you can upload it to your LM.

-- SMTP username !IMPORTANT!
user = 'yourgmailaddress'

-- SMTP password !IMPORTANT!
password = 'yourpassword'

--Sender
from = 'user1@example.com'
alias_from = 'Sender Name'

--Recipient
to = 'user2@example.com'
alias_to = 'Recipient name'

--Subject
subject = 'LM backup'

--***********************************************************--
--******************** End of parameters ********************--
--***********************************************************--
--********** DON'T CHANGE ANYTHING UNDER THIS LINE **********--
--***********************************************************--

-- Create table to include mail settings
settings = {
  from = from,
  rcpt = to,
  user = user,
  password = password,
  server = 'smtp.gmail.com',
  port = 465,
  secure = 'tlsv12',
}

-- Create attachment
dst = 'LM-' .. os.date('%Y-%m-%d') .. '.zip'

-- Create subject
subject = subject .. ': ' .. dst

-- Load required modules to send email with attachment
smtp = require('socket.smtp')
mime = require('mime')
ltn12 = require('ltn12')

webrequest = require('webrequest')
data = webrequest('general', 'backup')

-- Create email header
settings.source = smtp.message({
  headers = {
    from = alias_from .. ' <' .. from .. '>',
    to = alias_to .. ' <' .. to .. '>',
    subject = subject
  },
  body = {
    preamble = '',
    [1] = {
      headers = {
         ['content-type'] = 'application/pdf',
         ['content-disposition'] = 'attachment; filename="backup.pdf"',
         ['content-transfer-encoding'] = 'BASE64'
      },
      body = ltn12.source.chain(
        ltn12.source.string(data),
        ltn12.filter.chain(mime.encode('base64'), mime.wrap())
      )
    },
    epilogue = 'End of message'
  }
})

-- Send the email
res, err = smtp.send(settings)

-- Create alert when sending gives an error with error message
if not res then
  log('SMTP error', err)
end

Created by Erwin van der Zwart from Schneider Electric (modified by EMBS)

Send via FTP #

Modify the host, user and pass variables as needed.

host = '192.168.0.9'
user = 'ftp'
pass = 'Ftp12345'

path = 'backups/LM-' .. os.date('%Y-%m-%d') .. '.zip'

ftp = require('socket.ftp')
ltn12 = require('ltn12')
webrequest = require('webrequest')
data = webrequest('general', 'backup')

res, err = ftp.put({
  host = host,
  user = user,
  password = pass,
  argument = path,
  source = ltn12.source.string(data)
})

if not res then
  log('FTP error', err)
end