Create table if not exists from mysqldump

前端 未结 8 1122
一整个雨季
一整个雨季 2020-12-29 21:35

I\'m wondering if there is any way in mysqldump to add the appropriate create table option [IF NOT EXISTS]. Any ideas?

相关标签:
8条回答
  • 2020-12-29 22:23

    Not what you might want, but with --add-drop-table every CREATE is prefixed with the according DROP TABLE statement.

    Otherwise, I'd go for a simple search/replace (e.g., with sed).

    0 讨论(0)
  • 2020-12-29 22:25

    create a bash script with this... make sure you make it executable. (chmod 0777 dump.sh)

    dump.sh

    #!/bin/bash
    
    name=$HOSTNAME
    name+="-"
    name+=$(date +"%Y-%m-%d.%H:%M")
    name+=".sql"
    echo $name;
    mysqldump --replace --skip-add-drop-table --skip-comments izon -p > "$name"
    sed -i 's/CREATE TABLE/CREATE TABLE IF NOT EXISTS/g' "$name"
    
    0 讨论(0)
提交回复
热议问题