bash 创建带有时间戳的 PostgreSQL 备份文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14728339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:29:23  来源:igfitidea点击:

Create PostgreSQL backup files with timestamp

linuxbashpostgresqlshellbackup

提问by Luiz E.

I'm doing a dump like this pg_dump prod-db -U postgres > prod-db.sql, but would be cool if I could do a dump like pg_dump prod-db -U postgres > prod-db-{date}.sqland be able to generate a file like prod-db-06-02-13.sqlvia shell...
I have no idea on how to start nor where to looking for. Any ideas, links, docs would be much appreciated.

我正在做这样的转储pg_dump prod-db -U postgres > prod-db.sql,但如果我能做类似的转储pg_dump prod-db -U postgres > prod-db-{date}.sql并且能够prod-db-06-02-13.sql通过 shell生成一个文件会很酷......
我不知道如何开始也不知道在哪里寻找。任何想法,链接,文档将不胜感激。

回答by Anton Kovalenko

Try this:

尝试这个:

pg_dump prod-db -U postgres > prod-db-$(date +%d-%m-%y).sql

Here's the date manual, for other format options.

这是其他格式选项的日期手册

回答by Ed Heal

Use backticks and the datecommand.

使用反引号和date命令。

i.e.

IE

pg_dump prod-db -U postgres > prod-db-`date +d-%m-%y`.sql