1

I made a save script, to save some directories in my /home. To do that I loop through the directories in /home, and launch a save for each of them.

These directories contain jar files, and some .txt. Properties and other directories. The first time I launch the script it worked great. I modified it, but yesterday when I try some feature it goes wrong and I can not find why.

I have this error when I use the tar -cjf command to create .tar.zip archive.

tar: home/myFolder : impossible stat: no file or folder of this type

backupdate=$(date +%Y-%m-%d) 
dirbackup=/save/$backupdate 
mkdir $dirbackup 
list_dossier=`ls ../home`

for server in $list_dossier 
do

    tar -cjf $dirbackup/$server.tar.zip home/$server

done

EDIT I'm trying to keep this shape of save : a folder with name the date, and in it, X archves contain my X folders of my /home

#!/bin/bash
backupdate=$(date +%Y-%m-%d)
dirbackup=/save/$backupdate
mkdir $dirbackup

for server in /home/*
do
    echo $server"test"
    tar -cjf $dirbackup$server.tar.zip $server

done

exit
  • 1
    Is that the actual script you are using? There's a syntax error in the for statement. There ought to be a ; (or a newline) before do. Also, you are much better off iterating over /home/*/, as in for serverpath in /home/*/; do server=$(basename "$serverpath"); tar ... "$serverpath"; done or something like that. – Kusalananda Apr 7 '19 at 21:43
  • You may also have problems with your ls ../home results if the files listed have whitespace in their names, or newline characters etc.. consider feeding your for loop (or a while loop) a list of null separated results instead. – Jeff Apr 8 '19 at 0:19
  • The do in my code was one line below, I have edit as my code is ^^ Ok for the /home/*/ and yes you right @JeffH.I have a space after the server name ! – BoBsmil3Y Apr 8 '19 at 8:09
  • I think home should be /home. – ctrl-alt-delor Apr 13 '19 at 18:27
  • Any particular reason why tar's output file has a *.zip extension? I ask because *.zip files are not the same as bzip files. – TSJNachos117 Apr 13 '19 at 22:39
0

This line will cause problems:

tar -cjf $dirbackup$server.tar.zip $server

That's because $server would be something like /home/myserver and so this command would expand to

tar -cjf /save/2019-04-13/home/myserver.tar.zip /home/myserver

Note the extra /home in the path to the tarfile.

Instead we can change directory before hand..

#!/bin/bash
backupdate=$(date +%Y-%m-%d)
dirbackup=/save/$backupdate
mkdir $dirbackup || exit
cd /home || exit

for server in *
do
    echo $server"test"
    tar -cjf $dirbackup$server.tar.zip $server
done
| improve this answer | |
  • Thanks for your help finally got it ! Can you just explain me the || exit ? Because exit is how to quit a script no ? – BoBsmil3Y Apr 13 '19 at 18:55
  • It just makes the script exit if the previous command fails, so if the mkdir fails then the script aborts rather than continuing. – Stephen Harris Apr 13 '19 at 19:53
  • Oh ok ! Thanks it's will help me to know that :D – BoBsmil3Y Apr 13 '19 at 20:48
0
backupdate=$(date +%Y-%m-%d) 
dirbackup=/save/$backupdate 
mkdir $dirbackup

find ../home -type d -not -name "." -a -not -name ".." -exec tar -cjf $dirbackup/'{}'.tar.bz2 '{}' \;
| improve this answer | |
  • There is no need to quote {}. Also note that this will be a pathname, not a filename. Using it as part of a filename would do unexpected things. – Kusalananda Apr 8 '19 at 8:17
  • Yes it's working like that :) But I want to separate each folder save ^^ I have tried your code, and it's seems to "ignore" some files. I have the same error as the beginning but only for few folder ... :/ – BoBsmil3Y Apr 8 '19 at 8:22
  • I have edit the question to update my code – BoBsmil3Y Apr 8 '19 at 10:14
  • Up ? please – BoBsmil3Y Apr 10 '19 at 20:50

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.