Skip to main content

stdin2sftp

A very simple pump from stdin to a remote file via sftp. Useful for when one cannot just pipe over ssh to cat or dd, for example. when remote sftp provider is limited to sftp-only, like OpenSSH with ChrootDirectory and internal-sftp, an rclone serve sftp or proftpd with mod_sftp,

Why?

I had a need to make a efficient backups of virtual machines disk images, which means copy-on-write snapshot of disk image, then transfer over the network while applying compression, without the need to first locally save compressed disk image and without blocking the host system or guest virtual machine during the process. The solution for that is to use reflinks as a copy-on-write snapshots, and then stream the content of reflinked disk image while compressing in between, to remote server. Sadly, neither scp nor sftp support reading from pipe., dropping not a file' error. This way the source image is not blocked and is immutable during the backup process regardless how how long all of the compression and transfer take, at no point in tiem I have a whole copy of the disk image and there’s no need to write whole image compressed to disk before transfer.

Real life usage

Make zero-copy snapshot of the Virtual Machine disk image:

cp -a --reflink=always vm_1.qcow2 vm_1.qcow2_snapshot

And upload it over the network, while adding compression to remote file:

zstd -6 < vm_1.qcow2_snapshot | \
stdin2sftp \
  -u backupuser \
  -H backuphost.example.com \
  -f "/mnt/backups/vm_dumps/vm_1/$(date '+%Y-%m-%d_%H-%M-%S').qcow2.zstd"

And if one is worried about saturating network link, an bandwidth limit can be applied with pv. Since with reflink, the source disk image is not blocked regardless of how long the transfer takes:

zstd -6 < vm_1.qcow2_snapshot | \
pv -L 64M | \
stdin2sftp \
  -u backupuser \
  -H backuphost.example.com \
  -f "/mnt/backups/vm_dumps/vm_1/$(date '+%Y-%m-%d_%H-%M-%S').qcow2.zstd"

after which the snapshot can be discarded:

rm -f vm_1.qcow2_snapshot

Metadata

Release files for stdin2sftp 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stdin2sftp 0.1.0
File Size Uploaded
stdin2sftp-0.1.0.tar.gz 3.5 kB Details

Release files / stdin2sftp-0.1.0.tar.gz

Download URL stdin2sftp-0.1.0.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d284c0947b9eb635235f5f967c3e34ace7f7a08ed8e229620de4b86bf03818ac
BLAKE2b-256 checksum
How to use checksums
f1c36093f85ab83140e342e42b222011053555c295b86ed55f0bb3938addebf7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page