Logic Machine Forum
Full Backup - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Full Backup (/showthread.php?tid=4955)



Full Backup - bednarekluc - 09.09.2023

I just learned the hard way that my backup script doesn't make a backup off everything.
I'm missing the startup script, the user libraries and the common functions.

This is the script that is use to make the daily backup.
Is there a way to backup everything at once?

Code:
--Take a backup and ftp to NAS
-- name of backup file
src = 'backup-' .. os.date('%Y.%m.%d_%H-%M') .. '.tar.gz'
-- where to put backup file on LM
dst = '/home/ftp/' .. src

-- where to put the backup on NAS
target = 'Targetpath' .. src

-- prepare files for backup
os.execute('sh /lib/genohm-scada/web/general/backup.sh')

-- create archive
os.execute('cd /lib/genohm-scada/storage && tar -c -z -f ' .. dst .. ' ./')


-- load the ftp support
local ftp = require("socket.ftp")
local ltn12 = require("ltn12")

-- ftp to NAS
f, e = ftp.put{
  host = "IPadres",
  user = "Username",
  password = "Password",
  type = "i",
  argument = target,
  source = ltn12.source.file(io.open(dst, "rb"))
 
}
if (e) then
  log (e)
  log (f)
  alert("Could not ftp: ", e, "\n")
end
log("ftp_backup")
 



-- cleanup
os.execute('cd /home/ftp && rm -rf backup_*')
os.execute('cd /lib/genohm-scada/storage && rm -rf user userlib.luas blockly.luas initscript.lua helpers.js genohm-scada.config filter*')



RE: Full Backup - admin - 09.09.2023

See this: https://forum.logicmachine.net/showthread.php?tid=4418&pid=28503#pid28503


RE: Full Backup - bednarekluc - 09.09.2023

(09.09.2023, 09:33)admin Wrote: See this: https://forum.logicmachine.net/showthread.php?tid=4418&pid=28503#pid28503

With this I get the backup in a binary string.

But I'm not able to put it to the ftp server.
Do I need to save it on the LM first before I can upload it to the FTP server?


RE: Full Backup - admin - 09.09.2023

Use ltn12.source.string


RE: Full Backup - npinguin - 11.09.2023

Can someone share the new script to make a full backup?


RE: Full Backup - admin - 11.09.2023

Try this, you need 2023 firmware for it to work. Change variables in the first 4 lines as needed.

Code:
host = 'FTP_SERVER_IP'
user = 'username'
pass = 'password'
path = 'backup-' .. 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 upload failed: ' .. tostring(err))
end



RE: Full Backup - manos@dynamitec - 08.02.2024

Hello Admin,

I have two SpaceLynks which used to run on FW 2.7.0 and today I updated one on FW 2.8.3. I have the following script which creates a backup of the system every day and sends it by mail.
Although the script seems to work, when I tried to restore the .tar.gz file on FW 2.8.3 without recieveing an error the system rebooted to an empty state where nothting was restored.
Then I just renamed the destination file from .tar.gz to .zip. This backup file could be properly restored but I noticed that this small change on the script was not saved on the restored project.

Is there a better way to create a full backup of the system and send by mail, that will run properly on FW 2.8.3 or 3.0.0 or any LM running on FW 202306? 

Below you can see the original script without the settings variables.

Code:
--***********************************************************--
--******************** 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.office365.com',
    port = 587,
        secure = 'starttls',
}

--Create attachment
src = 'SubSL_backup-' .. os.date('%Y.%m.%d') .. '.tar.gz'
dst = '/home/ftp/' .. src

-- prepare files for backup
os.execute('sh /lib/genohm-scada/web/general/backup.sh')

-- create archive
os.execute('cd /lib/genohm-scada/storage && tar -c -z -f ' .. dst .. ' ./')

--Create subject
subject = subjectpart1 .. ": " .. src .. " " .. 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 = table.concat(to, ', '),
  subject = subject
},
   
--Load attachment inside body     
body = {
preamble = "",
[1] = { 
      headers = {
         ["content-type"] = 'application/x-7z-compressed',
         ["content-disposition"] = 'attachment; filename="'..src..'"',
         ["content-description"] = '.. src ..',
         ["content-transfer-encoding"] = "BASE64",
      },

      body = ltn12.source.chain(
      ltn12.source.file(io.open(dst, "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 backup email: ", e, "\n")
end
log ('Email Sent')

-- cleanup
os.execute('cd /home/ftp && rm -rf SubSL_*')
os.execute('cd /lib/genohm-scada/storage && rm -rf user userlib.luas blockly.luas initscript.lua helpers.js genohm-scada.config filter*')

Kind regards,


RE: Full Backup - admin - 08.02.2024

Use this script: https://kb.logicmachine.net/scripting/backup-file-email-ftp/#send-via-email


RE: Full Backup - gtsamis - 06.03.2024

(08.02.2024, 11:54)admin Wrote: Use this script: https://kb.logicmachine.net/scripting/backup-file-email-ftp/#send-via-email

It seems that the backup from the script and also from utilities does not contain event, resident and sheduled scripts.
FW: 20240219

Can you please verify and sugest a solution.

BR
George


RE: Full Backup - admin - 07.03.2024

These scripts are stored in the main database.