Bash script TAR by Filetype is not gzipping the specified filetypes on recursive command

StackOverflow https://stackoverflow.com/questions/17341791

  •  01-06-2022
  •  | 
  •  

سؤال

Simply put, I did an exhaustive library update on my network to be compatible with XBMC. Now that it's done, I thought it wouldn't hurt to periodically make a backup of the nfo,bub,srt,jpg,tbn files (everything except any video file) that are in the various directories and subdirectories in my TV Shows folder. I also wanted to zip it by date. After playing around, I got this to semi-work:

find '/volume2/MyMedia/TV Shows/' -type f \( -name \*\.sub -o -name \*\.srt -o -name \*\.nfo -o -name \*\.jpg -o -name \*\.tbn \) -print0 | xargs -0 tar -pvczf '/volume2/Backups/XBMC Backups/'XBMC-TVShows-NFO-JPG-TBN-SUB-Backup-$(date +%m-%d-%y).tar.gz

It goes through the motions (scanning every directory and finding the files), and I see the zip file grow, but then suddenly the tar.gz goes down to 0 bytes and counts up again. I think it's doing that once it hits a new directory to scan.

At the end, I get a 1.8MB file with only the last directory it scanned through. Inside that, is almost everything, except it omitted 1 of the subdirectories. IE: It zipped up '30 Rock' and all of the files I specified, but, it did that for all folders except Season 2.

So I am at a loss. Everything looks clear to me in the code. For some reason it's not running properly. I think I have tunnel vision on the project now. A fresh set of eyes would be magnificent!

Here is an output: http://pastebin.com/ywctQ5Sf

Here is what the final tar.gz contained: http://s23.postimg.org/6pc22h7yz/output.png

Final note: The directories of the shows that it backs up do have whitespaces in them. So there is a directory structure like so:

>volume2
->MyMedia
-->TV Shows
--->Fullmetal Alchemist Brotherhood
---->Season 1
----->Fullmetal Alchemist Brotherhood.S01E01.Fullmetal Alchemist.nfo

Thank you for any help!

هل كانت مفيدة؟

المحلول

That's because xargs splits the huge list of files across multiple tar commands, which each try to create a new file. Try using the -A option instead of -vczf ... and pipe the whole command to gzip:

find ... -print0 | xargs -0 tar -A | gzip - > "....tar.gz"

نصائح أخرى

So after revisiting this, I tried l0b0's method. It comes back with:

tar: Options `-Aru' are incompatible with `-f -'

So I looked into another way and came up with this script:

#!/bin/bash
find /volume1/Media/TV\ Series/ -iname "*.sub" -exec tar rvfP /volume1/Backups/TestBackup.tar {} \;
find /volume1/Media/TV\ Series/ -iname "*.srt" -exec tar rvfP /volume1/Backups/TestBackup.tar {} \;
find /volume1/Media/TV\ Series/ -iname "*.nfo" -exec tar rvfP /volume1/Backups/TestBackup.tar {} \;
find /volume1/Media/TV\ Series/ -iname "*.jpg" -exec tar rvfP /volume1/Backups/TestBackup.tar {} \;
find /volume1/Media/TV\ Series/ -iname "*.tbn" -exec tar rvfP /volume1/Backups/TestBackup.tar {} \;
gzip /volume1/Backups/TestBackup.tar

It takes a long time, but I let it run in the middle of the night when nothing else is going on, and it works. I only need to run this backup very infrequently anyway.

If you were so inclined, you can also add an elapsed timer:

#!/bin/bash
date1=$(date +"%s")
# Script stuff here. Uncommented...
date2=$(date +"%s")
diff=$(($date2-$date1))
echo "$(($diff / 60)) minutes and $(($diff % 60)) seconds elapsed."
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top