Skip to content

Instantly share code, notes, and snippets.

@iuriguilherme
Created January 5, 2021 21:00
Show Gist options
  • Save iuriguilherme/2598282f3ab43dd4a014cf7a5c477f38 to your computer and use it in GitHub Desktop.
Save iuriguilherme/2598282f3ab43dd4a014cf7a5c477f38 to your computer and use it in GitHub Desktop.
Backup folders using tar
#!/bin/bash
if [ ! -z $2 ]
then
SOURCE_PATH="$(dirname $1)"
SOURCE_DIR="$(basename $1)"
DEST_PATH="$(dirname $2)"
DEST_FILE="$(basename $2).tar.xz"
XZ_OPT=-e9 ionice -c 2 -n 7 nice -n 19 tar -cJhvpnS -C "${SOURCE_PATH}" \
"${SOURCE_DIR}" -f "${DEST_PATH}/${DEST_FILE}"
else
cat << EOF
Usage: $0 /full/path/to/folder/to/be/compressed /path/to/output/file
The resulting file will be 'file.tar.xz' at '/path/to/output/' which is a \
compressed version of folder 'compressed' originally at \
'/full/path/to/folder/to/be/'.
For multiple files, do:
for FILE in apple banana orange; do $0 /path/to/\${FILE} \
/path/to/backup/\${FILE}
The above command will fill /path/to/backup with apple.tar.xz, banana.tar.xz \
and orange.tar.xz
If this yelds memory problems, it's because of XZ_OPT=-e9. I recommend making \
a swap partition:
[ ! -f /swap ] && sudo dd if=/dev/zero of=/swap bs=1024 count=1048576 && sudo \
chmod 0600 /swap && mkswap /swap && swapon /swap
EOF
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment