Archive script spot a mistake?

#!/bin/bash

source=/to_be_archived
dest=/archived

    echo "is this archive for an audio tar press (t) or an audio directory press (d)"
    read option

    case $option in

        d)
            cd "$source"

            echo "please specify full path to directory you want to be made into a tar"
            read -e dir

#            echo "please enter ID number ie ID1234"
#            read id

#            echo "please specify where you want the tar file to be stored"
#                       read -e dest

            cd "$dir"

            base=$(basename "$dir")

                echo -e "create "$base" into "$base".tar\n"
                                echo -e "move "$base".tar to "$dest"\n"
#                               echo -e "remove "$base".tar from "$dir"\n"
                echo -e "remove "$base" from "$dir"\n"

                                        echo "is this information correct, press (y) or press (n)"
                                        read correct

                                        case $correct in

                                                y)
                            echo "the script will now continue";;

                                                n)
                            echo "please re-run the script inputting correct details"
                                                        exit;;

                                                *)
                            echo "invalid selection, please re-run the script"
                                                        exit;;

                                        esac                

            date >> /inventories/"$base".csv
            echo "" >> /inventories/"$base".csv
#            echo -e ""$id"\n" >> /inventories/"$base".csv
            echo -e ""$dir"\n" >> /inventories/"$base".csv
            echo -e "how many files = `find . -type f | wc -l`\n" >> /inventories/"$base".csv
            echo -e "size of directory = `du -sh`\n" >> /inventories/"$base".csv
            ls -R >> /inventories/"$base".csv
            
            cd ..

            if ! tar -cf "$base".tar "$dir" ; then
                echo "something went wrong with the tar, please do manually"
                exit
            fi

            if ! mv "$base".tar "$dest" ; then
                echo "something went wrong with the move, please do manually"
                exit
            fi

#            if ! rm -f "$base".tar ; then
#                echo "something went wrong with the rm -f command, please do manually"
#                exit
#            fi

            if ! rm -rf "$dir" ; then
                echo "something went wrong with the rm -rf command, please do manually"
                exit
            fi;;

        *)

            echo "invalid selection, please re-run the script"
            exit;;

    esac

can anyone spot the mistake as when i run the script it cant make the tar

many thanks,

rob

Hi,

Could you possibly post the exact error you get from the tar command itself when the script is run ? You will of course get your own error message as defined in the script, but it would be very useful to know why tar itself believes it is failing.

ok thanks i will next time i run a test on it as when i create a directory and couple of sub directorys in the root directory and then some text files it works fine, no hicups

but when i do it on some more major directorys that hold couple of GB of data thats when it messes up

this is the error i get -

is this archive for an audio tar press (t) or an audio directory press (d)
d
please specify full path to directory you want to be made into a tar
robert_test/after_louise_1606/
create after_louise_1606 into after_louise_1606.tar

move after_louise_1606.tar to /vol/cha-archive/audio

remove after_louise_1606 from robert_test/after_louise_1606/

is this information correct, press (y) or press (n)
y
the script will now continue
tar: robert_test/after_louise_1606: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
something went wrong with the tar, please do manually

Hi,

This error indicates that the content you asked tar to add to an archive could not be found, and so it could not proceed. So either the directory robert_test/after_louise_1606 did not exist at the time the tar command ran, or it was located underneath a different part of the filesystem than the current working directory of tar at that time.

One thing you could try here, and which is generally good practice, is to use fully-qualified paths rather than relative paths in a script. So (for example) a path like /home/robert/robert_test/after_louise_1606 (I'm just making up the first part of that path obviously, since I don't know where on your filesystem the directory really is). Fully-qualified paths to binaries and files in scripts are almost always a good idea, and can eliminate an entire class of errors entirely.

So that'd be my take on this: either the directory you tried to add to an archive didn't exist at that exact moment in time, or you weren't actually running the tar command in the current working directory you expected to be in.

Does robert_test/after_louise_1606 exist in your current working directory (I guess: not).
I've got some difficulties to follow your flow; i.e. where you're located after cd "$source" and cd "$dir" ; That may be the reason tar "$dir" fails...

what a idiot i am, changed it and it now works like a charm -

#!/bin/bash

cd /vol/cha-work/_ARCHIVE/to_be_archived/audio
dest=/vol/cha-archive/audio

	echo "is this archive for an audio tar press (t) or an audio directory press (d)"
	read option

	case $option in

		t)

			echo "please specify full path to tar file"
			read -e tar

			base=$(basename "$tar")

#			echo "please enter ID number ie ID1234"
#			read id

#			echo "please specify where you want the tar file to be stored"
#			read -e dest

#				echo -e "rename "$base" to "$id"_"$base"\n"
	                       	echo -e "move "$base" to "$dest"\n"
                        	echo -e ""$base" will be removed from "$tar"\n"

					echo "is this information correct, press (y) or press (n)"
					read correct
	
					case $correct in

						y)
							echo "the script will now continue";;

						n)
							echo "please re-run the script inputting correct details"
							exit;;

						*)
							echo "invalid selection, please re-run the script"
							exit;;

					esac

			if ! mv "$tar" "$base" ; then
				echo "something went wrong with the move command, please do manually"
				exit
			fi

			if ! mv "$base" "$dest" ; then
				echo "something went wrong with the move command, please do manually"
				exit
			fi

#			if ! rm -f "$base" ; then
#				echo "something went wrong with the rm -f command, please do manually"
#				exit
#			fi
			;;

		d)

			echo "please specify full path to directory you want to be made into a tar"
			read -e dir

#			echo "please enter ID number ie ID1234"
#			read id

#			echo "please specify where you want the tar file to be stored"
#                       read -e dest

			cd "$dir"

			base=$(basename "$dir")

				echo -e "create "$base" into "$base".tar\n"
                                echo -e "move "$base".tar to "$dest"\n"
#                               echo -e "remove "$base".tar from "$dir"\n"
				echo -e "remove "$base" from "$dir"\n"

                                        echo "is this information correct, press (y) or press (n)"
                                        read correct

                                        case $correct in

                                                y)
							echo "the script will now continue";;

                                                n)
							echo "please re-run the script inputting correct details"
                                                        exit;;

                                                *)
							echo "invalid selection, please re-run the script"
                                                    	exit;;

                                        esac				

			date >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo "" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
#			echo -e ""$id"\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo -e ""$dir"\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo -e "how many files = `find . -type f | wc -l`\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo -e "size of directory = `du -sh`\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			ls -R >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			
			cd ..

			if ! tar -cf "$base".tar "$base" ; then
				echo "something went wrong with the tar, please do manually"
				exit
			fi

			if ! mv "$base".tar "$dest" ; then
				echo "something went wrong with the move, please do manually"
				exit
			fi

#			if ! rm -f "$base".tar ; then
#				echo "something went wrong with the rm -f command, please do manually"
#				exit
#			fi

			if ! rm -rf "$dir" ; then
				echo "something went wrong with the rm -rf command, please do manually"
				exit
			fi;;

		*)

			echo "invalid selection, please re-run the script"
			exit;;

	esac

basically i changed it from this -

if ! tar -cf "$base".tar "$dir" ; then

to this -

if ! tar -cf "$base".tar "$base" ; then