aboutsummaryrefslogtreecommitdiff
path: root/pkgs/rdsdump/rdsdump.bash
blob: 6b8cbf17c3a0a7b48fa4bd1009cbb6fbd2a799ab (plain)
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
#!/usr/bin/env bash
set -euo pipefail

mysql_args=
mysqldump_args=
master_data=0
while [ $# -gt 0 ]; do
  case $1 in
    --host=*|--password=*|--user=*|\
    --defaults-file=*|--defaults-extra-file=*|\
    --ssl=*|--ssl-ca=*|--ssl-key=*|--ssl-cert=*|\
    -h?*|-u?*|-p?*)
      mysql_args="$mysql_args $1"
      mysqldump_args="$mysqldump_args $1"
      shift 1;;
    --host|--user|\
    --defaults-file|--defaults-extra-file|\
    --ssl-ca|--ssl-key|--ssl-cert|\
    -h|-u)
      mysql_args="$mysql_args $1 $2"
      mysqldump_args="$mysqldump_args $1 $2"
      shift 2;;
    --master-data=*)
      master_data=$(echo "$1" | cut -d= -f2)
      shift;;
    --master-data)
      master_data=$2
      shift 2;;
    *)
      mysqldump_args="$mysqldump_args $1"
      shift;;
  esac
done

replica () {
  mysql $mysql_args "$@"
}

start_replication () {
  replica -N -e "CALL mysql.rds_start_replication;" >&2
}

stop_replication () {
  replica -N -e "CALL mysql.rds_stop_replication;" >&2
}

trap 'start_replication' EXIT
stop_replication

if [ "$master_data" -gt 0 ]; then
if [ "$master_data" -eq 2 ]; then
  printf '-- '
fi
replica -e 'SHOW SLAVE STATUS\G' | awk -f <(cat - <<- 'AWK'
  /\<Exec_Master_Log_Pos\>/    { log_pos = $2 };
  /\<Relay_Master_Log_File\>/  { log_file = $2 };
  END {
    printf "CHANGE MASTER TO MASTER_LOG_FILE='%s', MASTER_LOG_POS=%d;\n", log_file, log_pos
  }
AWK
)
fi

mysqldump $mysqldump_args &
sleep 30

start_replication
trap - EXIT

wait