Copy directory to an ftp server using nodejs

Asked

Viewed 225 times

3

I am developing an application in nodejs where I need to access an ftp server and send some folders there. Before putting these folders, I want to backup the folders on the server. How can I copy a directory inside the ftp server? The idea is to pass the folder 'servor_ftp/pasta1' to 'servor_ftp/backup/pasta1'

Do I need to download the folder first and then upload it to the server? I am currently using the ssh2-sftp-client module.

1 answer

0

I think the simplest solution would be to use the methods downloadDir() and upload() ssh2-sftp-client itself to download the folder and upload to a new folder, which may be on another server.

Below is an example if the two folders are on the same server, if they are two different server folders you will have to make two connections with sftp.connect()

    const sftp = require('ssh-sftp-client');
    const config = {
       host: {
         host: host
         username: username
         password: password,
       }
       folder: folder          // pasta no servidor a ser baixada
       local: local            // pasta no projeto
       bkp: bkp                // pasta de backup
    }
    await sftp.connect(config.host);
    await sftp.downloadDir(config.folder, config.local);
    await sftp.uploadDir(config.local, config.bkp);
    await sftp.end();

You can also use libraries to compress the directory, such as in this example

Using ssh2-sftp-client, I did a project similar to yours, but in my case received a folder with . xls files from an A server to turn into . csv and send to a B server.

Project link

  • Welcome to Sopt. Try to add the examples to the reply, leaving the links only for additional information.

  • I updated the answer, I think the example would solve the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.