Moving folders in Linux inconsistency

0

I have a shell script that deploy an application to a staging server. However, one of the application's directory needs to be backed up and restored after the deployment (so it is not overwritten). I manage to backup the directory, but the restore part does not behave as expected. Basically, here are the two lines in the shell script :

...
# backup user avatars
mv vitex/app/modules/users/pub/img/users/ ~/tmp/img-users.BAK/ &> /dev/null

...
# restore user avatars
mv ~/tmp/img-users.BAK/ vitex/app/modules/users/pub/img/users/ &> /dev/null

The problem is that the first command creates the correct folder under ~/tmp/, but the second command creates vitex/app/modules/users/pub/img/users/img-users.BAK/, which is not good.

The expected behaviour would be that img-users.BAK would move all files back, overwritting any files in the destination folder, leaving any other files untouched.

How can this be done?

Thanks!

Note: recursivity is not necessary as there are no directory under that path.

** Edit **

Step 1 : backup directory

Before

./vitex/app/modules/users/pub/img/
  ./users/
    ./1.png
    ./14.png
    ./README
    ...
./tmp/
  <empty>

After

./vitex/app/modules/users/pub/img/
   <empty>
 ./tmp/
   ./img-users.BAK/
    ./1.png
    ./14.png
    ./README
    ...

Everything is as expected.

Step 2 : Restore directory

Before

./vitex/app/modules/users/pub/img/
  ./users/
    ./1.png
    ./3.png
    ./README
    ...
./tmp/
  ./img-users.BAK/
    ./1.png
    ./14.png
    ./README
    ...

After (actual)

./vitex/app/modules/users/pub/img/
  ./users/
    ./img-users.BAK/
      ./1.png
      ./14.png
      ./README
      ...
    ./3.png
    ./1.png
    ./README
    ...
./tmp/
  <empty>

After (expected)

./vitex/app/modules/users/pub/img/
  ./users/
    ./1.png        <-- overwritten
    ./14.png
    ./3.png
    ./README       <-- overwritten
    ...
./tmp/
  <empty>

Yanick Rochon

Posted 2014-09-17T15:33:49.797

Reputation: 932

Answers

2

The commands you issued are doing exactly the right thing, namely, moving directories. If you need to move or copy the files in the img/users directory back, then do that explictly:

mv -f ~/tmp/img-users.BAK/* vitex/app/modules/users/pub/img/users/

Note that I included the -f which will force overwrite without asking. Be careful. Also, not sure why you want to throw away STDOUT, so I left that off too.

crimson-egret

Posted 2014-09-17T15:33:49.797

Reputation: 1 900

Had to add rm -Rf ~/tmp/img-users.BAK after moving the files, though. But this works. Chose this because of the -f flag, too. – Yanick Rochon – 2014-09-17T20:10:55.370

Probably don't want the -Rf, just rmdir, under the theory that the directory should be empty (ignoring dot files) after the mv completed. Moreover, if the mv failed, you probably really don't want to recursively remove the temp dir and it's contents, which are still useful, presumably. – crimson-egret – 2014-09-17T23:48:43.797

You would be right, however if the process fail, I just revert the VM to the previous snapshot (part of the script), as the mv and rm commands are being executed through SSH. I didn't mention this as it has nothing to do with the question here. – Yanick Rochon – 2014-09-18T00:35:08.280

1

The difference is that in the first case, the destination does not exists, and already exists in the second.

mv ~/tmp/img-users.BAK/ vitex/app/modules/users/pub/img/users/

The second command moves all but the last files into the last directory, so you need to list the all files to move. You can do that with globbing:

mv ~/tmp/img-users.BAK/* vitex/app/modules/users/pub/img/users/

As an (important) side note, you should avoid to have a / at the end of the source directory. It can have very odd side effects, and is not of much use anyway:

From pinfo mv:

**Warning**: Avoid specifying a source name with a trailing slash,
when it might be a symlink to a directory.  Otherwise, `mv' may do
something very surprising, since its behavior depends on the underlying
rename system call.  On a system with a modern Linux-based kernel, it
fails with `errno=ENOTDIR'.  However, on other systems (at least
FreeBSD 6.1 and Solaris 10) it silently renames not the symlink but
rather the directory referenced by the symlink. 

Volker Siegel

Posted 2014-09-17T15:33:49.797

Reputation: 1 188

Do you mean "in the first case, the destination does not exists, and does in the second", instead? – Yanick Rochon – 2014-09-17T15:44:25.620

You move .../users into ~/tmp/img-users.BAK/, so you have ~/tmp/img-users.BAK/users, right? And later, you want the files in ~/tmp/img-users.BAK/users back in the other .../users, right? Yes, it's very unclear which dest exist or not, depends on perspective... – Volker Siegel – 2014-09-17T16:00:14.830

You are right with the "exists" thing. But, I tried and ended up with img/users/users, not img/users/img-users.BAK. Now I'm confused. – Volker Siegel – 2014-09-17T16:07:17.610

See the edit :) – Yanick Rochon – 2014-09-17T17:03:25.417

@VolkerSiegel - chances are that you issued your experiment twice and moved a directory into a previously existing /tmp/img-users.BAK directory. – crimson-egret – 2014-09-17T17:26:10.460

0

If the in-between stage between backup and restore is re-creating the users directory (as seems to be the case), then to restore it, you'll want to either re-name individual files (as has been shown in previous answers, though I would argue that may not be what you want), or, in the restore script, remove the users directory before moving it back into place, so, with backup staying the same (except to take in the trailing-slash issue mentioned in the answer by Volker Siegel:

...
# backup user avatars
mv vitex/app/modules/users/pub/img/users ~/tmp/img-users.BAK

You would add a command to restore, like:

...
# restore user avatars
rm -rf vitex/app/modules/users/pub/img/users
mv ~/tmp/img-users.BAK vitex/app/modules/users/pub/img/users

I've also removed the redirection to /dev/null, because I think in the typical case you won't need it (mv will be silent), whereas if something weird goes on, you'll get the error messages that could be useful in figuring out what happened.

lindes-hw

Posted 2014-09-17T15:33:49.797

Reputation: 168