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
6
  • 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. Commented Apr 7, 2019 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. Commented Apr 8, 2019 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 ! Commented Apr 8, 2019 at 8:09
  • I think home should be /home. Commented Apr 13, 2019 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. Commented Apr 13, 2019 at 22:39

2 Answers 2

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
3
  • Thanks for your help finally got it ! Can you just explain me the || exit ? Because exit is how to quit a script no ? Commented Apr 13, 2019 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. Commented Apr 13, 2019 at 19:53
  • Oh ok ! Thanks it's will help me to know that :D Commented Apr 13, 2019 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 '{}' \;
4
  • 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. Commented Apr 8, 2019 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 ... :/ Commented Apr 8, 2019 at 8:22
  • I have edit the question to update my code Commented Apr 8, 2019 at 10:14
  • Up ? please Commented Apr 10, 2019 at 20:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.