Tar archives monthly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tar archives monthly
# 1  
Old 07-12-2015
Tar archives monthly

Hi,

I want to archive files by month,
is there anyway of this code looks better?

Code:
find /tmp/w/ -type f   -newermt '2014-01-01' ! -newermt '2014-02-01' | xargs tar -czvf files01.tar 
find /tmp/w/ -type f   -newermt '2014-02-01' ! -newermt '2014-03-01' | xargs tar -czvf files02.tar 
find /tmp/w/ -type f   -newermt '2014-03-01' ! -newermt '2014-04-01' | xargs tar -czvf files03.tar

Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for multiline code samples.

Last edited by Don Cragun; 07-12-2015 at 04:37 PM.. Reason: Change ICODE tags to CODE tags.
# 2  
Old 07-12-2015
There are a couple of strange things with the command line:
Code:
find /tmp/w/ -type f   -newermt '2014-03-01' ! -newermt '2014-04-01' | xargs tar -czvf files03.tar

  1. By convention, a gzipped file should have .gz as its final suffix; not .tar.
  2. The reason for using xargs command (or find ... -exec command +) is to run the given command as many times as are needed to meet ARG_MAX limitations. But, if you invoke tar multiple times with the -c option, your output file will be recreated from scratch on each invocation (wiping out the data stored in the previous invocation). You could get around that problem if you remove your output file before you start and use tar -r instead of tar -c, but on many systems you can't use the -z option with -r.
If you use pax instead of tar to create the archive, you can feed it the names of files to be processed through its standard input instead of worrying about command line argument length limits. So you might want to try something more like:
Code:
find /tmp/w/ -type f -newermt '2014-03-01' ! -newermt '2014-04-01' | pax -w | gzip > files03.tar.gz

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 07-12-2015
Code:
find /tmp/w/ -type f -newermt '2014-01-01' ! -newermt '2014-02-01' -print0 | tar czvf file01.tar.gz --null -T -


Last edited by Aia; 07-12-2015 at 10:06 PM..
# 4  
Old 07-13-2015
thanks for yours input , i will take that into consideration, but about the main question,
how can i make this code in better way , so i do not have to right it multiple times?
i try to do it with for cycles but as i have -newermt 2 times in same iteration it makes it harder
Code:
find /tmp/w/ -type f -newermt '2014-01-01' ! -newermt '2014-02-01' | pax -w | gzip > files01.tar.gz
find /tmp/w/ -type f -newermt '2014-02-01' ! -newermt '2014-03-01' | pax -w | gzip > files02.tar.gz
find /tmp/w/ -type f -newermt '2014-03-01' ! -newermt '2014-04-01' | pax -w | gzip > files03.tar.gz

# 5  
Old 07-13-2015
Code:
year=$(date +%Y)
for month in 01 02 03
do
 if [ $month -ne 12 ]
 then
  endm=$(printf "%02d" $((month+1)))
  endy=$year
 else
  endm=01
  endy=$((year+1))
 fi
 find /tmp/w/ -type f -newermt "$year-$month-01" ! -newermt "$endy-$endm-01" | pax -w | gzip > "files$month.tar.gz"
done


Last edited by MadeInGermany; 07-13-2015 at 08:32 AM.. Reason: removed unnecessary formatting for year
# 6  
Old 07-13-2015
thanks for u answers, but i just wonder how can i save file01.tar.gz in the folder where it find archives and not on local folder ?

coz this function save archive on directory where i run the script
Code:
find /tmp/w/ -type f -newermt '2014-01-01' ! -newermt '2014-02-01' -print0 | tar czvf file01.tar.gz --null -T -

# 7  
Old 07-13-2015
Simply give your output file a full path:-
Code:
find /tmp/w/ -type f -newermt '2014-01-01' ! -newermt '2014-02-01' -print0 | tar czvf /full/path/to/file01.tar.gz --null -T -


Is this what you need?


Robin

Last edited by rbatte1; 07-13-2015 at 11:13 AM.. Reason: Spelling correction
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Multi-Volume tar archives. [solved]

Hi, The only off-line storage medium I have is DVD. I am trying to back up around 10G of data and if I can achieve a practical solution I will use it more generally. I am currently considering something along the lines of: tar --create --multi-volume --tape-length=nnnn <pathspec> |... (0 Replies)
Discussion started by: MikeGM
0 Replies

2. Shell Programming and Scripting

tar command to explore multiple layers of tar and tar.gz files

Hi all, I have a tar file and inside that tar file is a folder with additional tar.gz files. What I want to do is look inside the first tar file and then find the second tar file I'm looking for, look inside that tar.gz file to find a certain directory. I'm encountering issues by trying to... (1 Reply)
Discussion started by: bashnewbee
1 Replies

3. Shell Programming and Scripting

Create unique tar archives from a list of directories

I'm looking to archive a client directory from a CIFS share There are multiple directories that will be stored in a text file and I'm looking to create an individual tar archive of each folder in the directory. I've tried a number of commands to no avail. Here's what I would like. ... (2 Replies)
Discussion started by: Steelysteel
2 Replies

4. Post Here to Contact Site Administrators and Moderators

monthly membership??

Hi, Do we have monthly membership (VIP) in our forum? if not, do we have any plans for it? Regards, (1 Reply)
Discussion started by: clx
1 Replies

5. UNIX for Dummies Questions & Answers

tar -cvf test.tar `find . -mtime -1 -type f` only tar 1 file

Hi all, 4 files are returned when i issue 'find . -mtime -1 -type f -ls'. ./ora_475244.aud ./ora_671958.aud ./ora_934052.aud ./ora_934050.aud However, when I issued the below command: tar -cvf test.tar `find . -mtime -1 -type f`, the tar file only contains the 1st file -... (2 Replies)
Discussion started by: ahSher
2 Replies

6. OS X (Apple)

Decompressing Tar Archives (Finally!)

If you've come across this problem with unzipping/decompressing zips, you might find this helpful: I was having a little trouble with unzipping (decompressing) tarred archives under OS 10.5 until today. My first attempt was to just simply double-click on the zip file (i.e., example.tar.gz) and... (2 Replies)
Discussion started by: unimachead
2 Replies

7. Shell Programming and Scripting

monthly calculation

pls can anyone help me with this script, the script is below, i need the script to get the previous month result every new month , the problem is that the loop has to be automated to always calculate for previous month . a=`date "+%Y"` #this year to be used b=$(date "+%Y%m" --date='49 days... (6 Replies)
Discussion started by: neyo
6 Replies

8. UNIX for Dummies Questions & Answers

flash archives

HI, are you supposed to run flarcreate in multi user mode? or should you do it in single user? (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies

9. UNIX for Dummies Questions & Answers

segmenting tar archives

assuming i need to create a tar archive which would turn out to be bigger than 2gb, how could i segment the archive into say, 1 gb parts? (3 Replies)
Discussion started by: crudealien
3 Replies

10. UNIX for Dummies Questions & Answers

tar archives

I have a tar archive which I believe may be corrupted, produced on an HP-UX 10.x box and written to a 4mm DDS-3 tape. I understand that gnu tar has a -W (--verify) option which will attempt to verify the archive after it has been created. Am I right in saying that this option cannot be used to... (4 Replies)
Discussion started by: sam_pointer
4 Replies
Login or Register to Ask a Question