79

When backing up with rsync, How do I keep the full directory structure?

For example, the remote server is saturn, and I want to backup saturn's /home/udi/files/pictures to a local directory named backup.

I want to have (locally) backup/home/udi/files/pictures rather than backup/pictures.

Adam Matan
  • 13,194
  • 19
  • 55
  • 75

4 Answers4

102

Use the -R or --relative option to preserve the full path.

Dennis
  • 109
  • 4
Dentrasi
  • 3,752
  • 1
  • 24
  • 19
20

Another common related use case it to keep a selected part of the directory tree with /./, e.g. if you wanted:

/home/udi/files/pictures

to go to:

backup/files/pictures

you can write:

rsync --relative /home/udi/./files/pictures backup

The magic /./ gets specially parsed by rsync before it passes that path to the system calls, and tells it where to start creating directories from. The /./ means nothing for other POSIX utilities in general, e.g. the path a/./b is the same as a/b in POSIX.

2

With the Cygwin Windows rsync, and assuming the remote rsync is pointing to the root, I'd do:

rsync -vtrz --delete server::rsyncid/home/udi/files/pictures /cygdrive/d/backup/home/udi/files

That will put the contents of the remote pictures directory in /backup/home/udi/files/pictures. Presumably the syntax under unix would be similar.

JR

John Rennie
  • 7,776
  • 1
  • 23
  • 35
  • It is not useful because we need to reproduce the whole directory hierarchy on the local side before executing the rsync command. – Ludovic Kuty Apr 02 '12 at 11:42
0

You can make changes in cron due to when you want to start rsync

rsync -avz -e ssh ruser@rserver:/home/udi/files/pictures/* /backup/pictures/

also you may add ssh key to remote server connect without passwd