This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

FTPS example
#1
Hello

I am trying to secure ftp a backup file to a nas

Does anyone have an example?

I use currently the folowing code wich fails with error : * string: 504 TLS/SSL protection required.
Code:
    -- ftp to NAS
 log("System Backup: transfering backup to NAS, host:" .. FTP_TARGET_IP .. ", target:" .. target)
 res, err = socket.ftp.put({
     host = FTP_TARGET_IP,
     user = FTP_TARGET_LOGIN,
     password = FTP_TARGET_PW,
     type = "i",
     argument = target,
     source = ltn12.source.file(io.open(dst, "rb")) })
 
    if (err) then
   log(err)
   log(res)
   alert("System Backup: failed, could not ftp to NAS, host:" .. FTP_TARGET_IP .. ", target:" .. target)
    end

Thanks
Nicky
Reply
#2
Hi,

Try this:
Code:
require('socket.ftp')

-- Get hostname
hostname = io.readfile('/proc/sys/kernel/hostname'):trim()

-- ftp file
ftpfile = string.format('ftp://user:password@192.168.10.1/backup-' .. hostname .. '-' .. os.date('%Y.%m.%d') .. '.tar.gz')

--Create attachment
src = '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 cleanup archive
os.execute('cd /lib/genohm-scada/storage && tar -c -z -f ' .. dst .. ' ./')

-- upload to ftp
result, err = socket.ftp.put(ftpfile, io.readfile(dst))

-- error while uploading
if err then
 log('FTP upload error: %s', tostring(err))
else
 log('file uploaded to external ftp')
end

--Delete created backup file from tmp folder inside HL
os.execute('cd /lib/genohm-scada/storage && rm -rf user userlib.luas blockly.luas initscript.lua helpers.js genohm-scada.config filter*')

--Delete temp file from HL ftp
os.remove(dst)

BR,

Erwin
Reply
#3
Thank Erwin
But that looks to be a regular FTP, i need sftp?
Thanks
Nicky
Reply
#4
Which LM version do you have?
Reply
#5
(21.09.2017, 14:24)admin Wrote: Which LM version do you have?
Reactor v2 running latest firmware
Reply
#6
Update this package through system config > packages:
https://dl.openrb.com/lm-17/pkg/luasocke...20_mxs.ipk

Then add secure = true to your ftp.put config table. If you get an error that data connection must be encrypted try adding datasecure = true as well. This might not work for all FTP servers and it depends on how the server is configured.
Reply
#7
Sad 
Thanks Admin

Close but not yet there

I updated the package and added the option secure = true
on the synology nas: empty file is created 
on the logic machine I have an error:"closed"

I added the option secure = true and datasecure = true
on the synology nas: no file is created
on the logic machine I have an error:"503 Use AUTH and PBSZ commands first"
Reply
#8
Ok, can you enable remote diagnostics on your LM and forward port 22, then send me access via PM?
Reply
#9
Thanks for access providing, please install updated package and try again:
https://dl.openrb.com/lm-17/pkg/luasocke...21_mxs.ipk
Reply
#10
(25.09.2017, 11:29)admin Wrote: Thanks for access providing, please install updated package and try again:
https://dl.openrb.com/lm-17/pkg/luasocke...21_mxs.ipk

Thanks a lot, Works perfectly ! 

Are you going to include this in future firmwares, or will it be a seperate install?

Nicky
Reply
#11
Yes, it will be included in the future fw releases.
Reply
#12
Hi!

Is there any update about this?

Can I use sftp in Logic Machine to make a backup in a NAS? Those links are broken

Thanks
Reply
#13
Updated packages are already included in the latest firmware. No need to install anything manually.
SFTP (file transfer over SSH) is not supported yet. This topic is about FTPS which is a secure version of the FTP protocol. Your NAS should support it.
Reply
#14
I would like to point out that the script is used in many web servers, both Schneider and LM, and has always worked and still works every day.
when I try to upload the backup to ftp, the upload happens, but the backup has size 0 and if I try to open it it gives me an error. Furthermore, it is logged in the error log
os.remove call blocked. What is going on?
Code:
require('socket.ftp')

-- Get hostname
hostname = io.readfile('/proc/sys/kernel/hostname'):trim()

-- ftp file
ftpfile = string.format('ftp://user:pass@ip/backup/' .. os.date('%Y.%m.%w') .. '-' .. hostname .. '.tar.gz;type=i')

--Create attachment
src = '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 cleanup archive
os.execute('cd /lib/genohm-scada/storage && tar -c -z -f ' .. dst .. ' ./')

-- upload to ftp
result, err = socket.ftp.put(ftpfile, io.readfile(dst))

-- error while uploading
if err then
  log('FTP upload error: %s', tostring(err))
else
  log('file uploaded to external ftp')
end

--Delete created backup file from tmp folder inside HL
os.execute('cd /lib/genohm-scada/storage && rm -rf user userlib.luas blockly.luas initscript.lua helpers.js genohm-scada.config filter*')

--Delete file from HL ftp
os.remove(dst)
Reply
#15
Which firmware do you use?
------------------------------
Ctrl+F5
Reply
#16
Erwin's code is outdated. This script should be used instead: https://kb.logicmachine.net/scripting/ba...email-ftp/
Reply
#17
now it gives me

io.readproc call blocked
os.tmpname call blocked

the problem is on a wiser with firmware 3.0.0

the file si uploaded with dimension zero kylobyte

Code:
host = 'ip'
user = 'user'
pass = 'pass'

path = 'backup/webserver-' .. 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
Reply
#18
Block unsafe functions in scripts: make sure this is disabled
------------------------------
Ctrl+F5
Reply
#19
(13.03.2024, 11:29)Daniel Wrote: Block unsafe functions in scripts: make sure this is disabled

Thanks,
the problem partially solved, file loaded but corrupt

   
Reply
#20
I just tried it on SL 3.0.0 and it works just fine. Use 7zip for editing.
------------------------------
Ctrl+F5
Reply


Forum Jump: