0

Before copying the resulting backup file to directory, script should first check to make sure there already isn’t a file with the same name (this is my first time writing a script)

#!/bin/bash

time=$(date +%m-%d-%y)
Backup_file=/home/abc
Dest=/home/def
filename=back-$time.tar.gz

tar -czf $Dest/$filename /home/abc
read $file

if [ -f $file ]; then
      echo "Error file $file already exist!"
else
      mv $file /home/def
fi
  • 2
    And, your question is? Also, which distro and release are you using? Different releases have different tools for us to recommend. Please click edit and add that vital information to your question so all the facts we need are in the question. Please don't use Add Comment, since that's our channel to you. All facts about your system should go in the Question with edit – K7AAY Apr 8 at 23:06
  • !/bin/bash time=(date +%m-%d-%y) Backup_file=/home/abc Dest=/home/def filename=back-$time.tar.gz tar -czf $Dest/$filename /home/abc read $file if [ -f $file ]; then echo "Error file $file already exist!" else mv $file /home/def fi – Luna Apr 8 at 23:06
  • 1
    1) When you use Add Comment, all the text formatting is lost, as you can see above. Please avoid using Add Comment.. 2) It still is not clear what your question is. 3) Which which distro and release are you using? Different releases have different tools for us to recommend. – K7AAY Apr 8 at 23:13
  • 1
    In addition to everything else, I would reccommend against using time as a variable as it is shell keyword for several shells and could cause confustion. In declaring it's value, you need to have time=$(date +%m-%d-%y) or else it won't work in the way that you want. I have edited this in your question. – Nasir Riley Apr 9 at 1:22
  • Do you mind if I suggest using a different date format? I know month-day-year is the American human-readable standard (while other places use day-month-year as their human-readable standard), but for computer filenames I would suggest using year-month-day as a more widely accepted standard in computing. One reason is that files sorted by filename are then also sorted sensibly by date. Sometimes there may be good reason not to use this, but I think it should at least be considered. – mdmay74 Apr 9 at 2:03
0

The tar command you are using will do the actual job of backing up your /home/abc directory to $filename in the /home/def directory. If you want to check for the existence of this filename in that directory before "copying the resulting backup file to directory", then you probably should test for the file's existence before running that tar command. Such as:

#!/bin/bash

time=$(date +%m-%d-%y)
Backup_file=/home/abc
Dest=/home/def
filename=back-$time.tar.gz

if [ -f $filename ]; then
  echo "Error file $filename already exist!"
else
  tar -czf $Dest/$filename /home/abc
fi

However, I might suggest a few "improvements":

#!/bin/bash

today=$(date +%Y-%m-%d)
Backup_file=/home/abc
Dest=/home/def
filename=back-${Backup_file//\//_}-${today}.tar.gz

if [ -f ${filename} ]; then
  echo "Error file ${filename} already exists!"
else
  tar -czf ${Dest}/${filename} ${Backup_file}
fi
  • I've changed the date format to use 4-digit year, then month, then day (my preference)
  • I've also changed the variable name, as pointed out by @Nasir
  • it can be useful for the backup filename to show what area it's backing up if you have more than one area that you might backup on the same day, so my filename variable uses the Backup_file variable, but replaces all instances of '/' with '_' (as per the Parameter Expansion section of man bash and as mentioned in this answer for replacing all instances of a character)
  • finally I've used the Backup_file variable in the tar command, so if you change the variable, it only needs to be changed in one place (definitely recommended)

If you really want to run the tar command and then check for existence of the file, you probably should tar to a temporary location so you can move it later. However, if you are not going to do anything with the tar file if a file of the same name already exists, then you might have wasted a lot of time before finding that out, so I think checking before running the tar command is a better option.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.