* [gentoo-user] Using date/time variables in cronjobs? @ 2013-05-05 16:56 Tanstaafl 2013-05-05 17:07 ` Mick ` (2 more replies) 0 siblings, 3 replies; 16+ messages in thread From: Tanstaafl @ 2013-05-05 16:56 UTC (permalink / raw To: gentoo-user Ok, another little thing... Is there a simple way to use date/time variables in cronjobs? Or do I need to use a bash script for this? I prefer simple, and just using the variables directly in the cron command would be easier if it works, so figured I'd ask first... I'm trying to schedule a dump of my databases like so: pg_dumpall --username=username -o -f /home/user/mypg_backups/hourly/\%y/\%m/\%d/\%t.sql.gz But trying to run this command fails with: pg_dumpall: could not open the output file "/home/user/mypg_backups/hourly/%y/%m/%d/%t.sql.gz": No such file or directory Tried escaping the variables and not, same error... ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 16:56 [gentoo-user] Using date/time variables in cronjobs? Tanstaafl @ 2013-05-05 17:07 ` Mick 2013-05-05 17:32 ` Tanstaafl 2013-05-05 17:49 ` Alan McKinnon 2013-05-05 18:07 ` Tanstaafl 2 siblings, 1 reply; 16+ messages in thread From: Mick @ 2013-05-05 17:07 UTC (permalink / raw To: gentoo-user [-- Attachment #1: Type: Text/Plain, Size: 826 bytes --] On Sunday 05 May 2013 17:56:33 Tanstaafl wrote: > Ok, another little thing... > > Is there a simple way to use date/time variables in cronjobs? Or do I > need to use a bash script for this? I prefer simple, and just using the > variables directly in the cron command would be easier if it works, so > figured I'd ask first... > > I'm trying to schedule a dump of my databases like so: > > pg_dumpall --username=username -o -f > /home/user/mypg_backups/hourly/\%y/\%m/\%d/\%t.sql.gz > > But trying to run this command fails with: > > pg_dumpall: could not open the output file > "/home/user/mypg_backups/hourly/%y/%m/%d/%t.sql.gz": No such file or > directory > > Tried escaping the variables and not, same error... Is it perhaps a matter of the process having write access? -- Regards, Mick [-- Attachment #2: This is a digitally signed message part. --] [-- Type: application/pgp-signature, Size: 490 bytes --] ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 17:07 ` Mick @ 2013-05-05 17:32 ` Tanstaafl 0 siblings, 0 replies; 16+ messages in thread From: Tanstaafl @ 2013-05-05 17:32 UTC (permalink / raw To: gentoo-user On 2013-05-05 1:07 PM, Mick <michaelkintzios@gmail.com> wrote: > On Sunday 05 May 2013 17:56:33 Tanstaafl wrote: >> Ok, another little thing... >> >> Is there a simple way to use date/time variables in cronjobs? Or do I >> need to use a bash script for this? I prefer simple, and just using the >> variables directly in the cron command would be easier if it works, so >> figured I'd ask first... >> >> I'm trying to schedule a dump of my databases like so: >> >> pg_dumpall --username=username -o -f >> /home/user/mypg_backups/hourly/\%y/\%m/\%d/\%t.sql.gz >> >> But trying to run this command fails with: >> >> pg_dumpall: could not open the output file >> "/home/user/mypg_backups/hourly/%y/%m/%d/%t.sql.gz": No such file or >> directory >> >> Tried escaping the variables and not, same error... > > Is it perhaps a matter of the process having write access? No, it is running as root... ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 16:56 [gentoo-user] Using date/time variables in cronjobs? Tanstaafl 2013-05-05 17:07 ` Mick @ 2013-05-05 17:49 ` Alan McKinnon 2013-05-05 18:07 ` Tanstaafl 2 siblings, 0 replies; 16+ messages in thread From: Alan McKinnon @ 2013-05-05 17:49 UTC (permalink / raw To: gentoo-user On 05/05/2013 18:56, Tanstaafl wrote: > Ok, another little thing... > > Is there a simple way to use date/time variables in cronjobs? Or do I > need to use a bash script for this? I prefer simple, and just using the > variables directly in the cron command would be easier if it works, so > figured I'd ask first... > > I'm trying to schedule a dump of my databases like so: > > pg_dumpall --username=username -o -f > /home/user/mypg_backups/hourly/\%y/\%m/\%d/\%t.sql.gz > > But trying to run this command fails with: > > pg_dumpall: could not open the output file > "/home/user/mypg_backups/hourly/%y/%m/%d/%t.sql.gz": No such file or > directory > > Tried escaping the variables and not, same error... What creates those variables %y etc? They don't "just exist", something sets them. Check that that thing sets them correctly. But I'll bet the directory /home/user/mypg_backups/hourly/\%y/\%m/\%d/ doesn't exist. pg_dumpall won;t create it, that's your job. So now you need a test that the directory exists, mkdir -p it if not, and only then write the output file. Which is far easier done in a wrapper script than in a one-liner, if only for the fact that you have indented text. Sometimes, making things simpler makes your life harder. This looks like such a case. -- Alan McKinnon alan.mckinnon@gmail.com ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 16:56 [gentoo-user] Using date/time variables in cronjobs? Tanstaafl 2013-05-05 17:07 ` Mick 2013-05-05 17:49 ` Alan McKinnon @ 2013-05-05 18:07 ` Tanstaafl 2013-05-05 18:18 ` Neil Bothwick 2013-05-05 18:31 ` Tanstaafl 2 siblings, 2 replies; 16+ messages in thread From: Tanstaafl @ 2013-05-05 18:07 UTC (permalink / raw To: gentoo-user Ok, some of those variables were wrong (copy pasted from my first brain-dead attempt)... I now have a rudimentary bash script with contents: #!/bin/bash BACKUP_DIR="/home/user/mypg_backups" PGUSER="superuser" PGtt=`date '+%H:%M'` PGhr=`date '+%H'` PGdd=`date '+%a'` PGmm=`date '+%b'` PGyy=`date '+%Y'` /usr/bin/pg_dumpall -U $PGUSER -o | gzip > $BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz Running this results in: # ./mypg_pgdumpall.sh ./mypg_pgdumpall.sh: line 10: /home/user/mypg_backups/2013/May/Sun/pg_all-13:54.gz: No such file or directory So, it is expanding the variables properly, but apparently won't automatically create the directories? Is there some kind of flag I can add to the command to do that? Running the bare command without the variables works as expected, so it is just a matter of getting the directories auto created if they don't already exist. Is there a reasonably easy way of doing this? On 2013-05-05 12:56 PM, Tanstaafl <tanstaafl@libertytrek.org> wrote: > Ok, another little thing... > > Is there a simple way to use date/time variables in cronjobs? Or do I > need to use a bash script for this? I prefer simple, and just using the > variables directly in the cron command would be easier if it works, so > figured I'd ask first... > > I'm trying to schedule a dump of my databases like so: > > pg_dumpall --username=username -o -f > /home/user/mypg_backups/hourly/\%y/\%m/\%d/\%t.sql.gz > > But trying to run this command fails with: > > pg_dumpall: could not open the output file > "/home/user/mypg_backups/hourly/%y/%m/%d/%t.sql.gz": No such file or > directory > > Tried escaping the variables and not, same error... > ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 18:07 ` Tanstaafl @ 2013-05-05 18:18 ` Neil Bothwick 2013-05-05 18:31 ` Tanstaafl 1 sibling, 0 replies; 16+ messages in thread From: Neil Bothwick @ 2013-05-05 18:18 UTC (permalink / raw To: gentoo-user [-- Attachment #1: Type: text/plain, Size: 443 bytes --] On Sun, 05 May 2013 14:07:50 -0400, Tanstaafl wrote: > /home/user/mypg_backups/2013/May/Sun/pg_all-13:54.gz: No such file or > directory > > So, it is expanding the variables properly, but apparently won't > automatically create the directories? Is there some kind of flag I can > add to the command to do that? mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd/ -- Neil Bothwick A friend in need may turn out to be a nuisance. [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 198 bytes --] ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 18:07 ` Tanstaafl 2013-05-05 18:18 ` Neil Bothwick @ 2013-05-05 18:31 ` Tanstaafl 2013-05-05 20:06 ` Todd Goodman 2013-05-05 20:17 ` Alan McKinnon 1 sibling, 2 replies; 16+ messages in thread From: Tanstaafl @ 2013-05-05 18:31 UTC (permalink / raw To: gentoo-user On 2013-05-05 2:18 PM, Neil Bothwick <neil@digimed.co.uk> wrote:> On Sun, 05 May 2013 14:07:50 -0400, Tanstaafl wrote: >> /home/user/mypg_backups/2013/May/Sun/pg_all-13:54.gz: No such file or >> directory >> >> So, it is expanding the variables properly, but apparently won't >> automatically create the directories? Is there some kind of flag >> I can add to the command to do that? > mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd/ Thanks Neill... Tried changing the command in the script to: > /usr/bin/pg_dumpall -U $PGUSER -o | gzip > mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz and got this error: # ./ecat_pgdump.sh gzip: invalid option -- 'p' Tried putting quotes around it like this: > /usr/bin/pg_dumpall -U $PGUSER -o | gzip > "mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz" and got the original error with the added 'mkdir -p': # ./mypg_pgdumpall.sh ./mypg_pgdumpall.sh: line 10: mkdir -p /home/user/mypg_backups/2013/May/Sun/ecat-14:26.gz: No such file or directory I'm guessing I just need to know where to put the quotes? ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 18:31 ` Tanstaafl @ 2013-05-05 20:06 ` Todd Goodman 2013-05-05 21:25 ` Neil Bothwick 2013-05-05 20:17 ` Alan McKinnon 1 sibling, 1 reply; 16+ messages in thread From: Todd Goodman @ 2013-05-05 20:06 UTC (permalink / raw To: gentoo-user * Tanstaafl <tanstaafl@libertytrek.org> [130505 14:32]: > On 2013-05-05 2:18 PM, Neil Bothwick <neil@digimed.co.uk> wrote:> On > Sun, 05 May 2013 14:07:50 -0400, Tanstaafl wrote: > >> /home/user/mypg_backups/2013/May/Sun/pg_all-13:54.gz: No such file or > >> directory > >> > >> So, it is expanding the variables properly, but apparently won't > >> automatically create the directories? Is there some kind of flag > >> I can add to the command to do that? > > > mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd/ > > Thanks Neill... > > Tried changing the command in the script to: > > > /usr/bin/pg_dumpall -U $PGUSER -o | gzip > mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz > > and got this error: > > # ./ecat_pgdump.sh > gzip: invalid option -- 'p' > > Tried putting quotes around it like this: > > > /usr/bin/pg_dumpall -U $PGUSER -o | gzip > "mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz" > > and got the original error with the added 'mkdir -p': > > # ./mypg_pgdumpall.sh > ./mypg_pgdumpall.sh: line 10: mkdir -p > /home/user/mypg_backups/2013/May/Sun/ecat-14:26.gz: No such file or > directory > > I'm guessing I just need to know where to put the quotes? You can't put it in that line. Put it on it's own line before the pg_dumpall line: mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd /usr/bin/pg_dumpall -U $PGUSER -o | \ gzip >$BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz You could have it check first and only do the mkdir if the directory didn't already exist: [[ -d $BACKUP_DIR/$PGyy/$PGmm/$PGdd ]] || \ mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 20:06 ` Todd Goodman @ 2013-05-05 21:25 ` Neil Bothwick 2013-05-05 21:56 ` Tanstaafl 0 siblings, 1 reply; 16+ messages in thread From: Neil Bothwick @ 2013-05-05 21:25 UTC (permalink / raw To: gentoo-user [-- Attachment #1: Type: text/plain, Size: 629 bytes --] On Sun, 5 May 2013 16:06:45 -0400, Todd Goodman wrote: > mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd > /usr/bin/pg_dumpall -U $PGUSER -o | \ > gzip >$BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz > > You could have it check first and only do the mkdir if the directory > didn't already exist: > > [[ -d $BACKUP_DIR/$PGyy/$PGmm/$PGdd ]] || \ > mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd You could, but it is redundant as mkdir already does that test when invoked with -p. -- Neil Bothwick Never argue with an idiot. First, they bring you down to their level. Then they beat you with experience. [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 198 bytes --] ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 21:25 ` Neil Bothwick @ 2013-05-05 21:56 ` Tanstaafl 2013-05-05 23:21 ` Tanstaafl 2013-05-06 3:25 ` Pandu Poluan 0 siblings, 2 replies; 16+ messages in thread From: Tanstaafl @ 2013-05-05 21:56 UTC (permalink / raw To: gentoo-user On 2013-05-05 5:25 PM, Neil Bothwick <neil@digimed.co.uk> wrote: > On Sun, 5 May 2013 16:06:45 -0400, Todd Goodman wrote: > >> mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd >> /usr/bin/pg_dumpall -U $PGUSER -o | \ >> gzip >$BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz >> >> You could have it check first and only do the mkdir if the directory >> didn't already exist: >> >> [[ -d $BACKUP_DIR/$PGyy/$PGmm/$PGdd ]] || \ >> mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd > > You could, but it is redundant as mkdir already does that test when > invoked with -p. Many thanks for the short noob bash scripting tutorial guys. Obviously I'm very new to it. I did confirm that there was no need to test (thanks Neil, this will come in handy I'm sure), but I also decided to uncomplicate it and just dump all fo the backups in a single directory. I realized this isn't like my email backups where I will be keeping years of them. I'll probably only keep 30 or so. So, my final script looks like this: #!/bin/bash BACKUP_DIR="/home/user/mypg_backups" PGUSER="superuser" PGyy=`date '+%Y'` PGmm=`date '+%m'` PGdd=`date '+%d_'` PGtt=`date '+%H:%M'` /usr/bin/pg_dumpall -U $PGUSER -o -f $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz For some reason, if I put the underscore in the filename itself, ie: $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd_$PGtt.gz It omitted the $PGdd variable entirely. I had to add the underscore into the variable to get the output like I wanted. Weird... Anyway, this is working perfectly, thanks guys. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 21:56 ` Tanstaafl @ 2013-05-05 23:21 ` Tanstaafl 2013-05-06 0:09 ` Volker Armin Hemmann 2013-05-06 9:48 ` Neil Bothwick 2013-05-06 3:25 ` Pandu Poluan 1 sibling, 2 replies; 16+ messages in thread From: Tanstaafl @ 2013-05-05 23:21 UTC (permalink / raw To: gentoo-user Last question... In order to keep only a certain number of backups, what would be the easiest and SAFEST way to delete the older ones? For example, I want to keep 17 hourlies, and 30 nightlies, so I have two cron jobs set up, the hourly, and the nightly. Each backs up to a separate dir. I'm thinking the easiest way would be to find and delete the oldest file in the backup target directory before executing the backup command. For the hourlies dir, I'd just find the files that are older than one day - so maybe: find $BACKUP_DIR/hourly* -type f -mtime +1 -delete Would that do it? So, in my script, I could add this in like: #!/bin/bash BACKUP_DIR="/home/user/mypg_backups/hourly" PGUSER="superuser" PGyy=`date '+%Y'` PGmm=`date '+%m'` PGdd=`date '+%d_'` PGtt=`date '+%H:%M'` find $BACKUP_DIR* -type f -mtime +1 -delete /usr/bin/pg_dumpall -U $PGUSER -o -f $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz and for the nightly backup script, since I want to keep 30 days worth: #!/bin/bash BACKUP_DIR="/home/user/mypg_backups/nightly" PGUSER="superuser" PGyy=`date '+%Y'` PGmm=`date '+%m'` PGdd=`date '+%d_'` PGtt=`date '+%H:%M'` find $BACKUP_DIR* -type f -mtime +30 -delete /usr/bin/pg_dumpall -U $PGUSER -o -f $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz Am I asking for trouble doing it this way? On 2013-05-05 5:56 PM, Tanstaafl <tanstaafl@libertytrek.org> wrote: > So, my final script looks like this: > > #!/bin/bash > BACKUP_DIR="/home/user/mypg_backups" > PGUSER="superuser" > PGyy=`date '+%Y'` > PGmm=`date '+%m'` > PGdd=`date '+%d_'` > PGtt=`date '+%H:%M'` > /usr/bin/pg_dumpall -U $PGUSER -o -f > $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz > > For some reason, if I put the underscore in the filename itself, ie: > > $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd_$PGtt.gz > > It omitted the $PGdd variable entirely. > > I had to add the underscore into the variable to get the output like I > wanted. Weird... > > Anyway, this is working perfectly, thanks guys. > ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 23:21 ` Tanstaafl @ 2013-05-06 0:09 ` Volker Armin Hemmann 2013-05-06 9:48 ` Neil Bothwick 1 sibling, 0 replies; 16+ messages in thread From: Volker Armin Hemmann @ 2013-05-06 0:09 UTC (permalink / raw To: gentoo-user Am 06.05.2013 01:21, schrieb Tanstaafl: > Last question... > > In order to keep only a certain number of backups, what would be the > easiest and SAFEST way to delete the older ones? > > For example, I want to keep 17 hourlies, and 30 nightlies, so I have > two cron jobs set up, the hourly, and the nightly. Each backs up to a > separate dir. > > I'm thinking the easiest way would be to find and delete the oldest > file in the backup target directory before executing the backup command. > > For the hourlies dir, I'd just find the files that are older than one > day - so maybe: > > find $BACKUP_DIR/hourly* -type f -mtime +1 -delete > > Would that do it? > > So, in my script, I could add this in like: > > #!/bin/bash > BACKUP_DIR="/home/user/mypg_backups/hourly" > PGUSER="superuser" > PGyy=`date '+%Y'` > PGmm=`date '+%m'` > PGdd=`date '+%d_'` > PGtt=`date '+%H:%M'` > find $BACKUP_DIR* -type f -mtime +1 -delete > /usr/bin/pg_dumpall -U $PGUSER -o -f > $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz > > and for the nightly backup script, since I want to keep 30 days worth: > > #!/bin/bash > BACKUP_DIR="/home/user/mypg_backups/nightly" > PGUSER="superuser" > PGyy=`date '+%Y'` > PGmm=`date '+%m'` > PGdd=`date '+%d_'` > PGtt=`date '+%H:%M'` > find $BACKUP_DIR* -type f -mtime +30 -delete > /usr/bin/pg_dumpall -U $PGUSER -o -f > $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz > > Am I asking for trouble doing it this way? > > On 2013-05-05 5:56 PM, Tanstaafl <tanstaafl@libertytrek.org> wrote: >> So, my final script looks like this: >> >> #!/bin/bash >> BACKUP_DIR="/home/user/mypg_backups" >> PGUSER="superuser" >> PGyy=`date '+%Y'` >> PGmm=`date '+%m'` >> PGdd=`date '+%d_'` >> PGtt=`date '+%H:%M'` >> /usr/bin/pg_dumpall -U $PGUSER -o -f >> $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz >> >> For some reason, if I put the underscore in the filename itself, ie: >> >> $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd_$PGtt.gz >> >> It omitted the $PGdd variable entirely. >> >> I had to add the underscore into the variable to get the output like I >> wanted. Weird... >> >> Anyway, this is working perfectly, thanks guys. >> > > > and then your backup scripts fails without you noticing and you just removed all your backups. oops. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 23:21 ` Tanstaafl 2013-05-06 0:09 ` Volker Armin Hemmann @ 2013-05-06 9:48 ` Neil Bothwick 1 sibling, 0 replies; 16+ messages in thread From: Neil Bothwick @ 2013-05-06 9:48 UTC (permalink / raw To: gentoo-user [-- Attachment #1: Type: text/plain, Size: 838 bytes --] On Sun, 05 May 2013 19:21:18 -0400, Tanstaafl wrote: > For example, I want to keep 17 hourlies, and 30 nightlies, so I have > two cron jobs set up, the hourly, and the nightly. Each backs up to a > separate dir. So each time your backup fails, you reduce the number of available backups by one. If you want to use this approach, delete one backup AFTER you have created AND tested a new backup. > I'm thinking the easiest way would be to find and delete the oldest > file in the backup target directory before executing the backup command. > > For the hourlies dir, I'd just find the files that are older than one > day - so maybe: What about rm -f $(ls -1t backuppattern* | tail -n +numbertokeep) -- Neil Bothwick The Computer is the logical advancement of humankind: intelligence without morality. [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 198 bytes --] ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 21:56 ` Tanstaafl 2013-05-05 23:21 ` Tanstaafl @ 2013-05-06 3:25 ` Pandu Poluan 2013-05-11 16:51 ` SOLVED - WAS " Tanstaafl 1 sibling, 1 reply; 16+ messages in thread From: Pandu Poluan @ 2013-05-06 3:25 UTC (permalink / raw To: gentoo-user [-- Attachment #1: Type: text/plain, Size: 2406 bytes --] On May 6, 2013 4:57 AM, "Tanstaafl" <tanstaafl@libertytrek.org> wrote: > > On 2013-05-05 5:25 PM, Neil Bothwick <neil@digimed.co.uk> wrote: >> >> On Sun, 5 May 2013 16:06:45 -0400, Todd Goodman wrote: >> >>> mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd >>> /usr/bin/pg_dumpall -U $PGUSER -o | \ >>> gzip >$BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz >>> >>> You could have it check first and only do the mkdir if the directory >>> didn't already exist: >>> >>> [[ -d $BACKUP_DIR/$PGyy/$PGmm/$PGdd ]] || \ >>> mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd >> >> >> You could, but it is redundant as mkdir already does that test when >> invoked with -p. > > > Many thanks for the short noob bash scripting tutorial guys. Obviously I'm very new to it. > > I did confirm that there was no need to test (thanks Neil, this will come in handy I'm sure), but I also decided to uncomplicate it and just dump all fo the backups in a single directory. I realized this isn't like my email backups where I will be keeping years of them. I'll probably only keep 30 or so. > > So, my final script looks like this: > > > #!/bin/bash > BACKUP_DIR="/home/user/mypg_backups" > PGUSER="superuser" > PGyy=`date '+%Y'` > PGmm=`date '+%m'` > PGdd=`date '+%d_'` > PGtt=`date '+%H:%M'` > /usr/bin/pg_dumpall -U $PGUSER -o -f $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz > > For some reason, if I put the underscore in the filename itself, ie: > > $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd_$PGtt.gz > > It omitted the $PGdd variable entirely. > > I had to add the underscore into the variable to get the output like I wanted. Weird... > > Anyway, this is working perfectly, thanks guys. > Totally unweird. In bash, underscores can be part of a variable name. $PGdd_$PGtt involves two variables: $PGdd_ (note the trailing underscore) and $PGtt You should write it like this: ${PGdd}_$PGtt The { } syntax is bash's way to indicate what exactly constitutes a variable name. IOW, the above construct has three parts: the variable $PGdd, an underscore, and the variable $PGtt Whenever you want to 'run' a variable name (i.e., combine it with other characters without specifying a whitespace), you should always use braces { } around the variable name. That said, since you're no longer creating a directory structure, why don't you just output everything using a single 'date' command? E.g. : date +'mypg-%Y-%m-%d_%H:%M' Rgds, -- [-- Attachment #2: Type: text/html, Size: 3176 bytes --] ^ permalink raw reply [flat|nested] 16+ messages in thread
* SOLVED - WAS Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-06 3:25 ` Pandu Poluan @ 2013-05-11 16:51 ` Tanstaafl 0 siblings, 0 replies; 16+ messages in thread From: Tanstaafl @ 2013-05-11 16:51 UTC (permalink / raw To: gentoo-user Just closing out this thread... Thanks to your and Neill and others' help, I've settled on this for my nightly job, that also deletes the oldest hourly's and daily over a certain number to keep: #!/bin/bash BACKUP_DIR="/my/backups/app/sql/nightly" BACKUP_DIR_hourly="/my/backups/app/sql/hourly" PGUSER="SuperUser" PG_my_app=`date '+%Y-%m-%d_%H:%M-app'` /usr/bin/pg_dumpall -U $PGUSER -o -f $BACKUP_DIR/$PG_my_app-sql.gz rm -f $(ls -1t $BACKUP_DIR/* | tail -n +91) rm -f $(ls -1t $BACKUP_DIR_hourly/* | tail -n +36) I'd like to learn how to do proper error detection using if/fi branching, but that will be a lesson for another day. I'll be monitoring these on a daily basis, so will hopefully catch any real problems before they cause any real problems. Thanks again to all who helped me get these functional. On 2013-05-05 11:25 PM, Pandu Poluan <pandu@poluan.info> wrote: > In bash, underscores can be part of a variable name. > > $PGdd_$PGtt involves two variables: $PGdd_ (note the trailing > underscore) and $PGtt > > You should write it like this: > > ${PGdd}_$PGtt > > The { } syntax is bash's way to indicate what exactly constitutes a > variable name. IOW, the above construct has three parts: the variable > $PGdd, an underscore, and the variable $PGtt > > Whenever you want to 'run' a variable name (i.e., combine it with other > characters without specifying a whitespace), you should always use > braces { } around the variable name. > > That said, since you're no longer creating a directory structure, why > don't you just output everything using a single 'date' command? E.g. : > > date +'mypg-%Y-%m-%d_%H:%M' > ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Using date/time variables in cronjobs? 2013-05-05 18:31 ` Tanstaafl 2013-05-05 20:06 ` Todd Goodman @ 2013-05-05 20:17 ` Alan McKinnon 1 sibling, 0 replies; 16+ messages in thread From: Alan McKinnon @ 2013-05-05 20:17 UTC (permalink / raw To: gentoo-user On 05/05/2013 20:31, Tanstaafl wrote: > On 2013-05-05 2:18 PM, Neil Bothwick <neil@digimed.co.uk> wrote:> On > Sun, 05 May 2013 14:07:50 -0400, Tanstaafl wrote: >>> /home/user/mypg_backups/2013/May/Sun/pg_all-13:54.gz: No such file or >>> directory >>> >>> So, it is expanding the variables properly, but apparently won't >>> automatically create the directories? Is there some kind of flag >>> I can add to the command to do that? > >> mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd/ > > Thanks Neill... > > Tried changing the command in the script to: > >> /usr/bin/pg_dumpall -U $PGUSER -o | gzip > mkdir -p >> $BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz That can never possibly work. Look at it carefully and see what it does: You pipe pg_dumpall to gzip and redirect that to a FILE called gzip. I hope you don't run that as root in /bin/! Anyway, what is the shell supposed to do with the -p $BACKUP.... ? What you want cannot be done in one step. You must do it this way (in pseudocode) if dir does not exist create dir write output to file in that dir In real bash test -d $BACKUP_DIR/$PGyy/$PGmm/$PGdd/ || mkdir -p $BACKUP_DIR/$PGyy/$PGmm/$PGdd/ /usr/bin/pg_dumpall -U $PGUSER -o -f $BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz I deliberately used the less common "test" command for readability rather than what you will usually find in practice "[[" > > and got this error: > > # ./ecat_pgdump.sh > gzip: invalid option -- 'p' > > Tried putting quotes around it like this: > >> /usr/bin/pg_dumpall -U $PGUSER -o | gzip > "mkdir -p >> $BACKUP_DIR/$PGyy/$PGmm/$PGdd/pg_all-$PGtt.gz" > > and got the original error with the added 'mkdir -p': > > # ./mypg_pgdumpall.sh > ./mypg_pgdumpall.sh: line 10: mkdir -p > /home/user/mypg_backups/2013/May/Sun/ecat-14:26.gz: No such file or > directory > > I'm guessing I just need to know where to put the quotes? No, you need to learn basic script techniques and how bash works wrt to redirection and piping. I recommend a rather famous tutorial - Bash Scripting Guide. Last I looked it was available on tldp.org, but Google knows all the places it is -- Alan McKinnon alan.mckinnon@gmail.com ^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2013-05-11 16:52 UTC | newest] Thread overview: 16+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2013-05-05 16:56 [gentoo-user] Using date/time variables in cronjobs? Tanstaafl 2013-05-05 17:07 ` Mick 2013-05-05 17:32 ` Tanstaafl 2013-05-05 17:49 ` Alan McKinnon 2013-05-05 18:07 ` Tanstaafl 2013-05-05 18:18 ` Neil Bothwick 2013-05-05 18:31 ` Tanstaafl 2013-05-05 20:06 ` Todd Goodman 2013-05-05 21:25 ` Neil Bothwick 2013-05-05 21:56 ` Tanstaafl 2013-05-05 23:21 ` Tanstaafl 2013-05-06 0:09 ` Volker Armin Hemmann 2013-05-06 9:48 ` Neil Bothwick 2013-05-06 3:25 ` Pandu Poluan 2013-05-11 16:51 ` SOLVED - WAS " Tanstaafl 2013-05-05 20:17 ` Alan McKinnon
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox