Advertisement
Guest User

Untitled

a guest
May 24th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Fix for cron incorrect paths
  4. HOME='/root'
  5.  
  6. # Sync database1 to database2
  7. SRC_DB='database1'
  8. DST_DB='database2'
  9.  
  10. EXCLUDE_TABLES=(table1 table2)
  11.  
  12. SRC_TABLES=`sudo -H mysql --defaults-extra-file=~/.my.cnf $SRC_DB -Bse 'show tables'`
  13.  
  14. for SRC_TABLE in ${SRC_TABLES[@]}
  15. do
  16. SYNC=1
  17.  
  18. for i in "${EXCLUDE_TABLES[@]}"
  19. do
  20. if [ "$i" == "$SRC_TABLE" ] ; then
  21. echo "Skipping $SRC_TABLE"
  22. SYNC=0
  23. break
  24. fi
  25. done
  26.  
  27. if [ $SYNC -eq 1 ] ; then
  28. echo "Syncing $SRC_TABLE"
  29. sudo -H pt-table-sync --execute --verbose --chunk-size=32M --charset=utf8 h=localhost,D=$SRC_DB,t=$SRC_TABLE h=localhost,D=$DST_DB,t=$SRC_TABLE
  30.  
  31. if [ $? -eq 1 ] || [ $? -eq 3 ] ; then
  32. echo "SYNC FAILED - Recreating Table on Destination"
  33.  
  34. sudo -H mysql --defaults-extra-file=/root/.my.cnf $DST_DB -e "drop table ${SRC_TABLE}"
  35. sudo -H mysqldump --defaults-extra-file=/root/.my.cnf --no-data ${SRC_DB} ${SRC_TABLE} > /tmp/sync_table.sql
  36. sudo -H mysql --defaults-extra-file=/root/.my.cnf $DST_DB < /tmp/sync_table.sql
  37. sudo -H pt-table-sync --execute --verbose --chunk-size=32M --charset=utf8 h=localhost,D=$SRC_DB,t=$SRC_TABLE h=localhost,D=$DST_DB,t=$SRC_TABLE
  38.  
  39. fi
  40. fi
  41. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement