Rsync from remote server to local laptop

As described in the topic, I need to rsync a directory from the server to a local laptop.
This is the command. It is not working. What is wrong?

sudo rsync -avzh -p <ssh port> root@<server IP>:/var/lib/mysql/<directory>/ /home/

First question; do I need to go over the ssh port?
ssh is setup such that a certificate required, that certificate is stored on the laptop.
ssh root login is disabled.

Thanks for your help.

Kind regards, Woflgang

Lets start with rsync option -p that is an alias for --perms and included in option -a. That is not for setting the ssh port.

One could use option -e, --rsh (see man rsync), but I’d recommend storing an alias into ssh config.

You do use sudo, i.e. the ‘root’ account of the laptop does run the rsync. Therefore, the root has to have access to the alias and certificate. One could add to /root/.ssh/config

Host cpmysql
  HostName <server IP>
  Port <ssh port>
  User root

And then one could run:

sudo rsync -avzh cpmysql:/var/lib/mysql/<directory>/ /home/

(but I don’t know how to specify the certifcate)

But is it really necessary to save the data in the laptop as ‘root’?

That command would copy the content of /var/lib/mysql/<directory>/ to /home/, which seems bit odd, since the /home/ in laptop probably contains your regular account’s directory and whatnot.
IMHO, it would be more clear to have a dedicated directory, e.g.:

sudo rsync -avzh cpmysql:/var/lib/mysql/<directory>/ /home/cpmysql/

If you create the /home/cpmysql and grant your regular account permission to write to it, then you don’t need to use ‘sudo’ (and the alias would be in your ~/.ssh/config)

You say that nobody can ssh root@<server IP>. Therefore, the rsync cannot do that either.
You have to either allow the root to login into the server, or not use the root account for the connection.

What is it that you are actually copying? A backup of database? Is it a “mysqldump” from the server?
(One should never copy files of mysql while the SQL server is running.)

You could create a tunnel to server:

ssh -L <sql port>:localhost:<sql port> cpmysql

You can run the mysqldump in the laptop, while that tunnel is open.

First, check that ssh is working as standard user from laptop to server.

Second, try to rsync as standard user, you can create a service account if needed.
On the server, you might need to add the account to the ‘mysql’ group to be allowed to read.

On the server check perms
ls -l /var/lib/mysql

On my own systems it’s a lot easier because I don’t use ‘/var/lib’ for mysql data.

Copying mysql binary data to a laptop is horrible anyway, it’s better to create a dump on the server, and then just copy the dump to the laptop.

1 Like