Bash script to archive files
Hi script experts,
# What to backup /interfaces/data/trace/in/* /interfaces/data/trace/out/* /interfaces/data/test/in/* /interfaces/data/test/out/* /interfaces/data/error/in/* /interfaces/data/error/out/* # Where to backup to. /interfaces/data/archive I need to tar those files/folders in those directories that older than 30days with file name as /interfaces/data/archive/$dir1_$dir2_$date.tar.gz where: dir1 = 3rd directory like /trace, /test, /error; dir2 = in or out; date = 20140829 format example of file: /interfaces/data/archive/trace_in_20140829.tar.gz Thank you guys. |
and what is your problem? http://www.linuxquestions.org/linux/...Ask_a_Question
|
Quote:
|
can you post your script? You need to construct the target directory from your variables using parameter substitution.
|
Well, I am very very new to scripting and maybe I need to learn more about for loop, if statement and etc. I just started to write the script on my own and there are a lot of tries and errors. Here is my script.
-------- #!/bin/bash # What to backup src1="/interfaces/data/trace/in/" src2="/interfaces/data/trace/out/" src3="/interfaces/data/test/in/" src4="/interfaces/data/test/out/" src5="/interfaces/data/error/in/" src6="/interfaces/data/error/out/" # Where to backup to. dest="/interfaces/data/archive" # Retention period max_days=30 # Create archive filename. date=$(date '+%Y''%m''%d') archive_file="$dir1_$dir2_$date.tar.gz" # Log file LOG_FILE="/interfaces/data/script/archive.$date.log" # Print start status message. echo "Backing up $src5 to $dest" date echo # Backup the files using tar. #cd $src5 #find . -mtime +$max_days | xargs tar cvzf $dest/error_in_$date.tar.gz # ---------- #for f in $src1 $src2 $src3 $src4 $src5 $src6 #do #ls -l $f # tar cvzf $dest/trace_in_$date.tar.gz $src1 #sleep 1 #done # ----------- #cd /interfaces/data/ #for i in * #do #[ -d "$i" ] && zip -r "$dest/$i.zip" "$i" #done # -------- #cd /interfaces/data/ for i in /interfaces/data/* do [ -d == "trace" ] #zip -r "$dest/$i.zip" "$i" #if [ "${f}" == "trace" && "${f}" == "test" && "${f}" == "error"] #if [ "$string" == "trace" ] #then #echo "I am in" ls -lh $i #zip -r "$dest/$string.zip" "$string" done # Print end status message. echo echo "Backup finished" date echo # Long listing of files in $dest to check file sizes. echo "Checking the file sizes in $dest" ls -lh $dest ------- The easy and simplest way that I could think of is go to each directory and tar it with hard coded .../error_in_$date.tar.gz as below: #cd $src5 #find . -mtime +$max_days | xargs tar cvzf $dest/error_in_$date.tar.gz Again, I am a beginner and my way of scripting is in beginner level as well. Maybe there is more flexible way that I am looking for now. Please advise guru :) |
as I told you you need to use parameter substitution. You can check it on the man page of bash.
Code:
#insert set -xv at the beginning to check what's happening |
new for me. Where should I insert full tar command? Could you please insert for me so that I can test it and hence see how your code manipulates the files.. This way I will learn your code.
|
I tried insert this code above "sleep 1" line:
find $f -mtime +$max_days | xargs tar cvzf $dest/$archive_file $f Am I doing correct way? However, the file created as 20140829.tar.gz. I untar the file, Here you go: # ll interfaces/ total 4 drwxrwxr-x 3 root root 4096 Aug 29 18:38 data # ll interfaces/data/ total 4 drwxrwxr-x 3 root root 4096 Aug 29 18:38 error # ll interfaces/data/error/ total 4 drwxrwxr-x 2 2037 sapsys 4096 Jul 25 08:48 out # ll interfaces/data/error/out/ total 224 -rw-rw---- 1 2031 sapsys 369 Jun 23 12:05 NDC900APF.NDC900APF_20140623_120526 -rw-rw---- 1 2031 sapsys 1107 Jun 25 09:20 NDC900APF.NDC900APF_20140625_092033 -rw-rw---- 1 2031 sapsys 1107 Jun 25 09:26 NDC900APF.NDC900APF_20140625_092658 -rw-rw---- 1 2031 sapsys 1107 Jun 25 10:14 NDC900APF.NDC900APF_20140625_101453 -rw-rw---- 1 2031 sapsys 1107 Jun 25 10:19 NDC900APF.NDC900APF_20140625_101954 |
you can try:
find $f -mtime +$max_days | tar cvzf $dest/$archive_file -T - |
Hi pan64,
I tried execute the script and this is what happened: + for f in '$src1' '$src2' '$src3' '$src4' '$src5' '$src6' + ls -l /interfaces/data/error/out/ total 224 ... files are here ... ... + dir2= + dir1=out + archive_file=20140902.tar.gz + find /interfaces/data/error/out/ -mtime +30 + tar cvzf /interfaces/data/archive/20140902.tar.gz -T - The loop for all the source directories are working fine. However, it looks like $dir2 could not retrieve the value. Maybe this is the reason why is the file created as 20140902.tar.gz. Actually what I need is: dir1=error dir2=out # but your code dir1=out archive_file=$dir1_$dir2_$date.tar.gz Some more only files in /interfaces/data/error/out are tar'ed. The rest of file are not despite they are more than 30 days old. |
actually you have not posted your script so I have no idea what is the reason. As I wrote you can put set -xv at the beginning of the script and you will see what's happening inside.
I can only guess the problem is that you put a final / at the end of the directories. You can remove it by: Code:
for f in $src1 $src2 $src3 $src4 $src5 $src6 |
Here is the script
Quote:
+ f=/interfaces/data/error/out + dname=/interfaces/data/error + dir2=out + dir1=error + archive_file=20140902.tar.gz + find /interfaces/data/error/out -mtime +30 + xargs tar cvzf /interfaces/data/archive/20140902.tar.gz -T - but the archive_file still 20140902.tar.gz. Once the script completed, I tar tvf 20140902.tar.gz to see the content, it backed up only interfaces/data/error/out. It should be more than /error. Dir /test/*, /trace/* have files that older than 30 days as well. |
to fix the archive name:
archive_file=${dir1}_${dir2}_$date.tar.gz |
Thanks. At least it works for the main part.
There is a small adjustment that I need to do as when I untar the archive file, the scripts actually tar'ed the whole/full dir like: interfaces/data/error/in It should only tar error/in, error/out etc. This is due to for loop for the variable $src1, $src2 etc. I will try to adjust it accordingly. |
in this command: find $f -mtime +$max_days | xargs tar cvzf $dest/$archive_file -T -
you should remove xargs, that is not required. It may cause errors. (see post #9) you may try the tar flag: --strip-components=NUMBER to remove leading directories (see man page) If you really want to say thanks just press YES... |
All times are GMT -5. The time now is 12:03 PM. |