Skip to content

Instantly share code, notes, and snippets.

@xanoni
Last active August 3, 2021 06:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xanoni/e94ff2c47ef5d35569c8acb522c3d4d3 to your computer and use it in GitHub Desktop.
Save xanoni/e94ff2c47ef5d35569c8acb522c3d4d3 to your computer and use it in GitHub Desktop.
scp-delete-during-transfer.sh: for when you're running out of disk space and need to scp a file out while deleting chunks of it
#! /usr/bin/env -S bash -e
SSH_USERHOST="user@host"
REMOTE_PATH="/tmp/disk_backup.img"
OUT_PATH="./$(basename ${REMOTE_PATH})"
TMP_PATH="/tmp/ssh-big-copy"
SLICES=200
echo -e "\nGetting from ${SSH_USERHOST}: ${REMOTE_PATH}\n"
TOT_SZ=$(ssh ${SSH_USERHOST} "du -b ${REMOTE_PATH}" | egrep -o '^[0-9]+')
SLICE_SZ_M=$((TOT_SZ/SLICES/1024/1024+1))
echo -e "Total size: ~$((TOT_SZ/1024/1024))M"
echo -e "Per slice: ~${SLICE_SZ_M}M (${SLICES} slices)"
mkdir "${TMP_PATH}"
for i in $(seq -w 1 ${SLICES} | sort -nr); do
echo -e "\nGetting slice with findex ${i} ...\n"
TO_SKIP_B=$((TOT_SZ-(SLICE_SZ_M*1024*1024)))
if [ ${TO_SKIP_B} -lt 0 ]; then TO_SKIP_B=0; fi
ssh "${SSH_USERHOST}" "dd if=\"${REMOTE_PATH}\" bs=1M iflag=skip_bytes skip=${TO_SKIP_B} count=${SLICE_SZ_M}" > "/tmp/ssh_out.tmp.${i}"
ssh "${SSH_USERHOST}" "truncate -s \"${TO_SKIP_B}\" \"${REMOTE_PATH}\""
TOT_SZ=${TO_SKIP_B}
echo -e "Remaining size = ${TOT_SZ}\n"
done
echo -en "Writing ... "
cat /tmp/ssh_out.tmp.* > "${OUT_PATH}"
md5sum "${OUT_PATH}"
echo -e "\nCleaning up ..."
rm -r "${TMP_PATH}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment