there is a set of удаленных hosts on which the rsync-сервер is spinning. It accepts and adds file folders to a specific directory.

such files and folders are sent from the server to the nodejs using the rsync client.

The question is: is it possible for the nodejs server to ask the hash sum of a specific directory for the rsync server on the remote host?

purpose It is necessary to implement the monitoring of business versions as well as system solutions, essentially an update system with feedback. Just in the information system, the transfer of the necessary files to the remote servers of the company using rsync was already implemented.

the hash sum is needed to compare with the hash sum recorded in the update system service database, thus to find out if the correct version of this or that 1c report, for example, in the branch.

upd: Can you advise another utility that would cope with a similar task if you ask it? (there is always an option to write your own, but I would like to leave great for later)

upd: And if you try to give, to a remote rsync server, a file, and if it has one, will it not return something like "I already have it to her, no transfer is necessary"? This would essentially be a solution to the problem, since most likely rsync compares by hash sums, and in this case it would be possible to understand that everything is OK, and if not, then we, firstly, know about it, and secondly, immediately we transfer the correct files.

enter image description here

Currently, the nodejs server sends files using the rsync package; it looks like this:

 var Rsync = require('rsync'); // Build the command var rsync = new Rsync() .shell('ssh') .flags('az') .source('/path/to/source') .destination('server:/path/to/destination'); // Execute the command rsync.execute(function(error, code, cmd) { // we're done }); 
  • and for what purpose do you need hash? I'm wondering for the reason that it looks like there is a “ hammer problem - aleksandr barakin
  • hash needed to сравнить with the hash recorded in the update system service бд , thus finding out верная ли версия this or that 1c report, for example, в филиале . - Vyacheslav Danshin
  • Unfortunately, it is not clear from the question what is stored and what programs you are requesting information from. therefore, just guessing: somewhere far away (and in different places) files are stored. they are not changed or renamed during storage. you need to identify these files. the least expensive is to store unique information (the same hash sum) in file names. at least it doesn’t require repeated hash sum calculations. - aleksandr barakin
  • one
    If files (for some esoteric reasons) cannot be renamed, then next to them you can store a file with hash sums (by line for each stored file: file path, space, hash sum). then you 1) do not need time to calculate the hash sum each time you need to identify the file and 2) implement the rsync program with new functionality, which is set out in the question header. - aleksandr barakin Nov.
  • but in this case I can not guarantee that the files have not changed? - Vyacheslav Danshin

3 answers 3

The directory hash sum cannot be asked, but you can ask what files have changed without performing the actual copy operation. You only need to specify the dry-run flag:

 rsync.set('dry-run'); // оно же rsync.dry(); 

Then from the output of the command to get a list of changed files. If you obviously did not prohibit this, then the comparison will occur exactly as you want, that is , using checksums .

You can get only the list of files without anything by specifying the out-format option, for example, in this form:

 out-format="%f" 
  • Yes, you can. If you look within the framework of the question, your answer is the most accurate. Although in a real project I went the way that was advised earlier (the option to keep the nodejs server on each remote server is more flexible). Therefore, I give him a +1. - Vyacheslav Danshin

is it possible from the nodejs server to ask a hash of a specific directory for an rsync server on a remote host?

No, that is not possible. you can only make a hash yourself.

The first option: hash-files

Installation

 npm install hash-files 

Using

 var hashFiles = require('hash-files'); // options is optional hashFiles(options, function(error, hash) { // hash will be a string if no error occurred }); 

or on the command line:

 $ ./bin/hash-files -f '["package.json"]' -a sha256 a29089cc5e3f8bf6ae15ea6b9cd5eaefb14bbb12e3baa2c56ee5c21422250c75 

The second option: nodejs itself

crypto.createHash (algorithm)

 const filename = process.argv[2]; const crypto = require('crypto'); const fs = require('fs'); const hash = crypto.createHash('sha256'); const input = fs.createReadStream(filename); input.on('readable', () => { var data = input.read(); if (data) hash.update(data); else { console.log(`${hash.digest('hex')} ${filename}`); } }); 
  • that's right, so i will get the hash before sending to the remote host. The rsync server on the remote host accepts and writes to its working directory. Of course, you can deploy to remote hosts on the node-серверу and listen to your api request for removing the hash. In this case, your decision is possible. Or did you not mean it? - Vyacheslav Danshin
  • Yes. So. I can not imagine how else to do. in the sense of how you can use nodejs to send a request to the rsync service on a remote machine - spectre_it
  • Well, for example, using npmjs.com/package/rsync - Vyacheslav Danshin
  • one
    deploying a nodejs server on each remote host seems like a sensible idea, after all, who knows what else I need to ask remote branch machines, and the node can be taught. - Vyacheslav Danshin

As far as I know, no rsync server / client implementation supports the feature you require. other tasks they solve.

but judging by the code given by you, the ssh protocol is used to communicate with the servers. Consequently, you can simply run the program for calculating the checksums of the necessary files directly on the server

 md5sum /путь/к/файлу1 /путь/к/файлу2 /путь/к/каталогу/* 

and analyze the answer:

 хэш-сумма /путь/к/файлу1 хэш-сумма /путь/к/файлу2 хэш-сумма /путь/к/каталогу/файл3 хэш-сумма /путь/к/каталогу/файл4 ... 

The md5sum program is used to calculate the checksum using the md5 algorithm, but the gnu / coreutils package includes many other programs that work with other crypto algorithms.


there are more than a lot of ssh client implementations for node.js : one , two , three , etc.