简体   繁体   English

Shell脚本创建新的SQL转储

[英]Shell script to create a new SQL dump

I am currently using MAMP pro and i want to create a simple terminal script (or a .bat file like on the PC) which allows me to create a sql dump (where i go in and preconfigure the database name) and it automatically creates me the .sql file. 我当前正在使用MAMP pro,我想创建一个简单的终端脚本(或PC上的.bat文件),该脚本可让我创建sql dump(我要在其中输入并预先配置数据库名称),然后它会自动创建我.sql文件。

This is what i know so far: 这是我到目前为止所知道的:

1: The script should first visit cd /applications/MAMP/library/bin 1:脚本应首先访问cd /applications/MAMP/library/bin

2: When it in that directory, it should run the following 2:当它在该目录中时,应运行以下命令

./mysqldump -u root -p databaseName > /Applications/MAMP/htdocs/website/db.sql

Finally, in that directory, when i click the script, it should not replace the older file, but create a new one, maybe just add the date or a integer after it. 最后,在该目录中,当我单击脚本时,它不应替换旧文件,而是创建一个新文件,也许只是在其后添加日期或整数。

Is that possible? 那可能吗?

Apologies, i have no idea the best way of doing this. 抱歉,我不知道这样做的最佳方法。

Thanks in advance. 提前致谢。

You can have this: 您可以拥有:

#!/bin/sh
TS=$(exec date '+%s')
cd /applications/MAMP/library/bin && \
    ./mysqldump -u root -p databaseName > "/Applications/MAMP/htdocs/website/db.$TS.sql"

It would create files in the form of db.(timestamp).sql . 它将以db.(timestamp).sql的形式创建文件。

If you want to have other forms, just changhe the date command. 如果要使用其他形式,只需更改date命令即可。 '+%s' specifies the format to produce which is the timestamp. '+%s'指定要产生的格式,即时间戳。 You can see other formats with date --help or man date . 您可以使用date --helpman date查看其他格式。

Just a variation on the other answer (vote for it!). 只是其他答案的一种(投票!)。 You don't have to change directory to execute mysqldump : 您无需更改目录即可执行mysqldump

#!/bin/bash
TS=$(exec date '+%s')
/applications/MAMP/library/bin/mysqldump -u root -p databaseName \
                  > "/Applications/MAMP/htdocs/website/db.$TS.sql"

Please beware that you have both /Application and /application directories. 请注意,您同时拥有/Application/application目录。 Depending your FS case sensitivity this might be an error. 根据您的FS区分大小写,这可能是一个错误。

Are you using the OSx? 您在使用OSx吗?

#!/bin/bash
cd /applications/MAMP/library/bin && mysqldump -u root -p databaseName >   /Applications/MAMP/htdocs/website/db.sql

After this, you'll need set the permission to your script: 之后,您需要为脚本设置权限:

chmod +x your_script_file

Now, you can call... 现在,您可以致电...

./your_script_file

or 要么

sh yout_script_file

If you want to replace the old file, this command will do this, because the new file name is the same the older file name 如果要替换旧文件,此命令将执行此操作,因为新文件名与旧文件名相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM