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
Related
I wrote a script that I want to execute every minute
I wanna save my database every minute.
My script:
#!/bin/sh
DB_USER="root"
DB_PASS="root"
DB_HOST="localhost"
OUTDIR=`date +%Y-%m-%d/%H:%M:%S`
mkdir -p /Users/mess/backup_DB/$OUTDIR
DATABASES=`MYSQL_PWD=$DB_PASS mysql -u $DB_USER -e "SHOW DATABASES;" | tr -d "| " | grep -v -e Database -e _schema -e mysql`
for DB_NAME in $DATABASES; do
MYSQL_PWD=$DB_PASS mysqldump -u $DB_USER --single-transaction --skip-lock-tables $DB_NAME -h $DB_HOST > /Users/mess/backup_DB/$OUTDIR/$DB_NAME.sql
done
for DB_NAME in $DATABASES; do
gzip /Users/mess/backup_DB/$OUTDIR/$DB_NAME.sql
done
When I execute my script it’s work but when I use cron to execute it automatically it’s doesn’t work, crontab create the folder but not the file (database.sql.zip)
Here my cron :
PATH=/bin:/usr/bin:/usr/sbin:/Users/mess/scripts/bin
* * * * * backup
Hello i have 10 databases each database have 3 table with same name users
I'm trying to dump each database to a separated folder by name of database include in that directory all my tables dumped.
So i started with this sample code.
mysql -uroot -p -e 'show databases' | while read dbname; do mysqldump -uroot -p -T/backup/$dbname/ --fields-terminated-by="|" "$dbname" > "$dbname".txt; done
Then i get this error.
mysqldump: Can't create/write to file '/home/database/users.sql' (Errcode: 2 - No such file or directory)
I dont have much skills with bash scripting
Please help me!!
My second Script
#!/bin/bash
echo "MySQL backup"
mysql -u root -pPwd -e "show databases" \
| grep -Ev 'Database|information_schema' \
| while read dbname;
do
echo "Dumping $dbname"
mysqldump -u root -pPwd --fields-terminated-by="|" $dbname > /var/lib/mysql-files/$dbname/ > "$dbname".txt; done
done
You specified folders to store dumps, but do not create them in script, thus, add a mkdir prior to mysqldump command:
do
echo "Dumping $dbname"
mkdir -p /var/lib/mysql-files/${dbname}
mysqldump ... > /var/lib/mysql-files/${dbname}/${dbname}.sql
done
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
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.
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