solarflare
solarflare

Reputation: 441

Including a date/time in a file name dumped from psql

I'm planning on running a .sh script that will run periodically through cron on linux. I'm running postgres 8.4 on centos.

My script will have something like this in it:

psql -U username -d db_name -c "COPY orders TO stdout DELIMITER ',' CSV HEADER;" > orders.csv

I know there are other ways to dump tables into csv files but this is the only one I could use without admin rights.

My problem is naming the files. I want to specifically name the file something along the lines of: yyyymmdd-hhmm-orders.csv

I'm not the best scripting guru out there (as you can tell) so how can I get the dumps to dynamically do this?

Thanks

Upvotes: 0

Views: 2317

Answers (2)

user8854776
user8854776

Reputation: 201

Use below code and its worked fine

Assigned date format with one variable and used the same

Code:

I=`date +%Y%m%d-%H%M%S -d`
psql -U username -d db_name -c "COPY orders TO stdout DELIMITER ',' CSV HEADER;" > $i-orders.csv

Upvotes: 0

amacvar
amacvar

Reputation: 311

`date '+%Y%m%d-%H%M'`-orders.csv

I personally also add the seconds %S to the file name

man date

Will show the other formatting options

Upvotes: 3

Related Questions