[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: scp overwriting precaution?



* Greg Marks <gtmarks@gmail.com> [20-02/26=We 21:48 -0600]:
> [...] if I accidentally copy an older version of
> a file on my laptop to a newer version of the
> file on my server, the newer version is lost.
>
> Is it possible to configure my server so that [it creates a
> backup on the server of any existing file that is] overwritten?

This is built-in to rsync with its options --backup,
--backup-dir, and --suffix.  Generally, rsync is so much
more capable than scp that I never use scp any more.

* [full original post]:
> When using scp to copy files from my server to my laptop (both running
> Debian 10 and both with the same directory tree), I like to back up
> the files in case I discover that I've overwritten a newer version of
> a file with an older version.  (I seem to make this mistake about once
> or twice a year.)  To this end, on my laptop I've defined the following
> "get from server" function:
> 
>    files="";
>    for file in "${@}";
>       do
>          if [[ -f $file ]]
>             then
>                cp -i -v "$file" /tmp/"$file".`date +\%Y.\%m.\%d.\%H.\%M.\%S`.backup
>          fi
>       done
>    for file in "${@}"
>       do
>          files="$files $(pwd | sed 's/\/home\/<username1>/\/home\/<username0>/g')/\"$file\""
>       done
>    scp -T -p <username0>@[Server IP Address]:"$files" .
> 
> If I discover that I've made a mistake, there's a backup copy in /tmp.
> 
> I'd like to be able to create backup copies on my server when sending
> files in the other direction.  At the moment, I have a "send to server"
> function on my laptop that simply reads:
> 
>    scp -p $@ <username0>@[Server IP Address]:`pwd | sed 's/\/home\/<username1>/\/home\/<username0>/g'`/
> 
> But if I accidentally copy an older version of a file on my laptop to
> a newer version of the file on my server, the newer version is lost.
> 
> Is it possible to configure my server so that when it receives a remote
> scp request to write to a file on the server, it parses the request
> and backs up to /tmp any existing file on the server that is about to
> be overwritten?
> 
> Regards,
> Greg Marks


Reply to: