首页 MySQL 正文
299

mysqldump选择性导出一些行(--where选项)

  • yiqingpeng
  • 2018-10-15
  • 0
  •  
mysqldump -hlocalhost -P3306 -umyroot -pmypwd\!db2009\$ -C --single-transaction --no-create-info --opt mydatabase mytable --where="create_date>='2018-01-01'" > /var/www/backup.sql;
##密码中的特殊符号需要转义。
##一条命令只能操作一张表。

写成shell脚本以供调用:
#!/bin/bash
if [ $# -eq 0 ]; then
    echo 'Please specify dbname, tablename, like: dbname.tablename'
    exit 1
fi

#or for i in $@ do .... done
#while [[ $# -gt 0 ]]
#do
#param=$1
#shift
#done

function doExport {
    db=$1
    tbl=$2
    where=$3
    opt=$4
    if [[ -z $db || -z $tbl ]];
    then
        echo "Db and Table required"
        exit 1
    fi
    if [[ -z $where ]];
    then
        echo "Please specify condition, if non, input 1"
        exit 1
    fi
    if [[ -z $4 ]];
    then
        opt=""
    elif [[ "$4"x = "c"x ]];
    then
        opt="--no-create-info"
    else
        opt="-d"
    fi
    `mysqldump -hlocalhost -P3306 -umyroot -pmypwd\!db2009\$ -C --single-transaction $opt --opt $db $tbl --where="$where" 
> ~/sql/$db.$tbl.sql`
    cd ~/sql/
    tar -cvzf $db.$tbl.sql.tar.gz $db.$tbl.sql
}
doExport $1 $2 $3 $4

正在加载评论...