0

Is it possible to make a copy of a file if the content has changed to achieve a simple versioning?

myFolderInclSubfolderBackupWithVersioning.sh < source folder> < dest >

e.g folder on source

  • file1.jpg 100K (today)

folder on destination, like external HDD:

  • file1.jpg size 100K (today)
  • file1(2).jpg size 99K (yesterday)
  • file1(1).jpg size 101K (2 monts ago)

Thank you!

5
  • You can use cp --backup or rsync --backup and rsync --backup-dir. These options are described in the man pages for the two commands, and I recommend that you test them in a small test environment before using them, so that you know what will happen, and that it will be useful for you.
    – sudodus
    Jun 21, 2022 at 14:55
  • 1
    Most good backup applications will automatically determine if the content has changed, and won't re-backup files that have not changed. You do not need to re-invent this wheel.
    – user535733
    Jun 21, 2022 at 17:55
  • Thank you! If I'd stick to cp command: 'cp -r --backup=t source target # rename foo → foo.jpg~1~ (or foo.jpg~2~, etc)' is it possible to change format like this: 'foo.~1~.jpg (or foo.~2~.jpg, etc)'?
    – ssssstut
    Jun 23, 2022 at 12:01
  • computerhope.com/unix/ucp.htm#version-control says that backup=t and backup=numbered is the same. Do I have to use --force to aviod such a message: 'cp: 'test1.jpg' and 'test1.jpg' are the same file'?
    – ssssstut
    Jun 23, 2022 at 12:25
  • No, you should be happy that it warns you and refuses to overwrite itself. It would probably destroy the file. The cp --backup=t syntax should be used when writing to another directory.
    – sudodus
    Jun 25, 2022 at 7:05

1 Answer 1

0

The following setting will work:

  • a working directory work (can have subdirectories)
  • a backup directory backup alongside the working directory (in a real case you might have the backup directory in an external drive)

This command will backup from work to backup:

cp -r --backup=t work/* backup/

Example

$ find
.
./work
./work/file:name
./work/foo.sh
./work/file-0
./work/dollarfraga1
./work/dollarfraga2
./work/sub
./work/sub/hello
./work/dollarfraga
./backup
$ cp -r --backup=t work/* backup/
$ find
.
./work
./work/file:name
./work/foo.sh
./work/file-0
./work/dollarfraga1
./work/dollarfraga2
./work/sub
./work/sub/hello
./work/dollarfraga
./backup
./backup/file:name
./backup/foo.sh
./backup/file-0
./backup/dollarfraga1
./backup/dollarfraga2
./backup/sub
./backup/sub/hello
./backup/dollarfraga
$ cp -r --backup=t work/* backup/
$ find
.
./work
./work/file:name
./work/foo.sh
./work/file-0
./work/dollarfraga1
./work/dollarfraga2
./work/sub
./work/sub/hello
./work/dollarfraga
./backup
./backup/file:name
./backup/dollarfraga1.~1~
./backup/foo.sh
./backup/dollarfraga.~1~
./backup/file-0
./backup/file:name.~1~
./backup/dollarfraga1
./backup/dollarfraga2.~1~
./backup/file-0.~1~
./backup/dollarfraga2
./backup/sub
./backup/sub/hello
./backup/sub/hello.~1~
./backup/dollarfraga
./backup/foo.sh.~1~
$ cp -r --backup=t work/* backup/
$ find
.
./work
./work/file:name
./work/foo.sh
./work/file-0
./work/dollarfraga1
./work/dollarfraga2
./work/sub
./work/sub/hello
./work/dollarfraga
./backup
./backup/file:name
./backup/foo.sh.~2~
./backup/dollarfraga1.~1~
./backup/file-0.~2~
./backup/foo.sh
./backup/file:name.~2~
./backup/dollarfraga1.~2~
./backup/dollarfraga2.~2~
./backup/dollarfraga.~1~
./backup/file-0
./backup/dollarfraga.~2~
./backup/file:name.~1~
./backup/dollarfraga1
./backup/dollarfraga2.~1~
./backup/file-0.~1~
./backup/dollarfraga2
./backup/sub
./backup/sub/hello
./backup/sub/hello.~2~
./backup/sub/hello.~1~
./backup/dollarfraga
./backup/foo.sh.~1~

It is possible to use cp like this, but I would recommend, that you learn rsync, which is a much more advanced tool for copying directory structures including backup, or maybe some dedicated backup tool. rsync and those other tools will only copy files that are new or have changed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .