/ / Usuń zarchiwizowane pliki - bash, tar

Usuń zarchiwizowane pliki - bash, tar

Muszę usunąć pliki starsze niż podane w parametrze i dodać je do archiwum z zachowaniem struktury.

lista plików

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

skrypt nazywa się jak:

./run-on.sh 20150501

Pliki Feb, March i Test3 muszą zostać usunięte przez skrypt, ale nie potrafią zrozumieć, dlaczego pętla nie działa poprawnie.

Odpowiedzi:

1 dla odpowiedzi № 1

Myślę, że jesteś na dobrej drodze, ale nie przeczytałeś wystarczająco dobrych stron.

tar ma parametr do usunięcia plików z dysku, które są dodawane do archiwum.

touch używa parametru -t do ustawienia czasu pliku.

Dodatkowo powinieneś wystrzegać się używania zmiennych w bashu, zobacz pułapki zawsze używaj cytatów

#!/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

0 dla odpowiedzi nr 2

Oto rozwiązanie mojego problemu:

#!/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