Community discussions

MikroTik App
Topic Author
Posts: 27
Joined: Tue May 15, 2018 1:27 am

Create directory on remote FTP server

Fri Dec 11, 2020 11:41 pm

I use RouterOS v6.46.8(logn-term).

I write I script to backup locally graph data and then backup it to external FTP server.
I NEED to create automaticaly directory because the name of directory is derived from actual time and date, I WANT to execute a backup of graphs data downloaded from local device.

I use I code like the follow.
# Backup graph data to $LocalSavePath

# Backup to FTP
# Dir all file in sub-directory $LocalSavePath
:foreach i in=[/file find name~("^".$LocalSavePath) ] do={
  :if ([:len [:find [/file get $i type] "file"]]>0) do={
    :local FileName [/file get $i name]

    :local FTPServerSavePath ($FTPServerSavePathRoot.$DateTimeCode."/".[:pick $FileName [:len $LocalSavePath] [:len $FileName]]
    :set Status 7
    :do {
#      :put ("Testpoint : FTP Status=".$Status)
      :do {
        /tool fetch address=$FTPServerAddres port=$FTPServerPort mode=ftp user=$FTPServerUsername password=$FTPServerPassword upload=yes src-path=$FileName dst-path=$FTPServerSavePath
        :set Status -1
       } on-error={
        :set Status ($Status-1)
        :put ("Testpoint : FTP Status=".$Status)
        :delay 10
     } while ($Status>0);
    :if ($Status<0) do={
      :put ("Testpoint : FTP OK")
     } else {
      :put ("Testpoint : FTP Error")

Where for example:
$LocalSavePath = "uSD/Report/"
$FileName = "uSD/Report/graph/cpu/daily.gif"
$FTPServerSavePathRoot = "Router_Report/"
$DateTimeCode = "20201130-1023" (Deriverd from Time & Data at backup moment)
The result is:
FTPServerSavePath = "Router_Report/20201130-1023/graph/cpu/daily.gif"

The folder "20201130-1023" do NOT EXIST, and I CANNOT create it manualy, because the name was created on the fly.
And the fecth now respond failed to copy the file.

If I log-in to the FTP server from a Windows Client with the SAME username and password used in script I can creare folders.
Then the script can make the upload, so NOT is a problem of config, so NOT is a problem of device settings and permissions.

As described in my forum post viewtopic.php?f=9&t=169738
So as fecth create local destination folder if not exists when DOWNLOAD. It CAN create directory path if not exist when UPLOAD a file to a remote server.
Because there in NOT another method in Mikrotik language script to create folder.

I ask to Mikrotik if they can implement this simple thinks.
If similar requests will be from users, then we will check on how to implement such a feature Create a directory on the remote FTP server, in the future.

How many people think this is a good think ?
Best regards.

Who is online

Users browsing this forum: No registered users and 22 guests