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