Gentoo Archives: gentoo-user

From: Alan McKinnon <alan.mckinnon@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Using date/time variables in cronjobs?
Date: Sun, 05 May 2013 20:17:57
Message-Id: 5186BE64.8020801@gmail.com
In Reply to: Re: [gentoo-user] Using date/time variables in cronjobs? by Tanstaafl
1 On 05/05/2013 20:31, Tanstaafl wrote:
2 > On 2013-05-05 2:18 PM, Neil Bothwick <neil@××××××××××.uk> wrote:> On
3 > Sun, 05 May 2013 14:07:50 -0400, Tanstaafl wrote:
4 >>> /home/user/mypg_backups/2013/May/Sun/pg_all-13:54.gz: No such file or
5 >>> directory
6 >>>
7 >>> So, it is expanding the variables properly, but apparently won't
8 >>> automatically create the directories? Is there some kind of flag
9 >>> I can add to the command to do that?
10 >
11 >> mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd/
12 >
13 > Thanks Neill...
14 >
15 > Tried changing the command in the script to:
16 >
17 >> /usr/bin/pg_dumpall -U $PGUSER -o | gzip > mkdir -p
18 >> $BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz
19
20
21 That can never possibly work. Look at it carefully and see what it does:
22
23 You pipe pg_dumpall to gzip and redirect that to a FILE called gzip.
24
25 I hope you don't run that as root in /bin/!
26 Anyway, what is the shell supposed to do with the -p $BACKUP.... ?
27
28 What you want cannot be done in one step. You must do it this way (in
29 pseudocode)
30
31 if dir does not exist
32 create dir
33 write output to file in that dir
34
35 In real bash
36
37 test -d $BACKUP_DIR/$PGyy/$PGmm/$PGdd/ || mkdir -p
38 $BACKUP_DIR/$PGyy/$PGmm/$PGdd/
39
40 /usr/bin/pg_dumpall -U $PGUSER -o -f
41 $BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz
42
43
44
45 I deliberately used the less common "test" command for readability
46 rather than what you will usually find in practice "[["
47
48
49
50 >
51 > and got this error:
52 >
53 > # ./ecat_pgdump.sh
54 > gzip: invalid option -- 'p'
55 >
56 > Tried putting quotes around it like this:
57 >
58 >> /usr/bin/pg_dumpall -U $PGUSER -o | gzip > "mkdir -p
59 >> $BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz"
60 >
61 > and got the original error with the added 'mkdir -p':
62 >
63 > # ./mypg_pgdumpall.sh
64 > ./mypg_pgdumpall.sh: line 10: mkdir -p
65 > /home/user/mypg_backups/2013/May/Sun/ecat-14:26.gz: No such file or
66 > directory
67 >
68 > I'm guessing I just need to know where to put the quotes?
69
70
71 No, you need to learn basic script techniques and how bash works wrt to
72 redirection and piping.
73
74 I recommend a rather famous tutorial - Bash Scripting Guide.
75
76 Last I looked it was available on tldp.org, but Google knows all the
77 places it is
78
79 --
80 Alan McKinnon
81 alan.mckinnon@×××××.com