Moving multiple directories on remote ssh connection, sometimes moves directories inside of each other..please help


I am using cygwin to create a 7 day snapshot backup of my hard drive every night.  
I basically have 7 folders, delete the oldest and move 0 to 1, 1 to 2, etc, then upload my files to 0.

I have attached the snippet.

The problem I am having is that sometimes these folders are winding up inside of each other, and then they wind up filling my hard drive up on the receiving server.

For example, folder 1 will be found inside of folder2 as you can see below when I do a ls -al *

total 464756
drwx------   5 user user      4096 Nov 14 16:42 .
drwx------   8 user user      4096 Nov 14 16:42 ..
drwx------   4 user user      4096 Nov 13 16:56 daily.0
drwx------  12 user user      4096 Nov 12 10:38 folder1
drwx------   5 user user      4096 Nov  1 13:40 folder2

total 20
drwx------   5 user user 4096 Nov 12 16:43 .
drwx------   8 user user 4096 Nov 14 16:42 ..
drwx------   4 user user 4096 Nov  8 02:46 daily.1
drwx------  12 user user 4096 Nov  8 02:46 folder1
dr-x------   5 user user 4096 Nov  1 13:40 folder2

total 24
drwx------  4 user user 4096 Nov  9 16:39 .
drwx------  8 user user 4096 Nov 14 16:42 ..
drwx------  3 user user 4096 Nov  7 18:39 daily.2
dr-x------  5 user user 4096 Jun 25 10:24 folder2

So my question is why are they moving inside of the folders.  I have a 3 day snapshot set up and it seems to be working perfectly.  I have checked the code to ensure it is consistent between the two.


@echo off
rem Rsync Job Control File
rem this will keep a 7 day rotating snapshot and a 3 week rotating snapshot on remote machine
rem This section will rotate the folders for a 7 day snapshot
c:\cygwin\bin\ssh -l USER HOST rm -Rf /home/USER/daily.7; mv /home/USER/daily.6 /home/USER/daily.7 ; mv /home/USER/daily.5 /home/USER/daily.6 ; mv /home/USER/daily.4 /home/USER/daily.5; mv /home/USER/daily.3 /home/USER/daily.4; mv /home/USER/daily.2 /home/USER/daily.3; mv /home/USER/daily.1 /home/USER/daily.2; mv /home/USER/daily.0 /home/USER/daily.1; cp -al /home/USER/daily.1 /home/USER/daily.0
rem this section actually transfers the files
C:\cygwin\bin\rsync -avz --delete -e c:\cygwin\bin\ssh /cygdrive/c/folder1 USER@HOST:/home/USER/daily.0/ 
C:\cygwin\bin\rsync -avz --delete -e c:\cygwin\bin\ssh /cygdrive/c/folder2 USER@HOST:/home/USER/daily.0/ 
c:\cygwin\bin\ssh -l USER HOST chmod -Rf 700 /home/USER/daily.0

Open in new window

Who is Participating?

The mv command will move one dir inside the other one if it exists.

e.g. if dir1 does not exit and I run

mv dir0 dir1

then dir0 is renamed dir1 at the same dir level
but if dir1 exists then dir0 is moved to be below dir1

You may do a check of the existance of the other dir before mv dir0 to dir1. It is also better to have all these commands in one shell script on the remote server which you call remotely.

Why do you cp -al /home/USER/daily.1 /home/USER/daily.0 ?

you might be better served to make a tarball of the directories you want to save....

tar -cvf myArchive_<dateHere> ./dir1 ./dir2 ./dir3 <etc>

then you can compress it and move it as a unit.  

puck916Author Commented:
I do cp-al to hard link the files....although looking at the new versions of rsync (all except mac version) this is now available through an rsync argument.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.