【问题标题】:mysqldump bash script throws errormysqldump bash脚本抛出错误
【发布时间】:2015-01-16 19:48:22
【问题描述】:

您好,我正在尝试创建一个备份某些表的脚本。表的数量很大,我不想复制粘贴命令。这是我的脚本。

#! /bin/bash
#The following commands exports/dump tables
# Usage: sh export-script DBName username password
#
#
#
export TABLES="A B C D...Z"
#
mysqldump $1 --skip-add-drop-table --replace --no-create-info --user=$2 --password=$3  ${TABLES} > dump.YYYY-MM-DD.sql

但是我收到以下错误:

"ysqldump: 找不到表:"Z

我是脚本新手,令人惊讶的是我找不到这个简单问题的解决方案。求救。

【问题讨论】:

  • 当我在终端上复制/粘贴脚本时,脚本运行没有问题。
  • 如果 MS windows 参与创建脚本文件,请使用 dos2unix mySqlDumperScript.sh。祝你好运。
  • @shellter 成功了。谢谢。

标签: mysql bash shell mysqldump


【解决方案1】:

在这里,我假设您将 export TABLES="A B C D...Z" 替换为不包含省略号的表列表。抱歉,不得不问一下,因为有些人认为 bash 的行为类似于其他脚本环境,可能会将 D...Z 扩展到 D E F ... etc,但事实并非如此。

尝试双引号 ${TABLES} 参数,如下所示:

mysqldump $1 --skip-add-drop-table --replace --no-create-info --user=$2 --password=$3  "${TABLES}" > dump.YYYY-MM-DD.sql

这会将所有表作为单个参数传递。如果这不起作用,我建议循环:

for table in ${TABLES} ; do
    mysqldump $1 --skip-add-drop-table --replace --no-create-info --user=$2 --password=$3  ${table} > dump_${table}.YYYY-MM-DD.sql
done

注意这如何为每个表创建一个单独的dump_ SQL 文件。如果所有表都需要一个文件,请使用:

/bin/rm -f dump.YYYY-MM-DD.sql
for table in ${TABLES} ; do
    mysqldump $1 --skip-add-drop-table --replace --no-create-info --user=$2 --password=$3  ${table} >> dump.YYYY-MM-DD.sql
done

我刚刚注意到 cmets 建议您使用 sh ... 运行。不要使用sh 运行。使用bash export-script DBName username password 运行,或者简单地使用export-script DBName username password 运行并让进程shell 通过顶部的#! 行选择运行脚本的shell。在这种情况下,它将使用bash 运行。

【讨论】:

  • 我认为行尾有问题。我应该在问题中提到我使用 Windows 编写此脚本。是的,它应该使用 bash 运行,因为文件以 #! 开头。感谢您的帮助。
猜你喜欢
  • 2014-12-09
  • 2023-03-07
  • 2016-08-18
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
相关资源
最近更新 更多