1

I am using the below script to move files from a source directory to a destination1 directory then copy the files to destination2 directory and it works well sometimes but suddenly sometimes it starts moving the whole system files under / like home, etc, usr,..... to the destination directory which then the whole machine system is corrupted and I have to do a recovery. So I am not sure why this happens and what can be the reason for this to happen or what triggers it?

#!/bin/bash -i
#
alias brc='source ~/.bashrc'
mydir='My-Dir1*'
min_age=5
cdate=$(date +%F)
echo -n "Enter ID: "; read u; echo $u
dstdir1="$(find  /home/myuser/Documents/dstdir1/customers/ -maxdepth 1 -name "*${u}*" -type d)/Transaction_$cdate"
dstdir2="$(find  /home/myuser/Documents/dstdir2/customers/ -maxdepth 1 -name "*${u}*" -type d)/Transaction_$cdate"
mkdir -p $dstdir2
mkdir -p $dstdir1
dir_list=$(find "/home/myuser/Documents/customers/$u/" -type d -iname "$mydir" | grep -iv Recycle.Bin)
echo $dir_list
if [ -z "$dir_list" ]; then
    echo "Error: could not find any directories matching pattern $mydir" >&2
    exit 1
fi
source ~/.bashrc
cwd=$PWD
echo "SELECT THE SOURCE DIRECTORY"

onlyonce=0

select dir in $dir_list; do
    srcdir="/home/myuser/Documents/customers/$u"
    echo "Files will be moved from:"
    echo "$srcdir to"
    echo "$dstdir1 then copied to"
    echo "$dstdir2"
    read -p 'continue (y/n)? ' -r answer
    [ "$answer" = "y" ] || exit 0

    echo "Moving then coping files older than $min_age minutes."
    while :; do
        echo 'setting source file and directory permissions...'
        find "$srcdir" -type d ! -perm 0775 -exec chmod 0775 '{}' \;
        find "$srcdir" -type f ! -perm 0664 -print0 | xargs -0 -r -- chmod 0664
        cd "$srcdir"
        echo 'Moving files...'
        rsync_extra_opts='-ptOW --info=progress2 --no-super --remove-source-files'
        find . -type f -mmin +$min_age -print0 | rsync -0 --files-from=- $rsync_extra_opts ./ "$dstdir1/"
        if [ $? -ne 0 ]; then
            echo 'correcting destination directory permissions...'
            find "$dstdir1" -type d ! -perm 0775 -exec chmod -c 0775 '{}' \;
        fi
        cd "$dstdir1"
        echo 'Coping files...'
        rsync_extra_opts='-ptOW --info=progress2 --no-super'
        find . -type f -mmin +$min_age -print0 | rsync -0 --files-from=- $rsync_extra_opts ./ "$dstdir2/"
        if [ $? -ne 0 ]; then
            echo 'correcting destination directory permissions...'
            find "$dstdir2" -type d ! -perm 0775 -exec chmod -c 0775 '{}' \;
        fi

        cd "$cwd"
        df -h "$srcdir" "$dstdir1" "$dstdir2"
        echo 'sleeping 5 minutes...'
        sleep 5m
        echo ""
    done
    break
done
cd "$cwd"
1

I would imagine that your script is running from / and the unprotected cd "$srcdir" is failing at some point.

Some suggestions

  • Use printf statements and/or set -x to see what values your variables contain
  • Use rsync --dry-run and other non-destructive operations while testing
  • Protect the cd "$srcdir" (cd "$srcdir" || continue, or if cd "$srcdir"; then ...), or [[ -d "$srcdir" ]] || continue)
  • Better still, don't change directory anywhere and avoid the cd by using full paths in the rsync
| improve this answer | |
  • Thanks for the suggestions. But what might be causing this to happen? The script is located in a folder in my documents folder. But then I run it using a desktop launcher with the following Exec command Exec=x-terminal-emulator -e /home/myuser/Documents/MyApp/myscript.sh so I don't know how it runs from / ? – Tak Oct 23 '19 at 12:39
  • It must be running as root, otherwise it wouldn't be able to move /usr, etc. Or you've got a really broken system. – roaima Oct 23 '19 at 13:31
  • And how to know if it's running as root or not? – Tak Oct 23 '19 at 13:37
  • id inside the script – roaima Oct 23 '19 at 13:48
  • But there is no id in the script. I just wanted to know what could have caused this. – Tak Oct 23 '19 at 20:41
0

Ok, after commenting five times, I've reached the point where I should just post an answer :-)

(Previous comments deleted)

To me it looks like dstdir1 and dstdir2 may be empty, so that your rsync-command rsyncs to /.

To prevent such errors, change the header of your file (trailing space needed after -):

#!/bin/bash - 
set -o nounset   # exit on unset variables.
set -o errexit   # exit on any error.

It may be helpful to install shellcheck on your box.

I've run it against your script and it complains about unquoted vars on lines 7, 10, 11, 13, 41 and 49. onlyonce=0 seems to be unused.

The alias brc is unused, and I'd advise against using aliases in shellscripts. You should use functions instead.

| 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.