As I've written in the article below, there is a quite easy way to connect to and control remote Linux servers with Node.js.
How To Manage Remote Servers And Databases Through SSH Tunnel With Node.js
I've shown examples to run commands on server, and executing queries on remote database using the "node-ssh" package. These kind of remote scripts makes it easy to save time managing massive servers.
This time, I want to show you how to automate file downloading from remote servers.
Collecting and analyzing log files are common tasks for server admins. Log files are often huge, so you have to compress the file, download it to local machine, decompress the file at local, and delete the compressed file from server. It's not a difficult task, but it takes time. And this task comes up repeatedly.
There are nice packages for Node.js to automate these tasks.
"node-ssh" is a package for SSH connecting, and it also have functions to upload and download files through SSH.
https://github.com/steelbrain/node-ssh
"tar" package have functions to compress and decompress files, and it works also on Windows.
https://www.npmjs.com/package/tar
Using these packages, I've wrote a script that...
- Connect with SSH to remote server
- Compress log file with tar command
- Download compressed log file to local machine
- Delete compressed file from server
- Decompress log file on local machine
Here is the script: