Centos – move files from one device to another

centosmvshell

I am trying to automate a remote backup on our centOS server, I run the current command :

nice -n 10 mv -vfb /media/localbackup/* /media/remotebackup/

however when this is ran I get the following error and no files are moved

mv: inter-device move failed: `/media/localbackup/04-21-13' to `/media/remotebackup/04-21-13'; unable to remove target: Is a directory

I have been told it is better to use cp however this won't delete the files afterwards and I need to remove the files upon successful backup.

Best Answer

I also suggest you using rsync instead of mv, but adding to @Hartmut's answer:

rsync -abmv --remove-source-files /media/localbackup /media/remotebackup/
find /media/localbackup -depth -type d -empty -delete

And 2 additions here:

  1. The -m (--prune-empty-dirs) parameter to rsync, which on my version 3.0.7 protocol version 30 says: prune empty directory chains from the file-list but doesn't do that for some reason. Doesn't error either (not the only one to notice it).
  2. The find command which will delete the empty directories.

now, the reason against cp && rm is that cp might fail half-way through. The next time you want to backup you will be in a weird situation (partially copied files to name the worst case). Also, the next backup will not know what to do with the previously backed up files which were not deleted. On the other hand rsync covers these aspects just fine.

Also, using a script (as first answer suggests) to do what rsync already does, but worse, is a no-go (sorry Hauke Laging :-) ). No need to reinvent the wheel here.