不能接受长参数列表的 mysqldump



我已经尝试使用以前接受的解决方案来解决我的问题,但它们不起作用:

例子"id_list">

[jlefler@server ~]$ cat id_list | tail -10
17900018
17900019
17900020
17900021
17900022
17900023
17900024
17900025
17900026
17900027[jlefler@server ~]$ cat id_list |wc -l
51624

我试图执行一次转储2000个id的命令:

xargs -a id_list -d 'n' -n 2000 sh -c 'mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in ($0 $@)" >> dump.sql' x

但是我得到了这个意想不到的输出:

[jlefler@server ~]$ xargs -a id_list -d 'n' -n 2000 sh -c 'mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in ($0 $@)" >> dump.sql' x
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17851966
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17853966
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17855967
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17857967
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17859975
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17861978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17863978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17865978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17867978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17869978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17871978
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17873979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17875979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17877979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17879979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17881979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17883979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17885979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17887979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17889979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17891979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17893979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17895979
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17897980
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17899986
Warning: Using a password on the command line interface can be insecure.
"ysqldump: Couldn't find table: "17850378
基本上,最终结果应该是这样的:
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in (17900018,17900019,17900020...)" >> dump.sql

一次需要2000个id并将它们添加到转储中。sql,然后转到下一个2000,直到从文件中读取所有id。

使用echo命令显示输出,我得到以下结果:

)17853963ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17851964
)17855964ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17853964
)17857964ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17855965
)17859972ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17857965
)17861975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17859973
)17863975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17861976
)17865975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17863976
)17867975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17865976
)17869975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17867976
)17871975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17869976
)17873976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17871976
)17875976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17873977
)17877976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17875977
)17879976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17877977
)17881976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17879977
)17883976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17881977
)17885976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17883977
)17887976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17885977
)17889976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17887977
)17891976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17889977
)17893976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17891977
)17895976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17893977
)17897977ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17895977
)17899983ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17897978
)17850375ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (17899984
17900027)mp -uuser -ppassword --compact --no-create-info db table_name --where=id in (17850376
)17853963ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17851964
)17855964ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17853964
)17857964ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17855965
)17859972ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17857965
)17861975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17859973
)17863975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17861976
)17865975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17863976
)17867975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17865976
)17869975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17867976
)17871975ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17869976
)17873976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17871976
)17875976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17873977
)17877976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17875977
)17879976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17877977
)17881976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17879977
)17883976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17881977
)17885976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17883977
)17887976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17885977
)17889976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17887977
)17891976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17889977
)17893976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17891977
)17895976ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17893977
)17897977ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17895977
)17899983ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17897978
)17850375ump -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17899984
17900027)mp -uuser -ppassword --compact --no-create-info db table_name --where=id in (x 17850376

说明:

  • OP当前的语法问题似乎是由于dos/windows行结束符(rn)在输入文件(id_list)
  • 假设OP可以删除r字符,下一个问题可能是无效的in()列表…

示例输入文件:

$ cat id_list
1
2
3
4
5
6
7
8
9
10

修改xargs呼叫:

$ xargs -a id_list -n 3
1 2 3
4 5 6
7 8 9
10

添加echo以查看当前代码生成的内容:

$ xargs -a id_list -n 3 bash -c 'echo mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in ($0 $@)"'
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where=id in (1 2 3)
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where=id in (4 5 6)
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where=id in (7 8 9)
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where=id in (10 )

指出:

  • OP已经标记了bash的问题,所以我用bash -c代替了sh -c
  • 从这个输出看来,我们需要用逗号替换--where/in()子句的空格
  • 似乎我们还需要添加一对转义双引号,以便它们出现在--where/in()子句
  • 周围

解决这些问题的一个想法:

$ xargs -a id_list -n 3 bash -c 'list=$(sed "s/ /,/g;s/,$//g" <<< "$0 $@"); echo mysqldump -uuser -ppassword --compact --no-create-info db table_name --where=""id in ($list)""'
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where=id mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in (1,2,3)"
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in (4,5,6)"
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in (7,8,9)"
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in (10)"

我有一个不同的方法来处理这个问题(没有xargs)。
我试着重新创造你的场景,它对我很有效。
请看下面。

为id生成示例文件:

for i in {1000..15000};do echo "${i}" >> /tmp/ids.txt;done

代码:

#!/bin/bash
# Get the total count of Ids present in the file.
filecount=`wc -l /tmp/ids.txt | awk '{print $1}'`
while [ "${filecount}" != "0" ]
do
# Get the first 2000 ids from the file and make them comma-separated.
ids=`head -2000 /tmp/ids.txt | sed -ze 's/n/,/g' -e 's/,$//g'`
# Take the dump
mysqldump -uuser -ppassword --compact --no-create-info db table_name --where="id in (${ids})" >> /tmp/dump.sql
# Remove the 2000 ids so that we can proceed to the next ids 
sed -i '1,2000d' /tmp/ids.txt
# Necessary to end the while loop :)
filecount=`wc -l /tmp/ids.txt | awk '{print $1}'`
echo "${filecount}"
done

输出:

mysqldump: [Warning] Using a password on the command line interface can be insecure.
12001
mysqldump: [Warning] Using a password on the command line interface can be insecure.
10001
mysqldump: [Warning] Using a password on the command line interface can be insecure.
8001
mysqldump: [Warning] Using a password on the command line interface can be insecure.
6001
mysqldump: [Warning] Using a password on the command line interface can be insecure.
4001
mysqldump: [Warning] Using a password on the command line interface can be insecure.
2001
mysqldump: [Warning] Using a password on the command line interface can be insecure.
1
mysqldump: [Warning] Using a password on the command line interface can be insecure.
0

我能够成功地把这个转储。输出中的数字就是上面脚本中的echo "${filecount}"

注意:我建议在使用这种方法之前,先复制一份id_list文件,因为我们要从为其进行备份的文件中删除id。(对我来说,这只是一个自我生成的示例文件)

最新更新