Bash script: "mkdir -p" doesn't work with var(cat x)

Hello, :slight_smile:

I've an issue with the creation of a directory, All work without it :mad: So, below, my scripts with the debug output :

#!/bin/bash

# PATHS
HOME_BACKUP="/home/backup"
HOME_SCRIPT="/home/scripts/test/backup_server"
TARGET="/var/www"

# DATE
DATE_Ymd=$(date +%Y-%m-%d)

# SENDEMAIL
SENDER="MYSERVER@example.com"
RECIPIENT="test@example.com"
SMTP="0.0.0.0"

# DEBBUG
set -x

# SCRIPT
for IP in `egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' config_ip.cfg`
do
        ssh "$IP" 'hostname' > hostname.txt
        HOSTNAME=$(cat hostname.txt)

                cd "$HOME_BACKUP"
				
				mkdir -p "$HOSTNAME"
				
				cd "$HOSTNAME"

                mkdir "$DATE_Ymd"

        rsync -azvtP test_user@"$IP":"$TARGET" "$HOME_BACKUP"/"$HOSTNAME"/"$DATE_Ymd"

        tar -czvf "$HOME_BACKUP"/"$HOSTNAME"/"$DATE_Ymd".tar.gz "$DATE_Ymd"

                rm -rf "$DATE_Ymd"

                cd "$HOME_SCRIPT"
         
                rm "$HOME_SCRIPT"/hostname.txt
done

rm dry_run.txt

Config File :

IP CLIENT: 192.168.1.93
IP CLIENT: 192.168.1.30

Debug

test_user@MYSERVER:/home/scripts/test/backup_server$ ./backup_script.sh
++ egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' config_ip.cfg
+ for IP in '`egrep -o '\''([0-9]{1,3}\.){3}[0-9]{1,3}'\'' config_ip.cfg`'
+ ssh 192.168.1.93 hostname
++ cat hostname.txt
+ HOSTNAME=vm1
+ cd /home/backup
+ cd vm1
+ mkdir 2016-12-07
+ rsync -azvtP test_user@192.168.1.93:/var/www /home/backup/vm1/2016-12-07
receiving incremental file list
www/
www/html/
www/html/COPYING
         19,419 100%   18.52MB/s    0:00:00 (xfr#1, to-chk=28/31)
+ tar -czvf /home/backup/vm1/2016-12-07.tar.gz 2016-12-07
2016-12-07/
2016-12-07/www/
2016-12-07/www/html/
+ rm -rf 2016-12-07
+ cd /home/scripts/test/backup_server
+ rm /home/scripts/test/backup_server/hostname.txt

For the first IP CLIENT there aren't problem, but for the next (Assuming that the $HOSTNAME directory does not exist) it doesn't work, it even not treat the command :

+ for IP in '`egrep -o '\''([0-9]{1,3}\.){3}[0-9]{1,3}'\'' config_ip.cfg`'
+ ssh 192.168.1.30 hostname
++ cat hostname.txt
+ HOSTNAME=vm2
+ cd /home/backup
+ cd vm2
./backup_script.sh: line 16: cd: vm2: No such file or directory
+ mkdir 2016-12-07
+ rsync -azvtP test_user@192.168.1.30:/var/www /home/backup/vm2/2016-12-07
receiving incremental file list
rsync: mkdir "/home/backup/vm2/2016-12-07" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(674) [Receiver=3.1.0]
+ tar -czvf /home/backup/vm2/2016-12-07.tar.gz 2016-12-07
2016-12-07/
tar (child): /home/backup/vm2/2016-12-07.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
+ rm -rf 2016-12-07
+ cd /home/scripts/cpp/backup_server
+ rm /home/scripts/cpp/backup_server/hostname.txt

I tried with a simple condition [[ -d $dir ]] I've the same result..
Please who can I help me ? :b:

---------- Post updated at 02:49 PM ---------- Previous update was at 01:58 PM ----------

The problem was a "rights issue".. I copied my original script for my tests with the root right and when I executed the copied script with the user_test, it did not have the right to create the directory, stupid mistake.. I hope have helped others

1 Like

Glad you got it working, thanks for coming back to report the problem. Very strange how it did not report mkdir -p in the log file, or any of its error messages.