kozlone
kozlone

Reputation: 111

Remove archived files

I need to remove files that older than given in a parameter and add them to the archive with saving the structure.

file listing

oracle@dbi-702D-6x:~/test.d$ ls -l
total 20
-rw-rw-r-- 1 oracle oracle    0 Sep  6 07:06 aug
-rw-rw-r-- 1 oracle oracle    0 Feb 10  2015 feb
-rw-rw-r-- 1 oracle oracle    0 Sep  6 07:06 june
-rw-rw-r-- 1 oracle oracle    0 Mar  3  2015 march
-rw-rw-r-- 1 oracle oracle    0 Sep  6 07:06 may
drwxrwxr-x 3 oracle oracle 4096 Sep  6 08:00 rem-old-res
-rwxrw-r-- 1 oracle oracle  469 Sep  6 08:00 rem-old.sh
-rwxrw-r-- 1 oracle oracle  467 Sep  6 08:00 rem-old.sh~
-rw-rw-r-- 1 oracle oracle   85 Sep  6 08:00 roll-back.sh
drwxrwxr-x 2 oracle oracle 4096 Sep  6 07:35 some.d

oracle@dbi-702D-6x:~/test.d/some.d$ ls -l
total 0
-rw-rw-r-- 1 oracle oracle 0 Sep  6 07:06 test1
-rw-rw-r-- 1 oracle oracle 0 Sep  6 07:06 test2
-rw-rw-r-- 1 oracle oracle 0 Jan  4  2015 test3

run-on.sh

#!/bin/bash

dirname=$2
date=$1
now=`date +%Y-%m-%d`
here=`pwd`

mkdir $HOME/rem-old-res
touch temp-file -d $date 

cp * -R $HOME/rem-old-res
cp -R $HOME/rem-old-res/ $here/rem-old-res

for file in `find rem-old-res -type f -newer temp-file`
do
    rm $file
done

tar czf rem-old-res.tar.gz rem-old-res 

echo "#!/bin/bash" > $here/roll-back.sh
echo "tar xvzf $HOME/rem-old-res.tar.gz $here/rem-old-res" >> $here/roll-back.sh


rm -rf $HOME/rem-old-res
rm $here/temp-file

script is called like :

./run-on.sh 20150501

Feb, march and test3 files are has to be removed by the script, but they don't. I can't get why for loop doesn't work properly.

Upvotes: 0

Views: 60

Answers (2)

kozlone
kozlone

Reputation: 111

Here is the solution of my problem:

#!/bin/bash

# example of usage ./rem-old.sh 20150501 .
# The first parametr is a date in the format YYMMDD and the second is a path to the particular direcoty 

dirname=$2
date=$1
now=`date +%Y-%m-%d`
here=`pwd`

touch temp-file -d $date 

for file in `find -type f ! -newer temp-file`
do  
    if [[ $file != "./temp-file" ]]
    then
        tar --append --file=rem-old-res.tar $file
        rm $file
    fi
done

gzip rem-old-res.tar

# roll-back.sh is a script to cancel the changes
echo "#!/bin/bash" > $here/roll-back.sh
echo "gzip -d rem-old-res.tar.gz" >> $here/roll-back.sh
echo "tar -xvf rem-old-res.tar" >> $here/roll-back.sh
echo "rm rem-old-res.tar" >> $here/roll-back.sh
chmod u+x roll-back.sh

rm $here/temp-file

Upvotes: 0

Oliver Friedrich
Oliver Friedrich

Reputation: 9240

I think you are pretty much on a good way, but you did not read the man-pages good enough.

tar has a parameter to remove the files from disk that are added to the archive.

touch uses the -t parameter for setting the file time.

Additionally, you should beware of using variables in bash, see pitfalls always use quotes

#!/bin/bash

dirname=$2
date=$1
now=`date +%Y%m%d%H%M`
here=`pwd`

touch -t "$date" temp-file
find "$here" -newer temp-file tarfiles 
tar -czf rem-old-res.tar.gz -T tarfiles --remove-files

echo "#!/bin/bash" > $here/roll-back.sh
echo "tar xvzf $HOME/rem-old-res.tar.gz $here/rem-old-res" >> $here/roll-back.sh

rm $here/temp-file
rm $here/tarfiles

Upvotes: 1

Related Questions