Find the answer to your Linux question:
Results 1 to 7 of 7
Hi Guys We have a folder (or set of different folders which have a file structure as the following Code: 2013_09_02_* 2013_08_31_* 2013_08_30_* ........ 2013_07_30_* ....... 2012_11_27_* Now what we ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    its
    its is offline
    Just Joined!
    Join Date
    Oct 2010
    Posts
    9

    Unhappy Query on writing a script to manage backups


    Hi Guys

    We have a folder (or set of different folders which have a file structure as the following

    Code:
    2013_09_02_*
    2013_08_31_*
    2013_08_30_*
    ........
    2013_07_30_*
    .......
    2012_11_27_*
    Now what we want to achieve is probably write such a script which deletes these backups on such criteria.. Probably can be run only once a month

    1) Keep all files for current and last month

    Code:
    2013_09_02_*
    2013_08_31_*
    2013_08_30_*
    .....
    2013_08_01_*
    2) Anything before that.. keeps only the 10th 20th 30th of the month file and deletes the rest

    Code:
    2013_07_30_*
    2013_07_20_*
    2013_07_10_*
    2013_06_30_*
    .....
    2012_11_30_*
    2012_11_20_*

    Any ideas guys.. not good at coding..

    Thanks

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    Code:
    find "${dir:-.}" -type f ! \( -iname "$(date -d -1month +'%Y_%m_*')" -o -iname "$(date +%Y_%m_*)" -o -regextype  posix-extended -regex ".*/[0-9]{4}_[0-9]{2}_(1|2|3)0" \) -exec rm {} \+
    read the man page to know what does what.

  3. #3
    its
    its is offline
    Just Joined!
    Join Date
    Oct 2010
    Posts
    9
    Quote Originally Posted by watael View Post
    Code:
    find "${dir:-.}" -type f ! \( -iname "$(date -d -1month +'%Y_%m_*')" -o -iname "$(date +%Y_%m_*)" -o -regextype  posix-extended -regex ".*/[0-9]{4}_[0-9]{2}_(1|2|3)0" \) -exec rm {} \+
    read the man page to know what does what.
    Thanks.. I tried this on a temp directory by replacing "${dir:-.}" with . but it did nothing.. missing something?

  4. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    you need to elaborate the regex to fit your needs
    Code:
    $ fileList=( 2013_09_02_ 2013_08_31_ 2013_08_30_ 2013_07_30_ 2012_11_27_ 2013_07_30_ 2013_07_20_ 2013_07_10_ 2013_06_30_ 2012_11_30_ 2012_11_20_ )
    $ mkdir tempDir
    $ cd tempDir/
    $ touch "${fileList[@]}"
    $ find -type f ! \( -iname "$(date -d -1month +'%Y_%m_*')" -o -iname "$(date +%Y_%m_*)" -o -regextype  posix-extended -regex ".*[0-9]{4}_[0-9]{2}_(1|2|3)0.*" \) -exec echo rm {} \+
    rm ./2012_11_27_

  5. #5
    its
    its is offline
    Just Joined!
    Join Date
    Oct 2010
    Posts
    9
    Quote Originally Posted by watael View Post
    you need to elaborate the regex to fit your needs
    Code:
    $ fileList=( 2013_09_02_ 2013_08_31_ 2013_08_30_ 2013_07_30_ 2012_11_27_ 2013_07_30_ 2013_07_20_ 2013_07_10_ 2013_06_30_ 2012_11_30_ 2012_11_20_ )
    $ mkdir tempDir
    $ cd tempDir/
    $ touch "${fileList[@]}"
    $ find -type f ! \( -iname "$(date -d -1month +'%Y_%m_*')" -o -iname "$(date +%Y_%m_*)" -o -regextype  posix-extended -regex ".*[0-9]{4}_[0-9]{2}_(1|2|3)0.*" \) -exec echo rm {} \+
    rm ./2012_11_27_
    Thanks.. It works with your steps given above.. looking at regex it fits my need as well as far as I can see..

    But on my files it does not work.. they are in format

    Code:
    -rw-r--r-- 1 user user 1.8M Sep  2 13:03 2013_08_30_000000_1.ldif.gz
    -rw-r--r-- 1 user user  270 Sep  2 13:03 2013_08_30_000000_2.ldif.gz
    -rw-r--r-- 1 user user  54M Sep  2 13:03 2013_08_30_000000_3.ldif.gz
    -rw-r--r-- 1 user user 265K Sep  2 13:03 2013_08_31_000000_4.ldif.gz
    -rw-r--r-- 1 user user  92M Sep  2 13:03 2013_08_31_000000.ldif.gz
    -rw-r--r-- 1 user user 5.8M Sep  2 13:03 2013_08_31_000000_5.ldif.gz
    -rw-r--r-- 1 user user  32M Sep  2 13:03 2013_08_31_000000_6.ldif.gz
    -rw-r--r-- 1 user user 323K Sep  2 13:03 2013_09_01_000000_7.ldif.gz
    -rw-r--r-- 1 user user 7.2K Sep  2 13:03 2013_09_01_000000_8.ldif.gz
    -rw-r--r-- 1 user user  861 Sep  2 13:03 2013_08_31_000000_9.ldif.gz
    -rw-r--r-- 1 user user  238 Sep  2 13:03 2013_08_31_000000_10.ldif.gz
    .....

  6. #6
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    none of these files has to be deleted

    I've added one: «2012_06_26_000000_1.ldif.gz», and it appears correctly.

  7. #7
    its
    its is offline
    Just Joined!
    Join Date
    Oct 2010
    Posts
    9
    Quote Originally Posted by watael View Post
    none of these files has to be deleted

    I've added one: «2012_06_26_000000_1.ldif.gz», and it appears correctly.
    stupid stupid stupid.. my mistake.. i had already run the command without echo and it had worked.. just didn't notice..

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •