[TriLUG] Some script help

Todd Edwards via TriLUG trilug at trilug.org
Fri May 8 14:02:11 EDT 2015


You can easily enough compare dates in bash if you convert to epoch such as:

$ date -d $(echo 'backup_2014-06-13_7-00-07_14.1R1.tgz' | cut -d'_' -f2) +%s

1402632000


You can then use standard comparison operators to compare the dates.
http://www.tldp.org/LDP/abs/html/comparison-ops.html


Then you can convert epoch back to a normal time stamp in the format of
your choosing:

tedwards at SNIP ~$ date -d @$(date -d $(echo
'backup_2014-06-13_7-00-07_14.1R1.tgz' | cut -d'_' -f2) +%s)

Fri Jun 13 00:00:00 EDT 2014

tedwards at SNIP ~$ date -d @1402632000

Fri Jun 13 00:00:00 EDT 2014

tedwards at SNIP ~$ date -d @1402632000 +%Y_%m_%d

2014_06_13

On Fri, May 8, 2015 at 11:23 AM, Aaron Morrison via TriLUG <
trilug at trilug.org> wrote:

> Bash is fine for this, but the key is to not try to compare dates. Bash
> doesn't do that well at all.
>
> My suggestion is to let "find" do the purge work for you and then
> copy/sync what's left.
>
> find /dir/path -type f -name 'backup*' -time +7 -delete
>
> That will delete all files in /dir/path starting with "backup" that is
> older than 7 days.
>
> Simple is better (and easier to maintain).
>
> --am
>
>
> > On May 8, 2015, at 11:07, Igor Partola via TriLUG <trilug at trilug.org>
> wrote:
> >
> > +1 for not doing this in bash. While it's of course possible, Python
> would
> > be my choice. There you can compare dates as dates, not as sorted
> strings.
> >
> > Another approach: use logrotate to do this. I have seen this used
> > successfully to take backups from MySQL and keeping X number of days'
> worth
> > of backups.
> >
> > Third approach: forget copying "today's file" and copy the latest file.
> Use
> > `ls backup_*.tgz | sort | tail -n 1` to get the latest file.
> >
> > Fourth approach: use rsync and copy everything. Combine with third
> approach
> > to first delete the local backups that are older than 7 days.
> >
> > Igor
> > --
> > This message was sent to: Aaron Morrison <ae4ko1 at gmail.com>
> > To unsubscribe, send a blank message to trilug-leave at trilug.org from
> that address.
> > TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
> > Unsubscribe or edit options on the web    :
> http://www.trilug.org/mailman/options/trilug/ae4ko1%40gmail.com
> > Welcome to TriLUG: http://trilug.org/welcome
> --
> This message was sent to: Todd Edwards <todd at mtn.cc>
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that
> address.
> TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web  :
> http://www.trilug.org/mailman/options/trilug/todd%40mtn.cc
> Welcome to TriLUG: http://trilug.org/welcome
>


More information about the TriLUG mailing list