Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking if folder exists & contains files 1

Status
Not open for further replies.

craigey

Technical User
Apr 18, 2002
510
0
0
GB
Hi,

I'm trying to automate a backup of various directories using Rsync. The source directories are mounts of a couple of network shares & the destination is the mount of an external USB drive.

I have an issue with one of the directories where my script reports that the destination doesn't exist when I'm checking to see if it exists or not & I believe this is because the folder contains subfolders but no files (the subfolders contain files though).

I'm also concerned that using the --delete parameter for Rsync could effectively remove all files in the backup if the source is not mounted for some reason. The script does check if the folder exists, but if it's not mounted the folder would still exist meaning there are no files & Rsync may believe the files have been deleted & remove the files in the destination. How should I prevent this from occuring?

I would apreciate any help / advice.

Code:
#!/bin/bash
src='/home/craig'
dest='/media/Backup'
sourcedirs=( 'Music' 'wii' 'Pictures' 'my_stuff' )
destdirs=( 'mp3s' 'wii' 'my_pics' 'my_stuff' )
sourceelements=${#sourcedirs[@]}
destelements=${#destdirs[@]}
if [ "$sourceelements" -ne "$destelements" ];
then
        echo "The number of Source & Destination directories differ\nSource = $sourceelements  Destination = $destelements"
else
        for (( i=0;i<$sourceelements;i++)); do
                if [ -d "$src/${sourcedirs[${i}]}" ] && [ -d "$dest/${destdirs[${i}]}" ];
                then
                        echo "syncing files from $src/${sourcedirs[${i}]}/ to $dest/${destdirs[${i}]}/"
                        #rsync -arv --progress --delete --log-file=$src/$(date +%Y%m%d)_rsync.log $src/${sourcedirs[${i}]}/ $dest/${destdirs[${i}]}/
                else
                        if [ -d "$src/${sourcedirs[${i}]}" ];
                        then
                                echo "Source folder $src/${sourcedirs[${i}]}/ not found"
                        else
                                echo "Destination folder $dest/${destdirs[${i}]}/ not found"
                        fi
                fi
        done

fi
 
A starting point:
Code:
[ "$(ls $src/${sourcedirs[${i}]}" ] || echo EMPTY source

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you. Feel a little silly now as just assumed the -d would just correctly return if the directory existed or not. I didn't think to check if it was empty as it does contain subfolders.

I will test this when I get home - I did lookup some other ways of checking if the folders were mounted / empty or not.
 
I've tried the ls option, but this reports 'ls cannot access /home/craig/fakedir/' when it can't find the directory. any further ideas?
 
What is your actual code ?
Where is fakedir coming from ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
perhaps you should ignore that post & look at this one instead!

Code:
#!/bin/bash
src='/home/craig'
dest='/media/Backup'
sourcedirs=( 'Music' 'Pictures' 'software' 'Videos' )
destdirs=( 'mp3s' 'my_pics' 'Software' 'my_videos' )
sourceelements=${#sourcedirs[@]}
destelements=${#destdirs[@]}
if [ "$sourceelements" -ne "$destelements" ];
then
        echo "The number of Source & Destination directories differ\nSource = $sourceelements  Destination = $destelements"
else
        for (( i=0;i<$sourceelements;i++)); do
#               if [ -d "$src/${sourcedirs[${i}]}" ] && [ -d "$dest/${destdirs[${i}]}" ];
#               [ "$(ls -A $src/${sourcedirs[${i}]}/))" ] && echo "Not Empty" || echo "Empty"
                if [ "$(ls $src/${sourcedirs[${i}]}/)" ]; then
                        if [ "$(ls $dest/${destdirs[${i}]}/)" ]; then
                                if [ $(find $src/${sourcedirs[${i}]}/ . -maxdepth 1 -type f | wc -l) == "0" ]
                                then
                                echo "Source folder $src/${sourcedirs[${i}]}/ is empty - Guessing it's not mounted, so not Syncing"
                                else
                                echo "syncing files from $src/${sourcedirs[${i}]}/ to $dest/${destdirs[${i}]}/"
                                #rsync -arv --progress --delete --log-file=$src/$(date +%Y%m%d)_rsync.log $src/${sourcedirs[${i}]}/ $dest/${destdirs[${i}]}/
                                fi
                        else
                        echo "Destination folder $dest/${destdirs[${i}]}/ not found"
                        fi
                else
                echo "Source folder $src/${sourcedirs[${i}]}/ not found"
                fi
        done
fi
 
I'd try something like this:
Code:
...
for ((i=0;i<$sourceelements;i++)); do
  if [ -d "$dest/${destdirs[${i}]}" ]; then
    if [ -d "$src/${sourcedirs[${i}]}" ]; then
      if [ "$(ls $src/${sourcedirs[${i}]}/)" ]; then
        echo "syncing files from $src/${sourcedirs[${i}]}/ to $dest/${destdirs[${i}]}/"
        #rsync -arv --progress --delete --log-file=$src/$(date +%Y%m%d)_rsync.log $src/${sourcedirs[${i}]}/ $dest/${destdirs[${i}]}/
      else
        echo "Source folder $src/${sourcedirs[${i}]}/ is empty - Guessing it's not mounted, so not Syncing"
      fi
    else
      echo "Source folder $src/${sourcedirs[${i}]}/ not found"
    fi
  else
    echo "Destination folder $dest/${destdirs[${i}]}/ not found"
  fi
done
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Excellent Thanks.
Too many late nights - couldn't see where I was going wrong.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top