backup.bash 978 B

12345678910111213141516171819202122232425262728293031
  1. #!/bin/bash
  2. if [ ! -z $2 ]
  3. then
  4. SOURCE_PATH="$(dirname $1)"
  5. SOURCE_DIR="$(basename $1)"
  6. DEST_PATH="$(dirname $2)"
  7. DEST_FILE="$(basename $2).tar.xz"
  8. XZ_OPT=-e9 ionice -c 2 -n 7 nice -n 19 tar -cJhvpnS -C "${SOURCE_PATH}" \
  9. "${SOURCE_DIR}" -f "${DEST_PATH}/${DEST_FILE}"
  10. else
  11. cat << EOF
  12. Usage: $0 /full/path/to/folder/to/be/compressed /path/to/output/file
  13. The resulting file will be 'file.tar.xz' at '/path/to/output/' which is a \
  14. compressed version of folder 'compressed' originally at \
  15. '/full/path/to/folder/to/be/'.
  16. For multiple files, do:
  17. for FILE in apple banana orange; do $0 /path/to/\${FILE} \
  18. /path/to/backup/\${FILE}
  19. The above command will fill /path/to/backup with apple.tar.xz, banana.tar.xz \
  20. and orange.tar.xz
  21. If this yelds memory problems, it's because of XZ_OPT=-e9. I recommend making \
  22. a swap partition:
  23. [ ! -f /swap ] && sudo dd if=/dev/zero of=/swap bs=1024 count=1048576 && sudo \
  24. chmod 0600 /swap && mkswap /swap && swapon /swap
  25. EOF
  26. fi