0

I'm trying to back up certain files and directories in a script.

So far i have:

bkdest=/Web
sources=("includes_php" "html/css" "html/includes" "html/index.php")
datestamp=$(date +%Y_%m_%d_%H_%M_%S)
odir=/Web-Backup/${datestamp}_Web
for target in ${sources[@]}; do
  mkdir -p ${odir}${target}
  cp -R /Web${target} ${odir}${target}
done

The problem I am having is that I copy the folder, into an identically named folder when i copy folders. Is there a better way of doing this or do I need to handle folders and files in separate lists?

The machine I am using is on 10.12.6 if that makes a difference.

4
  • Is there a specific reason you don‘t backup the whole content of /Web? Also, what exactly is the problem you need solving here?
    – nohillside
    Commented Oct 17, 2022 at 19:03
  • There is way too much information, including lots of large PDF's. This is to backup things I am going to modify only. Commented Oct 18, 2022 at 14:38
  • I see. Can you explain where the problem is with "The problem I am having is that I copy the folder, into an identically named folder when i copy folders"?
    – nohillside
    Commented Oct 18, 2022 at 14:58
  • With this example, when copying, it creates /Web-Backup/html/css/css/<files>, making a superfluous sub folder. Commented Oct 18, 2022 at 15:10

1 Answer 1

1

In the end, I checked the sources array like so:

bkdest=/Web
sources=("includes_php/" "html/css/" "html/includes/" "html/index.php")
datestamp=$(date +%Y_%m_%d_%H_%M_%S)
odir=/Web-Backup/${datestamp}_Web
for target in ${sources[@]}; do
  if [ "${target: -1}" == "/" ]
  then
    mkdir -p ${odir}${target}
    cp -R /Web${target}* ${odir}${target}
  else
    cp /Web${target} ${odir}${target}
done

In short, I put trailing / on folders. I checked the last character of the string using ${target: -1} and if it's / I treat it as a folder, pulling the contents instead of the folder itself.

1
  • 2
    Using if [[ -d ${target} ]] would work even without the trailing /.
    – nohillside
    Commented Oct 18, 2022 at 15:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .