Mysqldump more than one table? - mysql

How can I use mysqldump to dump certain tables that start with common prefix?

Hehe, this is kind of a hack, but it works (using bash):
mysqldump -u USER -p DATABASE $(mysql -u USER -p -D DATABASE -Bse "show tables like 'PREFIX%'") > /tmp/DATABASE.out
Change the ALLCAPS words as needed.

You can create backup.sh script:
BACKUP_DIR="/path/to/backups"
DB_HOST="domain.com"
DB_USER="user"
DB_PASS="pass"
PREFIX="phpbb"
TMP_LIST = mysql --host=$DB_HOST --user=$DB_USER --password=$DB_PASS -e "show databases;" | grep $PREFIX
# just get to database name column (may be $1, don't remember)
TMP_LIST = cat $TMP_LIST | awk '{print $2}'
# Getting dbs
mkdir $BACKUP_DIR/tmp
for db in $TMP_LIST ; do
mysqldump --host=$DB_HOST --user=$DB_USER --password=$DB_PASS --opt $db > $BACKUP_DIR/tmp/$db.sql
zip -mj $BACKUP_DIR/tmp/$db.sql.zip $BACKUP_DIR/tmp/$db.sql 2>&1
done
Hope it help.

Related

bash script to update MySQL from Access using MDB Tools

I'm trying to make a cronjob script that will take entries in a MS Access database (*.mdb) and update the sql database on the server. The script I found drops the tables and replaces them with the ones in the mdb file.
#!/bin/bash
TABLES=$(mdb-tables -1 $1)
MUSER="cloyd"
MPASS="******"
MDB="$2"
MYSQL=$(which mysql)
for t in $TABLES
do
$MYSQL -u $MUSER -p$MPASS $MDB -e "DROP TABLE IF EXISTS $t"
done
mdb-schema $1 mysql | $MYSQL -u $MUSER -p$MPASS $MDB
for t in $TABLES
do
mdb-export -D '%Y-%m-%d %H:%M:%S' -I mysql $1 $t | $MYSQL -u $MUSER -p$MPASS $MDB
done
I tried adding unique keys to the table on the IDs and remove the drop table part but it just wasn't updating the table.
I have figured it out. I am using mysqlimport instead and it seems to be working much better.
#!/bin/bash
TABLES=$(mdb-tables -1 $1)
MUSER="cloyd"
MPASS="************"
MDB="$2"
MYSQL=$(which mysql)
for t in $TABLES
do
mdb-export -D '%Y-%m-%d %H:%M:%S' -Q $1 $t > $t.csv
mysqlimport --fields-terminated-by=, --silent --local --replace --ignore-lines=1 --user=cloyd --password=**** timesystem $t.csv
done

Backup mysql databases into self contained files

I have a linux system with Mysql contain more than 400 databases,I need to export each databases as a single *.sql file.Is it possible to do this with mysql_dump or Mysqlworkbench.
I have tried mysql_dump with --all-databases option.but this make a file with all database.it is large in size.
One way to achieve this is to write a bash script: (Source)
#! /bin/bash
TIMESTAMP=$(date +"%F")
BACKUP_DIR="/backup/$TIMESTAMP"
MYSQL_USER="backup"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="password"
MYSQLDUMP=/usr/bin/mysqldump
mkdir -p "$BACKUP_DIR/mysql"
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
for db in $databases; do
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/mysql/$db.gz"
done
For more information, have a look at this similar question.

Can mysql take -e option and an input SQL script from command line at the same time?

$ mysql -h foobar.com -u user -p salesdb -e "SET #arg='date';" < "myscript.sql"
did not work. I wonder if using -e and < "myscript.sql" at the same time is allowed at all?
$ echo "SET #arg='date';" > input_script.sql
$ cat myscript.sql >> input_script.sql
$ mysql -h foobar.com -u user -p salesdb < input_script.sql
probably won't work at the same time, but you don't need that...
(echo "SET #arg='date';"; cat "myscript.sql") |
mysql -h foobar.com -u user -p salesdb
Found on http://www.cyberciti.biz/faq/using-mysql-in-shell-scripts/
#!/bin/bash
mysql dbnane<<EOFMYSQL
SELECT * from table;
EOFMYSQL

Dump tables from database in mysql?

How can I dump each table in database in separate file with that table name?
You may want to check out the shell script suggested in the following article:
How do I dump all tables in a database into separate files? by Sunny Walia
Script:
#!/bin/bash
db=$1
if [ "$db" = "" ]; then
echo "Usage: $0 db_name"
exit 1
fi
mkdir $$
cd $$
clear
for table in `mysql $db -e 'show tables' | egrep -v 'Tables_in_' `; do
echo "Dumping $table"
mysqldump --opt -Q $db $table > $table.sql
done
if [ "$table" = "" ]; then
echo "No tables found in db: $db"
fi
Here is a Linux command line to backup all tables in YOURDATABASENAME to separate files in a specific path:
You will have to replace YOURDATABASENAME and YOURPATH with appropriate values.
for I in $(mysql --database=YOURDATABASENAME -e 'show tables' -s --skip-column-names); do mysqldump YOURDATABASENAME $I | gzip > "/YOURPATH/YOURDATABASENAME/$I.sql.gz"; done
I use this as a scheduled cron job to backup all tables daily.
NOTE: if you are using this from a Linux command line, you will have to have to add a user with global privileges to the database for YOURUSERNAME#localhost with no password. Otherwise, you will have to add user and password options to the script as follows, but this will require a password for each table!
for I in $(mysql -u MYSQLUSERNAME -p --database=YOURDATABASENAME -e 'show tables' -s --skip-column-names); do mysqldump -u MYSQLUSERNAME -p YOURDATABASENAME $I | gzip > "/YOURPATH/YOURDATABASENAME/$I.sql.gz"; done

Is there a way to dump all mysql databases except for system databases?

I use mysqldump to automatically dump all my databases to a text file and save this dump as backup. I use the --all-databases option which dumps my databases, but it also dumps system databases (information_schema, phpmyadmin, etc.) which I don't need.
Is there a way to dump all my databases with mysqldump without naming them explicitly on the command line (so that I don't have to modify the backup script every time I create a new database), but ignore all the system databases?
A similar approach, excluding the dbs you don't want to backup:
user=''
pass=''
# Use a | as a separator
exclude_dbs='information_schema|mysql|performance_schema'
mysqldump -u "$user" -p"$pass" --databases $(mysql -u $user -p$pass -rs -e 'SHOW DATABASES;' | tail -n+1 | grep -v -E '^('$exclude_dbs')$' | tr '\n' ' ') > databases.sql
You could write a bash script like this. It checks the database's name before dumping it.
#!/bin/sh
DATABASES="$(/lighttpd/local/bin/mysql --user=user --password=pass -Bse 'show databases')"
for db in ${DATABASES[#]}
do
if [ $db == "information_schema" ]
then
continue
fi
echo ${db}-$(date +%m-%d-%y).sql.bz2 is being saved in /backup/mysql
mysqldump --user=user --password=pass $db --single-transaction -R | bzip2 -c > ${db}-$(date +%m-%d-%y).sql.bz2
done
Enter as root user and type in command line
for DB in $(mysql -Bse 'show databases' | grep -v information_schema); do \
mysqldump $DB > "/$DB.sql"; \
done