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

Re: scp overwriting precaution?

On 2020-02-26 19:48, Greg Marks wrote:
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:

    for file in "${@}";
          if [[ -f $file ]]
                cp -i -v "$file" /tmp/"$file".`date +\%Y.\%m.\%d.\%H.\%M.\%S`.backup
    for file in "${@}"
          files="$files $(pwd | sed 's/\/home\/<username1>/\/home\/<username0>/g')/\"$file\""
    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?

Greg Marks

My preferred solution is ZFS and zfs-auto-snapshot. You set up a cron job and snapshots/ cleaning take place automatically according to whatever time schedules and retention counts you pick. If you damage or destroy files or directories, the '/.zfs/snapshot' directory is your friend. If you inflict mass damage on your filesystem, the 'zfs rollback' command is your friend.

When you have two or more ZFS pools and/or machines, you can use replication to backup entire filesystems, including snapshots, clones, etc..

I switched to FreeBSD on my SOHO CVS and Samba server about a year ago. ZFS boot, encrypted swap, and encrypted ZFS root are all installer options, which makes for a very nice OOTB experience.

I have been using Debian GNU/Linux for many years, first with ext* filesystems, then btrfs, and now ext4. I recently started experimenting with ZFS on Linux (ZOL) via the following packages:


apt-get pulled in most of the required packages, but licensing requires ZFS to be built and the build failed. STFW I realized that I needed to install linux-headers-amd64 (choose the correct package for your architecture). I believe I uninstalled zfs-dkms and then installed again to get everything to finish correctly. If ZOL works, I may migrate /home.


Reply to: