imperialWicket

am i the only croquet-playing computer nerd?

« WordPress 3.1 temporarily integrates Django? Open Source Participation: Documentation is a good first hurdle »

The Cleaner: A Bash Script to Delete Old Backup Files

2011-02-11

I wrote this backup directory cleaner a while ago, and I use it quite often. When I originally looked around for a solution, I could not find any existent scripts that really suited my needs. Cleaning directories is a pretty straight-forward need, and I assume that most people have something like this sitting around or just write something a little simpler whenever they need it. Nonetheless, since I put this together, and I use it regularly, I thought I would make it available here.

This script basically lets you choose a string to match, and delete the files matching that string until only a certain number are left. There is also a -q (quiet) flag, so that you can use the script in crontab (the default is verbose AND requires user confirmation). You can also pass it a directory from which to delete files; the default directory is the current working directory.

You can see some examples and read a little bit more in the help in the file contents, or by executing:

./cleaner.sh -h

cleaner.sh

TODO - add a -l flag that takes a parameter. "-l" indicates that you want to log data, and the parameter is the file in which logs are added. This allows users with more abstract cleaning efforts to log periodically delete and keep a log of how many files were deleted and what parameters were used (or something to that effect). Most of my cleaning needs involved daily backups of files or databases, and so the logs would be extremely predictable. But I can see a use for this, and may update the script soon to accommodate this case.

I hope this saves someone a few minutes of searching and script authoring. Let me know if you have other needs that could be easily added, or alternative implementations that I should consider.

3 Responses to The Cleaner: A Bash Script to Delete Old Backup Files

Feed for this Entry

3 Comments

  • Hi!
    I'm trying to use your script but it has syntax errors in Usage function. I've copied the entire text into a .sh file, gave execute permissions and it fails with this error:

    beto@Betoserver:~/backups$ ./limpiar_backups.sh -h
    ./limpiar_backups.sh: línea 16: error sintáctico cerca del elemento inesperado `('
    ./limpiar_backups.sh: línea 16: `cat << eof="" cleaner.sh="" this="" script="" cleans="" directories.="" it="" is="" useful="" for="" backup="" and="" log="" file="" directories,="" when="" you="" want="" to="" delete="" older="" files.="" usage:="" [options]="" options:="" -h="" show="" message="" -q="" defaults="" verbose,="" use="" turn="" off="" messages="" (useful="" using="" the="" in="" automated="" scripts).="" -s="" a="" search="" string="" limit="" deletion,="" '*'="" (all="" files).="" -m="" minimum="" number="" of="" files="`ls" required="" directory="" (files="" be="" maintained),="" 5.="" -d="" clean,="" current="" directory.="" examples:="" directory,="" everything="" but="" 5="" most="" recently="" touched="" files:="" same="" as:="" *="" .="" /home/myuser="" all="" including="" text="" "test",="" except="" recent:="" test="" 1="" don't="" ask="" any="" confirmation:="" }="" #="" set="" default="" values="" vars="" search_string="*" min_files="5" dir="." quiet="0" deleted="0" cleaner="" function="" delete()="" {="" -1p="" "$search_string"*="" 2="">/dev/null | grep -vc "/$"`'
    ./limpiar_backups.sh: línea 88: aviso: el documento-aquí en la línea 16 está delimitado por fin-de-fichero (se esperaba `eof=')

    I'm trying to fix it but I don't know the syntax used in that function.

    Thanks!

    #8223 | Comment by Beto on Feb 29, 2012 01:05am
  • Error in english:

    limpiar_backups.sh: line 16: syntax error near unexpected token `('
    limpiar_backups.sh: line 16: `cat << eof="" cleaner.sh="" this="" script="" cleans="" directories.="" it="" is="" useful="" for="" backup="" and="" log="" file="" directories,="" when="" you="" want="" to="" delete="" older="" files.="" usage:="" [options]="" options:="" -h="" show="" message="" -q="" defaults="" verbose,="" use="" turn="" off="" messages="" (useful="" using="" the="" in="" automated="" scripts).="" -s="" a="" search="" string="" limit="" deletion,="" '*'="" (all="" files).="" -m="" minimum="" number="" of="" files="`ls" required="" directory="" (files="" be="" maintained),="" 5.="" -d="" clean,="" current="" directory.="" examples:="" directory,="" everything="" but="" 5="" most="" recently="" touched="" files:="" same="" as:="" *="" .="" /home/myuser="" all="" including="" text="" "test",="" except="" recent:="" test="" 1="" don't="" ask="" any="" confirmation:="" }="" #="" set="" default="" values="" vars="" search_string="*" min_files="5" dir="." quiet="0" deleted="0" cleaner="" function="" delete()="" {="" -1p="" "$search_string"*="" 2="">/dev/null | grep -vc "/$"`'
    limpiar_backups.sh: line 88: warning: here-document at line 16 delimited by end-of-file (wanted `eof=')

    #8224 | Comment by Beto on Feb 29, 2012 01:08am
  • @Beto - My styling was causing parsing issues with the double less than symbols. I changed these to escape characters, and you should be good to go. All of the ="" and stray parentheses were being generated by the browser when it saw "<<".>


About You

Email address is not published

Add to the Discussion

Search