We have a script which is working perfectly, we use it to copy a huge database from server A to server B. Now I want to copy just a table from server A to server B having the table name as a variable so the script should only ask for the table name and copy the table from A to B.
This is the script I have made, I must confess I am not very experienced in shell scripting.
#!/bin/sh
#Run on Server A
TABLENAME=$1
echo $TABLENAME
_now=$(date +"%A %d-%m-%Y "at" %T")
#Copy table $TABLENAME from server A to server B
#Dump table into /directory server A
mysqldump -u admin -p'*****' database_name $TABLENAME > /directory/$TABLENAME.sql
# Copie table to server B
scp /directory/$TABLENAME.sql root#server_b.domain.com:/directory/
# Replace table in database on server B
ssh root#server_b.domain.com "mysql -f -u admin -p'******' database_name -e 'source /directory/$TABLENAME.sql'"
#Remove file on server B
ssh root#server_b.domain.com "rm /directory/$TABLENAME.sql"
#Remove file on A
rm /directory/$TABLENAME.sql
this is the error i get:
.sql
./script_file_name: line 19: unexpected EOF while looking for matching `"'
./script_file_name: line 22: syntax error: unexpected end of file
thank you
You are missing quotes (",') as part of the command.
ssh root#server_b.domain.com "mysql -f -u admin -p'******' database_name -e 'source /directory/$TABLENAME'"
Try this as your combined ssh and mysql statement:
ssh root#server_b.domain.com "mysql -h localhost -u admin -p'******' database_name -e 'source /directory/$TABLENAME'"
Added -h and the database host.
Removed the -f switch.
Let me know how it went.
sorry to have bothered you, this is the solution of my problem:
1. i had 2 $ in my mysql password which needed to be escaped like 'fds\$fds\$gfds\$' i did not know this before.
2. The script will not ask for a table name but the table name must be typed as a parameter after the run command like this:
./filename table_name
Just to complete my input with the following solution that worked fine for me.
Except some changed directory names and , this is the same principle and problem solved.
Hope this is useful.
#!/bin/sh
# Copy table $TABLENAME (passed as argument)
# from server A to server B.
# Run on Server A
TABLENAME=$1
echo $TABLENAME
# Dump table into /root/bash on server A.
mysqldump -h localhost -u root -p'***' tablename $TABLENAME > /root/bash/$TABLENAME.sql
# Copy table to server B.
scp /root/bash/$TABLENAME.sql root#<ip address>:/root/bash/$TABLENAME.sql2
# Replace table in database on server B
ssh root#<ip address> "mysql -f -u root -p'***' tablename -e 'source /root/bash/$TABLENAME.sql2'"
# Remove file on server B.
ssh root#<ip address> "rm /root/bash/$TABLENAME.sql2"
# Remove file on A
rm /root/bash/$TABLENAME.sql
Related
I want to copy a database from one server to another (Germany and China), since they are both hosted servers by two different service providers I can't just replicate them because they won't allow me to change the configfiles of the servers. I came to the conclusion that I need to setup a 3rd Server at my location from which I then can copy the data towards the other server so I have a "Master" in Germany a "Slave" in China and a "Messenger" at my location. All the Commands must be executed on the "messenger" by a bash script. The script works fine until the file should be sent into the database there it gives me the error that the file doesn't exist, but it exists.
mysqldump -h [host] -u [user] -p[mysqlpassword] databasename > filename.sql
sshpass -p [mypassword] ssh [ChineseServerIp] -l [user] sshpass -p [mypassword] scp user#GermanServerIP:filename.sql /home
sshpass -p [mypassword] ssh [ChineseServerIP] -l [user] mysql -u [mysqlUser] -p[mysqlpassword] Databese < /home/filename.sql
I can't just copy the file from the "messenger" Server into the Chinese one, it just would take to long because of the "great Chinese firewall" (I gzip the .sql file and then Transfer it when its on the Chinese one I unzip and upload it).
its solved, I just had to set the inserting of the sql file in ""
sshpass -p [mypassword] ssh [ChineseServerIP] -l [user] "mysql -u [mysqlUser] -p[mysqlpassword] Databese < /home/filename.sql"
I wrote a sql script to create a clean database (one.sql), I did this on CMD and it works,
cd MYSQL\path1\bin
mysql -u userID -ppassword -h host -P port < path2\one.sql > path2\test.log
it gives me the test.log file and database on the sql server was updated
And I tried to do the same thing using batch file,
#echo off
c:
cd "MYSQL\path1\bin"
mysql -u userID -ppassword -h host -P port < path2\one.sql > path2\test.log
pause
from the CMD prompt, it seems working, but no test.log file generated, and the new database in my sql server was not created.
I ran bat file by double clicking it in its folder.
Any suggestions?
Thank you
I'm writing a bash script to do some db stuff. New to MySQL. I'm on Mac and have MySQL installed via homebrew.
Am using username "root" right now and there isn't a pw set. I included the pw syntax below just to help others out that may have a pw.
My goal is to have mysql commands be as "clean" as possible in my bash script
Not a hige deal, but would like to do this if possible.
Example
# If I can do it without logging in (*ideal)
mysql CREATE DATABASE dbname;
# Or by logging in with - mysql -u root -pPassword
CREATE DATABASE dbname;
# Instead of
mysql -u root -pPassword -e"CREATE DATABASE dbname";
Tried to simplify it. I have a handful of things I gotta do, so would rather keep my code cleaner if possible. I tried logging in with the bash script, but the script stopped once logged into MySQL and didn't run any commands.
Another option I was considering (but don't really like) would be just to keep username and pw string in a var and call it for every commmand like so
# Set the login string variable
login_details="-u root -p password -e"
# example command
mysql $login_details"CREATE DATABASE dbname";
So any ideas?
Write a new bash script file and run this file after putting all your commands into it. Don't forget to give right username and password in your bash script.
For bash script:
#!/bin/bash
mysql -u root -pSeCrEt << EOF
use mysql;
show tables;
EOF
If you want to run single mysql command.
mysql -u [user] -p[pass] -e "[mysql commands]"
Example:
mysql -h 192.168.1.10 -u root -pSeCrEt -e "show databases"
To execute multiple mysql commands:
mysql -u $user -p$passsword -Bse "command1;command2;....;commandn"
Note: -B is for batch, print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file. Batch mode results in nontabular output format and escaping of special characters. -s is silent mode. Produce less output. -e is to execute the statement and quit
D:
cd Tools/MySQL5/bin
mysql -u root mysql
use xyz;
source C:/Users/abc/Desktop/xyz.sql;
\q
When I run the above lines in command prompt it works fine but when I save it as a batch file and run the file it connects to mysql but doesn't perform the sql scripts.
Now what I see is wrong here is that while executing the above commands one by one in your prompt, once you run mysql -u root mysql, you are in the mysql console. So your source command would work there but would not work in your batch since you are not in mysql console while running the batch file.
Solution:
What you can do for this is, instead of using source in mysql you can use
mysql dbname < filename
in your batch file in place of
mysql -u root mysql
use xyz;
source C:/Users/abc/Desktop/xyz.sql;
This link can assist you further if needed
This should work
mysql -u root xyz < C:/Users/abc/Desktop/xyz.sql;
It sources the SQL commands from your file
You could write something like this
mysql -u dbUsername yourDatabase -e "SELECT * FROM table;"
Or to run repeating tasks create a runtasks.bat file, save under the root of your project then write your cmd tasks inside
mysql -u dbUser -e "DROP DATABASE IF EXISTS testDatabase;"
mysql -u dbUser -e "CREATE DATABASE testDatabase;"
php index.php migration latest #runs your migration files
cd application\tests
phpunit
This would work.
mysql.exe -u user_name -p -h _host_ _schema_ -e "select 1 from dual;"
This will also give you output on same command terminal
I have a folder with o lot of sql scripts. I want to run all of them without specifying names of them. Just specify a folder name. Is it possible?
You can not do that natively, but here's simple bash command:
for sql_file in `ls -d /path/to/directory/*`; do mysql -uUSER -pPASSWORD DATABASE < $sql_file ; done
here USER, PASSWORD and DATABASE are the corresponding credentials and /path/to/directory is full path to folder that contains your files.
If you want to filter, for example, only sql files, then:
for sql_file in `ls /path/to/directory/*.sql`; do mysql -uUSER -pPASSWORD DATABASE < $sql_file ; done
That was what worked for me:
1. Created a shell script in the folder of my scripts
for f in *.sql
do
echo "Processing $f file..."
mysql -u user "-pPASSWORD" -h HOST DATABASE < $f
done