backup - bash .tar creation - Ask Ubuntu


i try create .tar.gz files or directories in parameters backup.

however, when create .tar.gz archive, have error :

tar: path_directory_1: cannot stat: no such file or directory tar: path_directory_2 : cannot stat: no such file or directory tar: stopped failed status due previous errors  

how built archive:

tar zcvf $dirbackup/backup-$backupdate.tar.gz $string 

where string is:

old_ifs=$ifs  ifs=$'\n'  string="";  line in $(cat $1);        string="$string $line"  done  ifs=$old_ifs 

$1: file paths directory , paths file save.

exemple of $1 content (just directories paths save):

~/documents/miage/l3/semestre5/web ~/documents/miage/l3/semestre5/communication/ 

result set -x :

+ tar zcvf /var/backups/mesbackups/backup-09-11-2016-13-58/backup-09-11‌​-2016-13-58.tar.gz '~/documents/miage/l3/semestre5/web' '~/documents/miage/l3/semestre5/communication/' tar: ~/documents/miage/l3/semestre5/web : cannot stat : no such file or directory  tar: ~/documents/miage/l3/semestre5/communication : cannot stat : no such file or directory  tar: stopped failed status due previous errors  

when run command in shell that's work ...

tar zcvf /var/backups/mesbackups/test/backup-09-11‌​-2016-13-58.tar.gz ~/documents/miage/l3/semestre5/web ~/documents/miage/l3/semestre5/communication/  

how can script ? seems work without quots no ?

analysis

looking @ input file examples

~/documents/miage/l3/semestre5/web ~/documents/miage/l3/semestre5/communication/ 

and error messages tar

tar: ~/documents/miage/l3/semestre5/web : cannot stat : no such file or directory tar: ~/documents/miage/l3/semestre5/communication : cannot stat : no such file or directory 

you seem presume either shell interpreter or tar expand tilde (~) when reading lines either of following constructs:

  • for in $(cat infile)
  • while read i; ...; done < infile
  • mapfile ... < infile

this presumption false. none of common shell interpreters performs expansion, or more tilde expansion in these situation. evaluates escape sequences (see read). no known (to me) implementation of tar performs argument expansion either (and shouldn't since create many issues).

solution

you need expand tilde characters in input files somehow.

the easiest way change input file manually or filter (if can $home doesn't include sed's escape or delimiter characters (here \ , ;)):

sed -e 's;^~\(\/\|$\);'"$home"'\1;' infile | tar cvzf my-archive.tar.gz --from-file - 

if want take advantage of available shell expansions , can assure proper input escaping can use eval built-in command:

while read -r i;   eval echo "$i" done < infile | tar cvzf my-archive.tar.gz --from-file - 

Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User

sql server - "Configuration file does not exist", Event ID 274 - Super User