-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmysql-sync
executable file
·90 lines (76 loc) · 2.49 KB
/
mysql-sync
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
#
# mysql-sync : Copy the data from a given database to another database.
# The schema of the second database must be compatible with the first one
#
# Version 0.1 (2010/07/22)
# (c) 2010 Mathieu Comandon
# Licensed under the terms of the GPL Version 3
#
set -e
usage()
{
cat << EOF
Usage: $0 --user=mysql_user --password=mysql_password --from=mysql_database --to=mysql_database
This script copies the data (and only the data) from a MySQL database to another MySQL database
OPTIONS:
-u, --user=user MySQL user (a user that has privileges on both tables)
-p, --password=password MySQL password
-f, --from=database Name of the source database
-t, --to=database Name of the destination database
-h, --help Prints this message
EOF
}
#Parse arguments
if [ "$#" -eq 0 ] ; then
usage
exit 2
fi
PARAMS=`getopt -n $0 -o u:p:f:t:h --long user:,password:,from:,to:,help -- "$@"`
eval set -- "$PARAMS"
while true ; do
case "$1" in
-u|--user) mysql_user=$2; shift 2 ;;
-p|--password) mysql_password=$2 ; shift 2 ;;
-f|--from) mysql_database_from=$2 ; shift 2 ;;
-t|--to) mysql_database_to=$2 ; shift 2 ;;
-h|--help) usage ; exit 1 ;;
--) shift ; break ;;
*) usage ; exit 2 ;;
esac
done
#Error checking
error_state=0;
if [ "$mysql_user" = '' ] ; then
echo "You MUST specify MySQL user !"
error_state=1
fi
if [ "$mysql_password" = '' ] ; then
echo "You MUST specify MySQL password !"
error_state=1
fi
if [ "$mysql_database_from" = '' ] ; then
echo "You MUST specify a source database !"
error_state=1
fi
if [ "$mysql_database_to" = '' ] ; then
echo "You MUST specify a destination database !"
error_state=1
fi
if [ "$error_state" = 1 ] ; then
echo "There are errors in your arguments, exiting."
exit 2
fi
dump_command="mysql-datadump -u ${mysql_user} -p ${mysql_password} -d ${mysql_database_from}"
dumpfile=$($dump_command)
#Save old data
backup_file='/tmp/'$mysql_database_to'-save-'$(date +%Y%m%d%H%M)'.sql'
mysqldump --user=$mysql_user --password=$mysql_password $mysql_database_to > $backup_file
#Erase old data
TABLES=$(mysql -u $mysql_user -p$mysql_password $mysql_database_to -e 'show tables' | awk '{ print $1}' | grep -v '^Tables' )
for t in $TABLES
do
mysql -u $mysql_user -p$mysql_password $mysql_database_to -e "SET foreign_KEY_CHECKS=0; delete from $t"
done
echo $dumpfile
mysql -u $mysql_user -p $mysql_password $mysql_database_to < $dumpfile