I would like to update the following bash script, which individually dumps each table in a given schema:
for t in $(mysql -NBA -h db_host -u db_user -pdb_pass db_name -e 'show tables')
do
mysqldump -h db_host -u db_user -pdb_pass db_name $t > db_name.$t.sql
to exclude some tables that do not need to be picked up by this script. This is what I mean:
for t in $(mysql -NBA -h db_host -u db_user -pdb_pass db_name -e 'show tables where `Tables_in_db_name` not like 'table1' and `Tables_in_db_name` not like 'table2'')
do
mysqldump -h db_host -u db_user -pdb_pass db_name $t > db_name.$t.sql
The sql is fine in itself, but I can't get it to run inside the -e command. Obviously in this example, the problem is the -e command's opening and closing apostrophes. I have tried:
Using quotes instead of apostrophes. ie -e "show tables where etc"
Using slashes on the internal apostrophes. ie \'table1\'
With no success. Does anybody know how to accommodate apostrophes and back ticks within these confines?
Thank you.
The SQL is fine in itself, but I can't get it to run inside the -e command. Obviously in this example, the problem is the -e command's opening and closing apostrophes.
Right. I assume it will be necessary to escape the backticks. After a short test it seems that
mysql -NBA -h ${DB_HOST} -u "${DB_USER}" -p" ${DB_PASS}" ${DB_NAME} -e "SHOW TABLES WHERE \`Tables_in_${DB_NAME}\` NOT LIKE 'table%'"
is working.
Related
As the question says, I have been trying to export each table of a database in individual .sql files. I know mysqldump -u username -p DBName TableName > Export.sql exports one specific table, but is there any way I can loop it and export all tables in individual files using SSH command?
Find the tables in the database and iterate over it.
mysql -u USERNAME -p -N -D DATABASENAME -e 'show tables';
This will list the tables of a particular database. Do something like this to export all the tables of the database. (Note: you need to remove the formatting charaters)
for tab in `mysql -u USERNAME -p -N -D DATABASENAME -e 'show tables';` do
mysqldump -u USERNAME -p DATABASENAME $tab > ${tab}.sql
done
I can execute mysql passing in a file as follows.
mysql -u username -p < some_file
In a bash script I have a function which echoes output which I want to pass into the same command in a bash script.
some_function() {
echo "Some SQL"
}
How can I pass the output into mysql using pipes/redirection?
I have tried the following, but it fails with no such file or directory. How can I use the output from the function here instead.
mysql -u username -p < some_function
No need to use a pipe or a redirection in this case, you can use directly -e options to execute some SQL commands:
mysql -u username -p -e "SQL/MySQL commands"
Exemple on a specific database:
mysql -u username -p -e "use database_name; SHOW tables"
mysql -u username -p -e "SHOW tables" database_name
And you can also catch the output of a command or function and passing it as argument like this:
sql_command="$(your_function)"
mysql -u username -p -e "${sql_command}" database_name;
If you really want to use a pipe or a redirection (but I think it make no sense in this case):
$ mysql -u root -p database_name < <(echo "SHOW TABLES") # redirection
$ mysql -u root -p database_name <<< "$(echo "SHOW TABLES")" # another way to use redirection
$ echo "SHOW TABLES"|mysql -u root -p database_name # pipe
I am trying to execute a mysql query from a shell script on Ubuntu 16. Let's call the variable $thing.
Shell script
thing=stuff.whatever
mysql -h host -u superuser -ppassword123 --database bestdbever -e "update table set column1=1 where column2='$thing';"
where gets sent is this....
mysql -h host -u superuser -ppassword123 --database bestdbever -e "update table set column1=1 where column2='\''stuff.whatever'\'';'
When escaping the single quote, it sends
mysql -h host -u superuser -ppassword123 --database bestdbever -e "update table set column1=1 where column2=\'\''stuff.whatever\'\'';'
and then I cry.
How can I run a mysql query (just he query part) which is written to a file, eg. accounts.txt
/test/mysql -h testdb.com -P 8995 -p -u testaccount -e
"select distinct
amp.account_id
from
account_marketplace_groups amp;" -D company > /tmp/output.csv
How can I put the actual query in a file and execute the above and still get the output in a csv
Would this be the right way to do it ?
/test/mysql -h testdb.com -P 8995 -p -u testaccount -e /account.txt -D company > /tmp/output.csv
The -e option can't be used with a filename, it expects the parameter to be the query. Just use input redirection, since mysql reads from standard input.
/test/mysql -h testdb.com -P 8995 -p -u testaccount -D company < /account.txt > /tmp/output.csv
+1 to Barmar's answer but here is another solution, which works equally well:
/test/mysql -h testdb.com -P 8995 -p -u testaccount \
-e "source /account.txt" -D company > /tmp/output.csv
That is, if you don't want to use < for input redirection, you can use the -e "source ..." command to read the file from within the command-line.
Hi I'm doing a database copy and paste from a master to a slave. However there is a table on the slave that I don't want to be overwritten by the master.
I have been trying out the following bash script which is ran via cron job - but it keeps overwriting the slave table I want to ignore. What am I doing wrong?
#!/bin/bash
#Database login credentials - need to be changed accordingly
dbHost="localhost"
#Master (Staging)
dbMastUser="admin_site"
dbMastName="admin_site_pineapple_master"
#Slave (Live)
dbSlavUser="admin_sync"
dbSlavName="admin_site_pineapple_slave"
dbPass="ExamplePassword"
EXCLUDED_TABLES=(
forms-responses
)
IGNORED_TABLES=''
for TABLE in "${EXCLUDED_TABLES[#]}"
do :
IGNORED_TABLES+=" --ignore-table=${dbMastName}.${TABLE}"
done
#Update the database from the Master to the Slave
mysqldump -h ${dbHost} -u ${dbMastUser} -p${dbPass} ${dbMastName} ${IGNORED_TABLES} | mysql -h ${dbHost} -u ${dbSlavUser} -p${dbPass} ${dbSlavName}
For ignoring tables you must use this syntax
mysqldump -h {hostname} -u {username} -p{password} --ignore-table test.votes test > E:/db_backups/test_1480080906.sql
general syntax
mysqldump -h {hostname} -u {username} -p{password} --ignore-table dbname.tbl_name db_name > E:/db_backups/test_1480080906.sql
Note : we need to set the options to ignore some tables
Thanks
Suman W.
/usr/local/mysql/bin/mysqldump -uroot -p{pwd} --skip-lock-tables --databases f6dms $(mysql -uroot -p{pwd} -Df6dms -Bse "show tables like 'tm_monitor_avg_price_%'"|awk '{print "--ignore-table=f6dms."$1}'|xargs)| gzip > /data/backup/database_f6dms_`date '+%m-%d-%Y'`.sql.gz;