Reputation: 8305
I have two mysql databases that have almost the same structure and representing the data of the same web app but one of them represents the current version and second one was made long time ago.
How can I create the database with both dumps inside but with old_ prefix for tables from the first and new_ prefix for tables from the second database?
Is there any mysqldump options to setup the prefix or other solution?
Upvotes: 4
Views: 8065
Reputation: 3109
Run the following query:
SELECT Concat('ALTER TABLE ', TABLE_NAME, ' RENAME TO my_prefix_', TABLE_NAME, ';') FROM information_schema.tables WHERE table_schema = 'my_database'
The output of which is several queries. Then run those queries.
This won't work if there's constraints, or other complicated things, but for simple DBs this works fine.
Upvotes: 0
Reputation: 31
This sed script is perhaps a little safer. Save it to a file and use sed -f to filter the dump file.
s/\(-- Table structure for table `\)\([^`]\+\)\(`\)/\1xyzzy_\2\3/
s/\(DROP TABLE IF EXISTS `\)\([^`]\+\)\(`\)/\1xyzzy_\2\3/
s/\(CREATE TABLE `\)\([^`]\+\)\(` (\)/\1xyzzy_\2\3/
s/\(-- Dumping data for table `\)\([^`]\+\)\(`\)/\1xyzzy_\2\3/
s/\(\/\*!40000 ALTER TABLE `\)\([^`]\+\)\(` DISABLE KEYS \*\/\)/\1xyzzy_\2\3/
s/\(LOCK TABLES `\)\([^`]\+\)\(` WRITE\)/\1xyzzy_\2\3/
s/\(INSERT INTO `\)\([^`]\+\)\(` VALUES (\)/\1xyzzy_\2\3/
s/\(\/\*!40000 ALTER TABLE `\)\([^`]\+\)\(` ENABLE KEYS \*\/\)/\1xyzzy_\2\3/
Search and replace xyzzy_ with your desired table prefix.
Upvotes: 2
Reputation: 65537
I may be misunderstanding the problem, but it sounds like you want to dump the 2 databases into a single SQL file to be used to restore the dbs, with the old tables going into one schema and the new tables going into another.
IF that's what you are trying to do, the simplest approach is just to insert the proper "use database" command before each dump.
Like so:
echo "use old_db;" > /tmp/combined_dump.sql
mysqldump old_db >> /tmp/combined_dump.sql
echo "use new_db;" >> /tmp/combined_dump.sql
mysqldump new_db >> /tmp/combined_dump.sql
Upvotes: 0
Reputation: 611
This stored procedure gets the table list from MySQL's inmemory tables in information_schema
and automatically moves to another DB using the RENAME
command.
DELIMITER $$
USE `db`$$
DROP PROCEDURE IF EXISTS `renameDbTables`$$
CREATE DEFINER=`db`@`%` PROCEDURE `renameDbTables`(
IN from_db VARCHAR(20),
IN to_db VARCHAR(30),
IN to_name_prefix VARCHAR(20)
)
BEGIN
/*
call db.renameDbTables('db1','db2','db_');
db1.xxx will be renamed to db2.db_xxx
*/
DECLARE from_state_table VARCHAR(20) DEFAULT '';
DECLARE done INT DEFAULT 0;
DECLARE b VARCHAR(255) DEFAULT '';
DECLARE cur1 CURSOR FOR SELECT TABLE_NAME FROM information_schema.TABLES
WHERE TABLE_SCHEMA=from_db;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
OPEN cur1;
REPEAT
FETCH cur1 INTO from_state_table;
IF NOT done THEN
-- select from_state_table;
SET @QUERY = '';
SET @QUERY = CONCAT(@QUERY,'RENAME TABLE ',from_db,'.', from_state_table,' TO ',to_db,'.', to_name_prefix, from_state_table,';');
-- SELECT @query;
PREPARE s FROM @QUERY;
EXECUTE s;
DEALLOCATE PREPARE s;
END IF;
UNTIL done END REPEAT;
CLOSE cur1;
END$$
DELIMITER ;
Upvotes: 1
Reputation: 4004
I have done the following using mysqldump
and sed
in the past, but I'll admit it may only be effective for one table at a time.
$ mysqldump -u user --password=mypass MyDB MyTable | sed s/MyTable/old_Mytable/ | mysql -u other_user -p NewDB
You could create a shell script with a list of the commands, one for each table, or perhaps another user has a way to modify this to work against multiple tables effectively in one shot.
Peer
Upvotes: 0
Reputation: 46423
A "mysqldump file" is just a text file full of SQL statements, so you can make quick modifications like these in a text editor.
1) Dump the two databases individually.
2) Edit the "old" dump file:
use mydatabase;
lineold_
in front of the table names. 3) Then, cat dump1 dump2 > combined_dump
4) mysql < combined_dump
Upvotes: 2
Reputation: 238068
Import them into different databases. Say they're called newdb
and olddb
. Then you can copy table1 to old_table1 like:
insert into newdb.old_table1
select *
from olddb.table1
If you have a huge number of tables, generate a script to copy them:
select concat('insert into newdb.old_', table_name,
'select * from olddb.', table_name, ';')
from information_schema.tables
where table_schema = 'olddb'
Upvotes: 0