Skip to content
Advertisement

How to make rsync read SRC from STDIN?

I want to dump my MySQL database and make daily backups with rsync.

First approach I came up with is something like mysqldump -ufoo -pbar baz > /var/tmp/baz.sql && rsync /var/tmp/baz.sql /backup/ && rm /var/tmp/baz.sql.

Then I started to wonder if it is possible not to use the temporary file /var/tmp/baz.sql, but instead to pipe the output of mysqldump directly to rsync.

To be more specific, what I want is quite similar to a command line which we use to update the GPG key for apt in Ubuntu: gpg --export --armor CE49EC21 | sudo apt-key add -, where the receiver of the pipe supports this ‘-‘ argument indicating it’ll read from stdin. I suppose rsync doesn’t have a similar argument. But I wanna know if there is a workaround.

Advertisement

Answer

This is right, it doesn’t work this way. It is because rsync is made to transfer complete file trees from A to B.

Because of the way rsync works, it cannot work, because rsync calculates several checksums before choosing to transfer a particular file (or parts of it), and doing so in only 2 iterations (ping-pong-steps).

That means a file has to be read several times. That would not work with a (potentially large) SQL dump because it would have to be buffered somehow. And this buffering is up to the user.

Actually storing the file should be the best workaround, especially if it is a file which only gets gradual differences.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement