0

I have a bash script which creates a mysqldump backup every hour in a certain directory.

The filenames of the backup files include the date and hour as per the following schema:

backupfile_<day>-<month>-<year>_<hour>.sql.gz

and to clarify here are some example filenames:

backupfile_30-05-2012_0800.sql.gz
backupfile_01-06-2012_0100.sql.gz
backupfile_05-06-2012_1500.sql.gz

Would someone help me with creating a script that will loop through all files in the directory and then delete files LEAVING the following:

  1. Keep alternate hour backups older than a day
  2. Keep twice daily backups older than a week
  3. Keep once daily backups older than a month.

I have the following beginnings of the script:

#!/bin/bash
cd /backup_dir

for file in *
do
    # do the magic to find out if this files time is up (i.e. needs to be deleted)
    # delete the file
done
Andy
  • 717
  • 1
  • 9
  • 24
  • You should use find utility, man find. – morphles Jun 06 '12 at 12:58
  • Possible duplicates: http://stackoverflow.com/questions/6099795/bash-script-to-find-old-files-based-off-date-in-file-name and http://stackoverflow.com/questions/3676810/bash-delete-based-on-file-date-stamp. – Todd A. Jacobs Jun 06 '12 at 13:11

2 Answers2

1

I have seen many fancy scripts like this for taking scheduled backups and wonder why folks don't make a use of logroate utility available on most of *nix distros available today support following options of your interest:

compress
     Old versions of log files are compressed with gzip by default.

dateext
     Archive old versions of log files adding a daily extension like YYYYMMDD instead
     of simply adding a number.

olddir directory
     Logs are moved into directory for rotation. The directory must be on the same
     physical device as the log file being rotated, and is assumed to be relative to
     the directory holding the log file unless an absolute path name is specified.
     When  this  option is used all old versions of the log end up in directory. This
     option may be overriden by the noolddir option.

notifempty
      Do not rotate the log if it is empty (this overrides the ifempty option).

postrotate/endscript
      The lines between postrotate and endscript (both of which must appear on lines by
      themselves)  are  executed  after the  log file is rotated. These directives may
      only appear inside of a log file definition.  See prerotate as well.
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • @CodeGnome: That's another misconception about logrotate that it requires root (or sudo) access. As a matter of fact it doesn't need that. I am using it for backup files rotation on very restrictive shared hosting environments. – anubhava Jun 06 '12 at 13:19
  • You're correct: logrotate *can* be run as an unpriveleged user by specifying `--state ` and an appropriate configuration file. – Todd A. Jacobs Jun 06 '12 at 13:25
0

You can parse your timestamps by iterating over filenames, or you can use the -cmin flag in the find command (see man 1 find for details).

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199