CP Question RE: Checking if file is smaller than destination
Hi,
I'm currently using cp to copy files over night to a backup server using: cp -pru /mnt/server-01/documents /home/backups/ This only copies files if they are newer than the destination (or if the destination file doesn't exist). What this won't protect me against is if someone overwrites a file with a blank one. The blank document will be newer than the old so it will overwrite the backup copy. Is there a way to check if the file size is smaller, and if so create a different copy for example: /home/backups/documents/me/quotes/computers.odt (100kb) /home/backups/documents/me/quotes/computers-2.odt (94kb) I hope that makes sense, thanks! P.S. If cp cannot do this, what can? |
You could use the -size option of the find cmd http://www.computerhope.com/unix/ufind.htm to check the size, passing in the size of the src file.
Might be overkill for each file. The easiest might be to generate sorted lists of src & dest dirs, then compare same named file sizes (is use ls -l). Basically, I think you need a short script. |
Thanks for your suggestions. As there is a huge amount of data, running a find may take far too long.
I found that with cp you can specify the backup switch (-b) which creates a backup of the old file. For example: /home/backups/documents/me/quotes/computers.odt~ (100kb) /home/backups/documents/me/quotes/computers.odt (94kb) (newer) The older of the two files has the ~ symbol at the end of the file name. These are always the two newest files (the backup file gets updated at the same time). I think this is the easiest method, but it would be nice if it was a bit more flexible (for example to store 3 copies of the file rather than two. I know you can have it number these files, but the hard drive will fill up very quickly as there's no limit to the number it increases to!). |
Like a I said, a simple/quick soln is just to ls -l src and destn dirs, piped through sort, and compare file sizes.
|
All times are GMT -5. The time now is 10:42 PM. |