Skip to content

Instantly share code, notes, and snippets.

@harunorimurata
Last active June 16, 2023 05:40
Show Gist options
  • Save harunorimurata/c8937288cc7c78c1cda623b0d7a8a7f4 to your computer and use it in GitHub Desktop.
Save harunorimurata/c8937288cc7c78c1cda623b0d7a8a7f4 to your computer and use it in GitHub Desktop.
#!/bin/bash
FROM_USER=user1
FROM_HOST=host1
FROM_DB=database1
FROM_TBL=table1
TO_USER=user2
TO_HOST=host2
TO_DB=database2
TO_TBL=table2
### Remove fifo
cleanup() {
[ -p "$FIFO" ] && rm "$FIFO"
}
### Read and import tsv from file
# `--local-infile=1` and `set global local_infile= 1;` is required if v8.0 <= MySQL version
import_table() {
mysql \
-u "$TO_USER" -p \
-h "$TO_HOST" -D "$TO_DB" <<EOF
set foreign_key_checks = 0;
set unique_checks = 0;
-- alter table $TO_TBL disable keys;
load data local infile '$1' into table $TO_TBL;
set foreign_key_checks = 1;
set unique_checks = 1;
-- alter table $TO_TBL enable keys;
EOF
}
### Output tsv to stdout
export_table() {
mysql \
--batch \
-u "$FROM_USER" -p \
-h "$FROM_HOST" -D "$FROM_DB" \
-e "select * from $FROM_TBL;"
}
### Main
trap 'cleanup' EXIT
FIFO=$(mktemp -u -p .)
mkfifo -m 666 "$FIFO"
import_table $FIFO &
export_table | tee "$FIFO" > exported_data.tsv &
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment