5 Replies Latest reply: Feb 21, 2014 12:43 PM by jh_ellis RSS

    Cleaning up backup files on Ubuntu

    jh_ellis

      We have a RHEL Linux server running two programs whose backups get copied over to another server running Ubuntu Linux. There is a "cleanup" script running in a cron job that is supposed to delete these backups that are more than ten days old but it is not working. In fact I can't get it to work even running it manually. Script is below-

       

       

      #!/bin/bash

      # bash shell script to cleanup jira backup files

      # The script will remove files that are 10 days or older

      #

      # Remove jira backup files

      for FILE in `find /usr/backup/jira -mtime +10`

        do echo $FILE

        /bin/rm -f $FILE 2>/dev/null

      done

       

      When I run this script at the command prompt- ./cleanup_jira.sh nothing happens; I just move down to the command prompt. I have root owning all the folders and files involved. This was all setup years ago to run by a different user, but apparently has not been working for quite awhile so I modified it all to be run as root. I do not get a mail message to root either when I try to run it.

       

      Any ideas please respond.

      Thanks for your time,

      John Ellis

        • 1. Re: Cleaning up backup files on Ubuntu
          Zoltan Kecskemethy

          Hi,

           

          find linux command can execute rm itself. You don't need to add in an extra cycle into your script.

          e.g.

          find /usr/backup/jira -mtime +10 -exec rm {} \;

           

          Or even you can use the -delete feature of it

          e.g.

          find /usr/backup/jira -mtime +10 -delete

           

          In this way you dont need a script at all you can simple add in find command into cron.

           

          HTH, Zoltan

          • 2. Re: Cleaning up backup files on Ubuntu
            Catch 22

            I second the use of the find command outlined in the previous response. However, it does not explain why the command or script does not work.


            What information do you get when you list the files in /usr/backup/jira in regard to modification date. You could also use the stat command for more info.


            What about the find command itself. Is /usr/bin/find the original file?

            • 3. Re: Cleaning up backup files on Ubuntu
              jh_ellis

              Well I'm embarrassed to say that I figured out the problem; I had copied a couple of the backup files over to my PC for testing purposes and then when I copied them back over it updated the 'Last Modified" date & time so the script didn't see them as being as old as they actually were. Thanks for the replies and better script and suggestions! My only defense is that I am not a scripter and had very little linux experience until I took this job. My Bad

              • 4. Re: Cleaning up backup files on Ubuntu
                EdStevens

                1456439 wrote:

                 

                Well I'm embarrassed to say that I figured out the problem; I had copied a couple of the backup files over to my PC for testing purposes and then when I copied them back over it updated the 'Last Modified" date & time so the script didn't see them as being as old as they actually were. Thanks for the replies and better script and suggestions! My only defense is that I am not a scripter and had very little linux experience until I took this job. My Bad

                FYI, when copying a file you can preserve the timestamp by use of the '-p' switch on the 'cp' command:

                 

                cp -p  source_file dest_file

                • 5. Re: Cleaning up backup files on Ubuntu
                  jh_ellis

                  Thanks for the tip Ed.