umnt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/sh
  2. # mount the usb stick in a specific directory and archive
  3. # it when unmounting.
  4. # TODO
  5. # - Finish implimenting restore and new
  6. # - Better error handling
  7. # - Rotate backups
  8. bak_dir="${HOME}/arc"
  9. bak_host='user@trusted.lan:arc'
  10. enc_host='user@untrusted.net:arc'
  11. case "$1" in
  12. 'sdy')
  13. dsk_id='b70ca4dea25c4679.a'
  14. mnt_dir="${HOME}/sdy"
  15. bak_file="Study-`date "+%y%m%d%M%S"`.tar.gz"
  16. ;;
  17. 'dat')
  18. dsk_id='.a'
  19. mnt_dir="${HOME}/dat"
  20. bak_file="Data-`date "+%y%m%d%H%M%S"`.tar.gz"
  21. ;;
  22. 'snd')
  23. dsk_id='.a'
  24. mnt_dir="${HOME}/snd"
  25. bak_file="Music-`date "+%y%m%d%H%M%S"`.tar.gz"
  26. ;;
  27. 'img')
  28. dsk_id='e5c3fc7e2c093e38.a'
  29. mnt_dir="${HOME}/img"
  30. bak_file="Photos-`date "+%y%m%d%H%M%S"`.tar.gz"
  31. ;;
  32. *)
  33. echo "usage: $0 [complex things here]"
  34. exit
  35. ;;
  36. esac
  37. mnt() {
  38. printf 'Mounting on %s\n' "$mnt_dir"
  39. doas mount "$dsk_id" "$mnt_dir"
  40. mount | grep "$mnt_dir"
  41. echo ''
  42. df -h "$mnt_dir"
  43. }
  44. umnt() {
  45. printf 'Unmounting disk\n'
  46. doas umount "$dsk_id" && rm -rf "$mnt_dir"
  47. }
  48. bak() {
  49. printf 'Writing backup to %s/%s\n' "$bak_dir" "$bak_file"
  50. cd "$mnt_dir"
  51. tar -czf "${bak_dir}/${bak_file}" ./
  52. du -h "${bak_dir}/${bak_file}"
  53. }
  54. rbak() {
  55. scp "${bak_dir}/${bak_file}" "${bak_host}/${bak_file}"
  56. }
  57. new() {
  58. # unfinished, unconfigured, never ran..
  59. # blanks a usb stick with new single ffs sd2a
  60. # need to copy disklabel for restore
  61. printf 'Device name? [sd2]: '
  62. read new_dsk
  63. if [ "$opt" = '\n' ] ; then
  64. new_disk='sd2'
  65. fi
  66. doas fdisk -i "$new_dsk"
  67. doas disklabel -E "$new_dsk" # label (e), create (a), save (w)
  68. doas newfs "${new_dsk}a"
  69. }
  70. enc_bak() {
  71. cd "$bak_dir"
  72. openssl enc -aes-256-cbc -in "${bak_file}" -out "${bak_file}.enc"
  73. # gpg2 --output "${bak_file}.gpg" --symmetric "${bak_file}"
  74. du -h "${bak_dir}/${bak_file}.enc"
  75. }
  76. dec_bak() {
  77. cd "$bak_dir"
  78. openssl enc -d -aes-256-cbc -in "${bak_file}.enc" -out "${bak_file}"
  79. # gpg2 --output "${bak_file}" --decrypt "${bak_file}.gpg"
  80. }
  81. if [ "$(mount | grep "$mnt_dir")" = '' ] ; then
  82. if [ -d "$mnt_dir" ] ; then
  83. printf 'Mount point %s exists. Ok? ' "$mnt_dir" && read opt
  84. else
  85. mkdir "$mnt_dir"
  86. fi
  87. mnt
  88. else
  89. printf 'Create backup? [Y/n]: ' && read opt
  90. if [ "$opt" != 'n' ] ; then
  91. bak
  92. printf 'Send backup to remote host? [Y/n]: ' && read opt
  93. if [ "$opt" != 'n' ] ; then
  94. rbak
  95. fi
  96. printf 'Encrypt backup? [Y/n]: '
  97. read opt
  98. if [ "$opt" != 'n' ] ; then
  99. enc_bak
  100. printf 'Send encrypted backup to remote host? [Y/n]: ' && read opt
  101. if [ "$opt" != 'n' ] ; then
  102. bak_file="${bak_file}.enc"
  103. bak_host="$enc_host"
  104. rbak
  105. fi
  106. fi
  107. fi
  108. umnt
  109. fi