Gentoo Archives: gentoo-user

From: Volker Armin Hemmann <volkerarmin@××××××××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Using date/time variables in cronjobs?
Date: Mon, 06 May 2013 00:10:16
Message-Id: 5186F4D5.50904@googlemail.com
In Reply to: Re: [gentoo-user] Using date/time variables in cronjobs? by Tanstaafl
1 Am 06.05.2013 01:21, schrieb Tanstaafl:
2 > Last question...
3 >
4 > In order to keep only a certain number of backups, what would be the
5 > easiest and SAFEST way to delete the older ones?
6 >
7 > For example, I want to keep 17 hourlies, and 30 nightlies, so I have
8 > two cron jobs set up, the hourly, and the nightly. Each backs up to a
9 > separate dir.
10 >
11 > I'm thinking the easiest way would be to find and delete the oldest
12 > file in the backup target directory before executing the backup command.
13 >
14 > For the hourlies dir, I'd just find the files that are older than one
15 > day - so maybe:
16 >
17 > find $BACKUP_DIR/hourly* -type f -mtime +1 -delete
18 >
19 > Would that do it?
20 >
21 > So, in my script, I could add this in like:
22 >
23 > #!/bin/bash
24 > BACKUP_DIR="/home/user/mypg_backups/hourly"
25 > PGUSER="superuser"
26 > PGyy=`date '+%Y'`
27 > PGmm=`date '+%m'`
28 > PGdd=`date '+%d_'`
29 > PGtt=`date '+%H:%M'`
30 > find $BACKUP_DIR* -type f -mtime +1 -delete
31 > /usr/bin/pg_dumpall -U $PGUSER -o -f
32 > $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz
33 >
34 > and for the nightly backup script, since I want to keep 30 days worth:
35 >
36 > #!/bin/bash
37 > BACKUP_DIR="/home/user/mypg_backups/nightly"
38 > PGUSER="superuser"
39 > PGyy=`date '+%Y'`
40 > PGmm=`date '+%m'`
41 > PGdd=`date '+%d_'`
42 > PGtt=`date '+%H:%M'`
43 > find $BACKUP_DIR* -type f -mtime +30 -delete
44 > /usr/bin/pg_dumpall -U $PGUSER -o -f
45 > $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz
46 >
47 > Am I asking for trouble doing it this way?
48 >
49 > On 2013-05-05 5:56 PM, Tanstaafl <tanstaafl@×××××××××××.org> wrote:
50 >> So, my final script looks like this:
51 >>
52 >> #!/bin/bash
53 >> BACKUP_DIR="/home/user/mypg_backups"
54 >> PGUSER="superuser"
55 >> PGyy=`date '+%Y'`
56 >> PGmm=`date '+%m'`
57 >> PGdd=`date '+%d_'`
58 >> PGtt=`date '+%H:%M'`
59 >> /usr/bin/pg_dumpall -U $PGUSER -o -f
60 >> $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd$PGtt.gz
61 >>
62 >> For some reason, if I put the underscore in the filename itself, ie:
63 >>
64 >> $BACKUP_DIR/mypg-$PGyy-$PGmm-$PGdd_$PGtt.gz
65 >>
66 >> It omitted the $PGdd variable entirely.
67 >>
68 >> I had to add the underscore into the variable to get the output like I
69 >> wanted. Weird...
70 >>
71 >> Anyway, this is working perfectly, thanks guys.
72 >>
73 >
74 >
75 >
76 and then your backup scripts fails without you noticing and you just
77 removed all your backups. oops.