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.
Related
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 under VPN and I don't have SSH access to remote server.
I can connect to remote database by console
mysql -u username -p -h remote.site.com
Now I'm trying to clone the remote database to local computer
mysqldump -u username -p -h remote.site.com mysqldump | mysql -u root -ppassword webstuff
And I've got error
mysqldump: Got error: 1045: Access denied for user 'webstaff'#'10.75.1.2'
(using password: YES) when trying to connect
How to copy mysql database from remote server to local computer?
Assuming the following command works successfully:
mysql -u username -p -h remote.site.com
The syntax for mysqldump is identical, and outputs the database dump to stdout. Redirect the output to a local file on the computer:
mysqldump -u username -p -h remote.site.com DBNAME > backup.sql
Replace DBNAME with the name of the database you'd like to download to your computer.
Check syntax and execute one command at a time, then verify output.
mysqldump -u remoteusername -p remotepassword -h your.site.com databasename > dump.sql
mysql -u localusername -p localpassword databasename < dump.sql
Once you've matched all passwords, you can use pipe.
Often our databases are really big and the take time to take dump directly from remote machine to other machine as our friends other have suggested above.
In such cases what you can do is to take the dump on remote machine using MYSQLDUMP Command
MYSQLDUMP -uuser -p --all-databases > file_name.sql
and than transfer that file from remote server to your machine using Linux SCP Command
scp user#remote_ip:~/mysql_dump_file_name.sql ./
This can have different reasons like:
You are using an incorrect password
The MySQL server got an error when trying to resolve the IP address of the client host to a name
No privileges are granted to the user
You can try one of the following steps:
To reset the password for the remote user by:
SET PASSWORD FOR some_user#ip_addr_of_remote_client=PASSWORD('some_password');
To grant access to the user by:
GRANT SELECT, INSERT, UPDATE, DELETE, LOCK TABLES ON YourDB.* TO user#Host IDENTIFIED by 'password';
Hope this helps you, if not then you will have to go through the documentation
Please check this gist.
https://gist.github.com/ecdundar/789660d830d6d40b6c90
#!/bin/bash
# copymysql.sh
# GENERATED WITH USING ARTUR BODERA S SCRIPT
# Source script at: https://gist.github.com/2215200
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
REMOTESERVERIP=""
REMOTESERVERUSER=""
REMOTESERVERPASSWORD=""
REMOTECONNECTIONSTR="-h ${REMOTESERVERIP} -u ${REMOTESERVERUSER} --password=${REMOTESERVERPASSWORD} "
LOCALSERVERIP=""
LOCALSERVERUSER=""
LOCALSERVERPASSWORD=""
LOCALCONNECTION="-h ${LOCALSERVERIP} -u ${LOCALSERVERUSER} --password=${LOCALSERVERPASSWORD} "
IGNOREVIEWS=""
MYVIEWS=""
IGNOREDATABASES="select schema_name from information_schema.SCHEMATA where schema_name != 'information_schema' and schema_name != 'mysql' and schema_name != 'performance_schema' ;"
# GET A LIST OF DATABASES
databases=`$MYSQL $REMOTECONNECTIONSTR -e "${IGNOREDATABASES}" | tr -d "| " | grep -v schema_name`
# COPY ALL TABLES
for db in $databases; do
# GET LIST OF ITEMS
views=`$MYSQL $REMOTECONNECTIONSTR --batch -N -e "select table_name from information_schema.tables where table_type='VIEW' and table_schema='$db';"
IGNOREVIEWS=""
for view in $views; do
IGNOREVIEWS=${IGNOREVIEWS}" --ignore-table=$db.$view "
done
echo "TABLES "$db
$MYSQL $LOCALCONNECTION --batch -N -e "create database $db; "
$MYSQLDUMP $REMOTECONNECTIONSTR $IGNOREVIEWS --compress --quick --extended-insert --skip-add-locks --skip-comments --skip-disable-keys --default-character-set=latin1 --skip-triggers --single-transaction $db | mysql $LOCALCONNECTION $db
done
# COPY ALL PROCEDURES
for db in $databases; do
echo "PROCEDURES "$db
#PROCEDURES
$MYSQLDUMP $REMOTECONNECTIONSTR --compress --quick --routines --no-create-info --no-data --no-create-db --skip-opt --skip-triggers $db | \
sed -r 's/DEFINER=`[^`]+`#`[^`]+`/DEFINER=CURRENT_USER/g' | mysql $LOCALCONNECTION $db
done
# COPY ALL TRIGGERS
for db in $databases; do
echo "TRIGGERS "$db
#TRIGGERS
$MYSQLDUMP $REMOTECONNECTIONSTR --compress --quick --no-create-info --no-data --no-create-db --skip-opt --triggers $db | \
sed -r 's/DEFINER=`[^`]+`#`[^`]+`/DEFINER=CURRENT_USER/g' | mysql $LOCALCONNECTION $db
done
# COPY ALL VIEWS
for db in $databases; do
# GET LIST OF ITEMS
views=`$MYSQL $REMOTECONNECTIONSTR --batch -N -e "select table_name from information_schema.tables where table_type='VIEW' and table_schema='$db';"`
MYVIEWS=""
for view in $views; do
MYVIEWS=${MYVIEWS}" "$view" "
done
echo "VIEWS "$db
if [ -n "$MYVIEWS" ]; then
#VIEWS
$MYSQLDUMP $REMOTECONNECTIONSTR --compress --quick -Q -f --no-data --skip-comments --skip-triggers --skip-opt --no-create-db --complete-insert --add-drop-table $db $MYVIEWS | \
sed -r 's/DEFINER=`[^`]+`#`[^`]+`/DEFINER=CURRENT_USER/g' | mysql $LOCALCONNECTION $db
fi
done
echo "OK!"
Copy mysql database from remote server to local computer
I ran into the same problem. And I could not get it done with the other answers. So here is how I finally did it (yes, a beginner tutorial):
Step 1: Create a new database in your local phpmyadmin.
Step 2: Dump the database on the remote server into a sql file (here I used Putty/SSH):
mysqldump --host="mysql5.domain.com" --user="db231231" --password="DBPASSWORD" databasename > dbdump.sql
Step 3: Download the dbdump.sql file via FTP client (should be located in the root folder)
Step 4: Move the sql file to the folder of your localhost installation, where mysql.exe is located. I am using uniform-server, this would be at C:\uniserver\core\mysql\bin\, with XAMPP it would be C:\xampp\mysql\bin
Step 5: Execute the mysql.exe as follows:
mysql.exe -u root -pYOURPASSWORD YOURLOCALDBNAME < dbdump.sql
Step 6: Wait... depending on the file size. You can check the progress in phpmyadmin, seeing newly created tables.
Step 7: Done. Go to your local phpmyadmin to check if the database has been filled with the entire data.
Hope that helps. Good luck!
Note 1: When starting the uniformer-server you can specify a password for mysql. This is the one you have to use above for YOURPASSWORD.
Note 2: If the login does not work and you run into password problems, check your password if it contains special characters like !. If so, then you probably need to escape them \!.
Note 3: In case not all mysql data can be found in the local db after the import, it could be that there is a problem with the mysql directives of your dbdump.sql
Better yet use a oneliner:
Dump remoteDB to localDB:
mysqldump -uroot -pMypsw -h remoteHost remoteDB | mysql -u root -pMypsw localDB
Dump localDB to remoteDB:
mysqldump -uroot -pmyPsw localDB | mysql -uroot -pMypsw -h remoteHost remoteDB
C:\Users\>mysqldump -u root -p -h ip address --databases database_name -r sql_file.sql
Enter password: your_password
This answer is not remote server but local server. The logic should be the same. To copy and backup my local machine MAMP database to my local desktop machine folder, go to console then
mysqldump -h YourHostName -u YourUserNameHere -p YourDataBaseNameHere > DestinationPath/xxxwhatever.sql
In my case YourHostName was localhost. DestinationPath is the path to the download; you can drag and drop your desired destination folder and it will paste the path in.
Then password may be asked:
Enter password: xxxxxxxx
How can i use mysqldump to backup and restore database to a remote server?
Both have root access. I am using putty to perform this.
So far I tried the following:
mysqldump -u root -p >z*x311a!# masdagn_joom15 | mysql \ -u root -p g2154hE6-AsXP --host=207.210.71.26 -C masdagn_joom15temp \g
but it refused
the local password is: >z*x311a!#
the remote password is: g2154hE6-AsXP
This link provides information on backing up and restoring with mysqldump. It also gives some examples with a remote server.
The important commands from that link being:
backup:
mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
restore:
mysql -u root -p[root_password] [database_name] < dumpfilename.sql
[local-server]# mysqldump -u root -prootpswd db | mysql \
-u root -ptmppassword --host=remote-server -C db1
[Note: There are two -- (hyphen) in front of host]
Please note that you should first create the db1 database on the remote-server before executing the following command.
mysqldump --user=username --password=pwd db_name | bzip2 -c > /backup_dir/db_name.sql.bz2
you can embed this part in a script, afterward you can use FTP to transfer to the other location.
To restore, you can
bzip2 -d db_name.sql.bz2
mysql --user=username --password=pwd db_name < db_name.sql
Your local password contains the > character, which is interpreted as a redirect character by most shells. As a general rule, it will make your life considerably easier if you keep your MySQL passwords alphanumeric [A-Za-z0-9]. And it will make your system more secure if you avoid publicly posting your passwords.
here is what I do for a quick dump to another remote server...
assuming that you have setup an ssh key between the 2 servers
create file dump-to-server.sh
chmod to executable (chmod 0755 dump-to-server.sh)
run your sync ./dump-to-server.sh schema_name root#remote.server.net
dump-to-server.sh
\#!/bin/bash
if [[ -z "$1" || -z "$2" ]]; then
echo "--------- usage ---------";
echo "./dump-to-server.sh schema_name root#remote.server.net";
echo "";
else
mysqldump --opt "$1" | gzip -c | ssh "$2" "gunzip -c | mysql $1"
fi
For a single DB, Taking backup from a remote server is :
mysqldump -u<user> -p<pwd> -h<remote-host> [database-name] > dump.sql
Restore is:
mysql -u<user> -p<pwd> -h<remote-host> [database-name] < dump.sql
more details about options of mysqldump are available here:
https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html
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