nix, shell, perl, php, mysql and mac os x tips and tricks

Friday, February 22, 2008

shell script to copy all MySQL databases from one server to another

01.#!/bin/bash
02. 
03.# destination for the backups
04.# mysqlbackup.lst needs to be in this dir,
05.# and list all the db names you want to transfer
06.dest="/home/user/downloads";
07.user="user";
08.pass="passwd";
09.host1="host1.net";
10.host2="host2.net";
11. 
12.for db in `cat $dest/mysqlbackup.lst`
13.do
14. 
15.    echo "dumping $db..."
16.    /usr/bin/mysqldump -u $user -h $host1 -p$pass $db > $dest/$db.sql
17.    echo "creating $db locally..."
18.    /usr/bin/mysqladmin -u $user -p$pass create $db
19.    echo "importing $db.sql to $db..."
20.    /usr/bin/mysql -u $user -h $host2 -p$pass $db < $dest/$db.sql
21.    echo "removing $dest/$db.sql..."
22.    rm $dest/$db.sql
23. 
24.done

No comments:

Post a Comment