I want to be able to copy a mysqdump from a remote server to my local machine.
I had a script to do this, but our operations guys have now changed the server permissions and it doesn't work.
So before I could SSH from my machine login as root and supply a password and connect to mysql, so I had a command in a Perl script like so :
sshpass -p root_password ssh root@$servername mysqldump $database_name > $mysqldump_location
Anyway, permissions have changed and I can't login using root from my local machine.
Now I need to
- login to the remote server with a deploy user.
- Once I am logged in, I need to use su - to get the root user
- Now I can execute the mysqldump command (the deploy user gets an access denied message)
I want to have a script or command that I can execute from my local machine and have it go through the steps - log in as deploy, switch to root, create a mysql dump and copy it to my local machine.
What is the best way to go about that? Is it possible with SSH tunnels? Or is a scripted option better (how do you script multiple logins)?
You're probably best to use ansible for this: https://docs.ansible.com/ansible/2.5/modules/mysql_db_module.html
...which will take care of sudo'ing and 'how' the dump is done.
Example, a mysqldump.yml playbook contains:
...then target your mysql box, sudo'ing (-kK) like so:
You don’t need to be root on the Linux OS to be able to run MySQL commands as root , you can simply log in to the MySQL root account from your personal Linux accountby adding the root username and the root password to your script (and maybe change the file system location where you write the database dump).
Even better create a dedicated user for MySQL backups so you won’t need excessive DBA privileges for a simple backup ..
mysqldump --single-transaction which can be used with INNODB tables requires the following privileges:
mysqldump --lock-all-tables for MyISAMB tables additionally requires the LOCK TABLES role:
You could use a RSA Key to login directly to root, if that's an option.
To allow login directly to root with RSA key:
After that, generate your RSA key (local machine) and put it into authorized_keys (remote server):