Can I hard link files with rsync instead of copying them?

6

1

cp -l hard links files instead of copying them, saving filesystem space. I need to use rsync instead of cp because of its --exclude capabilities.

So my question is, how do I get rsync to hard link files instead of copying them? Obviously, this is a local filesystem copy. I've read the docs for rsync's -H option, but it's unclear to me if this behaves the same way as cp -l.

nnyby

Posted 2018-09-22T17:53:11.930

Reputation: 959

It looks like I can get this behavior with --link-dest=$SOURCE_DIR: https://slaptijack.com/system-administration/rsync-backups-conserve-disk-space-with-hard-links/ I'm testing this out now.

– nnyby – 2018-09-22T18:33:30.043

Generally, rsync is used for back-up or archive purposes. Why would you want a back-up of file names without their contents? – AFH – 2018-09-22T18:37:56.893

@AFH my backup script creates rolling backups of gigabytes of data at many points in time, but most of this data is the same. I save disk space by copying these between time points with cp -al. Without using hardlinks, all these different copies of data (if they were real files) wouldn't fit on my 2TB drives. See: https://slaptijack.com/system-administration/rsync-backups-conserve-disk-space-with-hard-links/

– nnyby – 2018-09-22T18:42:25.930

So you can recover from accidental deletions, thought not overwrites, but you'll never be able to use your back-up to recover from a disc failure or partition loss. I use cp -au, which copies only changed files, so the process doesn't take too long, but it does give me an external copy of the latest version of every file for recovery purposes. Of course, to back up Gigabytes of data you need Gigabytes of storage, but if your files are important you need to make that investment. Only unimportant files are not backed up, by definition. – AFH – 2018-09-22T19:02:50.917

Answers

5

The answer has turned out to be yes, with rsync's --link-dest option. It's just not obvious because it's not a simple on/off flag like cp -l, because it takes a path argument.

So, cp -l a/ b/ can also be done like this:

rsync -a --link-dest=a a/ b/

nnyby

Posted 2018-09-22T17:53:11.930

Reputation: 959

Did you try this command yourself? The man page says that --link-dest option, if relative, is relative to the target directory. This answer is more correct: https://serverfault.com/a/210058/251416

– Yaroslav Nikitenko – 2020-01-31T18:11:15.747