1

I have a script that generates backup and finds files older than 30 days and compresses them. Now the problem I'm facing is if the files are more than one than how can I edit the script so that it compresses all those files as well. Below is the find function Im using. Please Help.

Time=+30
PATH_TO_DUMP=/home/tarun/Desktop/Backup

#Find any Backup File defined by the time constraint
file="$(find $PATH_TO_DUMP -type f -mtime $Time)"

#To verify if $file is empty or has some value
if [ ! -n "$file" ]; then
echo "No Earlier Backups were found to compress" >> $PATH_TO_LOG
else
echo "Earlier Backups $file will be compressed" >> $PATH_TO_LOG
gzip $file
fi
  • 1
    If I understand you correctly, you will have to use a loop. Look into 'for' loops. – matv1 Jun 28 '13 at 11:44
  • @matv1 : I tried to apply it but didnt worked Im not so in scripting can u please specify a bit more. Thanks. – Tarun Jun 28 '13 at 12:45
  • Can you post the full script? I think it will be usefull to all of us :) Ty in advance – user420919 Jun 17 '15 at 8:19
4

You can do that with a minor modification

(pipe the find output into while loop doing a read)

Time=+30
PATH_TO_DUMP=/home/tarun/Desktop/Backup
#Find any Backup File defined by the time constraint

find $PATH_TO_DUMP -type f -mtime $Time | while read file
do

  #To verify if $file is empty or has some value
  if [ ! -n "$file" ]; then
    echo "No Earlier Backups were found to compress" >> $PATH_TO_LOG
  else
    echo "Earlier Backups $file will be compressed" >> $PATH_TO_LOG
    gzip "$file"
  fi

done
| improve this answer | |
  • xargs solution is also a "one liner" option find $PATH_TO_DUMP -type f -mtime $Time | xargs -tn1 gzip (-tn1 because you seems to like verbose scripts :) – Emmanuel Jul 1 '13 at 12:49
  • Ok. Thanks Emmanuel. I found your script is working perfectly but after it has compressed files in .gz format in other turn it logs those files again for compression(It doesnt does that). I want to know how can I change the find parameter to exclude .gz file. – Tarun Jul 2 '13 at 4:53
  • 1
    @Tarun ok then try find $PATH_TO_DUMP -type f -mtime $Time ! -name \*gz | xargs -tn1 gzip or that one to avoid any compressed files find $PATH_TO_DUMP -type f -mtime $Time | egrep -v "\.(zip|gz|tgz|rar)$" | xargs -tn1 gzip – Emmanuel Jul 2 '13 at 8:43
2

Compress all the file with tar instead of gzip:

tar jcvf new_file.tar.bz2 $file

tar stores file
-j to bzip the tarred file (-z use gzip)
-c to create (-x to extract )
-v verbose
-f file to create ( or to open )

To extract:

tar jxvf new_file.tar.bz2

Otherwise, if you want an archive for every file:

echo $file | xargs gzip

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial- arguments followed by items read from standard input.

| improve this answer | |
  • I dont think tar will be useful because it will create a new zipped file I used gzip because it zips the original file so not makes any new compressed copies of it. – Tarun Jun 28 '13 at 14:00
  • @Tarun Then delete the uncompressed files (and exclude the tar.bz2 from a new archive: --exclude=$PATH_TO_DUMP/*.tar.bz2) – asclepix Jun 28 '13 at 14:16
  • Ok thats fine but will it be able to compress multiple files as multiple files like file1 to file1.tar.bz2 and file2 to file2.tar.bz2 in that situation. – Tarun Jun 28 '13 at 14:51
  • I thought you want only one archive. However, I've modify my answer – asclepix Jun 28 '13 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.