Skip to content

Instantly share code, notes, and snippets.

@timabell
Last active February 8, 2024 20:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save timabell/68d112d66623d9a4a3643c86a93debee to your computer and use it in GitHub Desktop.
Save timabell/68d112d66623d9a4a3643c86a93debee to your computer and use it in GitHub Desktop.
#!/bin/bash -v
# backing up a vm
cd /media/tim/WD6/
base="/home/tim/VirtualBox VMs"
src=win10-2018
mv $src.tar.lz4 $src.tar.lz4.old
ls -lh "$base/$src"
du -sh "$base/$src"
df -h "$base"
df -h .
du -sb "$base/$src"
du -sb "$base/$src" | awk '{print $1}'
tar -cpC "$base" "$src" -P | pv -s $(du -sb "$base/$src" | awk '{print $1}') | lz4 >> "$src.tar.lz4"
# output like this:
# 8.79GiB 0:00:43 [ 205MiB/s] [=====================================================================> ] 99%
#!/bin/sh
# https://gist.github.com/timabell/68d112d66623d9a4a3643c86a93debee#file-backup-sh
# lz4 is better than gz for large backups because you end up I/O bound on disk instead of CPU bound
# refs:
# * https://stackoverflow.com/questions/24063846/how-to-use-tar-with-lz4#24086155
# * https://superuser.com/questions/168749/is-there-a-way-to-see-any-tar-progress-per-file/665181#665181
# * https://stackoverflow.com/questions/8228047/adding-timestamp-to-a-filename-with-mv-in-bash/8229220#8229220
# Note that progress is inaccurate and variable because it's based on input bytes processed vs total, but the speed is limited by
# output bytes to the spinning rust backup disk, and the ratio varies with the compressability of the input data.
#sudo apt update
#sudo apt install liblz4-tool pv
# backing up a home folder
# set up folder on backup drive
echo "Opening/creating backup folder..."
mountpoint=/media/tim/backup/
cd "$mountpoint"
mkdir -p fox
cd fox
base=/home/
src=tim
echo "Getting source folder size..."
#size_human=`du -sh "$base/$src"`
size_bytes=`du -sb "$base/$src" | awk '{print $1}'`
echo "$size_bytes bytes to backup."
echo "Backing up..."
tar -cpC $base $src -P | pv -s "$size_bytes" | lz4 >> "$(date -d "today" +"%Y%m%d-%H%M")-home.tar.lz4"
echo "Done."
#!/bin/sh -v
cd restorepath/
pv /media/tim/WD6/thebackup.tar.lz4 | lz4 -dc - | tar -xv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment