For mange gange siden skrev jeg et script til dette problem. Som sagt ovenfor - replikering er det en rigtig god løsning, men i mit tilfælde kunne jeg ikke bruge det. Så .. hvis du har brug for noget lignende replikering af master->slave, vil dette script måske være nyttigt:
dumps.sh :
while getopts "c:l:" opt; do
case $opt in
c)
if [ -r "$OPTARG" ]; then
source "$OPTARG"
else
echo "Unreadable config file \"$OPTARG\""
exit 1
fi
;;
l) LOG_FILE="$OPTARG"
if [ ! -f "$LOG_FILE" ]; then
`touch $LOG_FILE`
fi
;;
\?) echo "Invalid options. -$OPTARG. USE -c config_file"
exit 1
;;
:) "Option -$OPTARG requires an argument."
;;
esac
done
logIt()
{
date_now=`date '+%D %T'`
if [ $LOG_FILE != "" ]; then
echo "$date_now : $*" >> $LOG_FILE
else
echo "$date_now : $*"
fi
}
build_tables()
{
TAB=""
logIt [email protected]
for table in $TABLES
do
TAB="$TAB ${1}${table}"
done
echo $TAB
}
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
RM="$(which rm)"
DEST="."
MBD="$DEST/mysql"
eval $RM -fv "$MBD/*"
FILE=""
[ ! -d $MBD ] && mkdir -p $MBD || :
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST
TAB=`build_tables $DB_PREFIX`
FILE="$MBD/$DB_NAME.sql";
($MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $DB_NAME $TAB 2>> $LOG_FILE) > $FILE
input_to_mysql()
{
###############################3
CP="$(which cp)"
SED="$(which sed)"
len=${#INSERT_DB_NAME[*]}
i=0
while [ $i -lt $len ]; do
NEW_FILE="$MBD/${INSERT_DB_NAME[$i]}.sql"
eval $CP $FILE $NEW_FILE
eval $SED -i "s/$DB_PREFIX/${INSERT_DB_PREFIX[$i]}/g" $NEW_FILE
eval $SED -i "s/^.\*!.*$//g" $NEW_FILE
let i++
done
i=0
while [ $i -lt $len ]; do
NAME="$MBD/${INSERT_DB_NAME[$i]}.sql"
if [ -e $NAME ]; then
$MYSQL -u${INSERT_DB_USER[$i]} -p${INSERT_DB_PASS[$i]} -h${INSERT_DB_HOST[$i]} ${INSERT_DB_NAME[$i]} < $NAME 2>> $LOG_FILE
#echo "$MYSQL -u${INSERT_DB_USER[$i]} -p${INSERT_DB_PASS[$i]} -h${INSERT_DB_HOST[$i]} ${INSERT_DB_NAME[$i]} < $NAME"
logIt "IMPORT TO ${INSERT_DB_NAME[$i]}"
else
logIt "File $NAME not exist";
fi
let i++
done
}
check_dump()
{
FILE_TMP_DUMP="$MBD/tmp_dump_${INSERT_DB_NAME[0]}.sql";
FILE_DIFF_RESS="$MBD/diff_res.diff"
tmp_tables=`build_tables ${INSERT_DB_PREFIX[0]}`
($MYSQLDUMP -u ${INSERT_DB_USER[0]} -h ${INSERT_DB_HOST[0]} -p${INSERT_DB_PASS[0]} ${INSERT_DB_NAME[0]} $tmp_tables 2>>$LOG_FILE) > $FILE_TMP_DUMP
DIFF="$(which diff)"
$DIFF $FILE $FILE_TMP_DUMP > FILE_DIFF_RESS
[ -s "$SMB/diff_res.diff" ];
SUCCESS=$?
eval $RM -f $FILE_TMP_DUMP $FILE_DIFF_RESS
return $SUCCESS
}
if check_dump; then
input_to_mysql
else
logIt "No need to dump"
fi
så kør som dump.sh -c config -l log.file
Hvor konfiguration som denne:
MyHOST="master_host"
MyUSER="master_user"
MyPASS="master_password"
DB_NAME="master_db_name"
DB_PREFIX="master_db_prefix_" # leave empty if you haven't table prefix
TABLES="table1 table2 table3" // list of tables - leave empty for all tables
###############################
INSERT_DB_NAME=(slave_dbname1 slave_dbname2)
INSERT_DB_HOST=(slave_host1 slave_host2)
INSERT_DB_USER=(slave_user1 slave_user2)
INSERT_DB_PASS=(slave_pass1 slave_pass2)
INSERT_DB_PREFIX=(slave_db_prefix1 slave_db_prefix1) //
Jeg satte den til cron, og alt fungerer fint for mig. Selvfølgelig kan du gøre alt dette manuelt ...