fork download
  1. #!/bin/bash
  2.  
  3. # Create BACKUP file in backup destination and add to cron
  4.  
  5. # Error codes
  6. # 1 - Unknown error
  7. # 1xx - fs problems
  8. # 2xx - backup failed
  9.  
  10. ERR_100="Error $1: Backup not mounted"
  11. ERR_110="Error $1: Target directories not available"
  12. ERR_200="Error $1: Backup failed"
  13.  
  14. targets_to_backup="/home/work/scripts/
  15. /home/work/something/
  16. /home/work/notes/
  17. /home/work/somethingelse/
  18. "
  19.  
  20. backup_destination="/mnt/backup/mybox/"
  21.  
  22. function backup_not_mounted
  23. {
  24. # Is backup mounted?
  25. if [[ ! -f "$backup_destination/BACKUP" ]]; then
  26. backup_not_mounted_return=100
  27. else
  28. backup_not_mounted_return=0
  29. fi
  30. }
  31.  
  32. function target_not_available
  33. {
  34. SAVEIFS=$IFS
  35. IFS=$(echo -en "\n\b")
  36. for target in $targets_to_backup
  37. do
  38. if [[ ! -d "$target" ]]; then
  39. target_not_available_return=110
  40. else
  41. target_not_available_return=0
  42. fi
  43. done
  44. IFS=$SAVEIFS
  45. }
  46.  
  47. function exit_error
  48. {
  49. case "$1" in
  50. 100) echo $ERR_100
  51. exit $1
  52. ;;
  53. 110) echo $ERR_110
  54. exit $1
  55. ;;
  56. 200) echo $ERR_200
  57. exit $1
  58. ;;
  59. 0)
  60. ;;
  61. *) echo "Unknown error $1"
  62. exit 1
  63. ;;
  64. esac
  65. }
  66.  
  67. function do_backup
  68. {
  69. SAVEIFS=$IFS
  70. IFS=$(echo -en "\n\b")
  71. for target in $targets_to_backup
  72. do
  73. nice -n 19 ionice -c 3 rsync -avz -R "$target" "$backup_destination"
  74.  
  75. if [[ ! -d "$target" ]]; then
  76. do_backup_return=200
  77. break
  78. else
  79. do_backup_return=0
  80. fi
  81. done
  82. IFS=$SAVEIFS
  83. }
  84.  
  85. function main
  86. {
  87. backup_not_mounted
  88. exit_error $backup_not_mounted_return
  89.  
  90. target_not_available
  91. exit_error $target_not_available_return
  92.  
  93. do_backup
  94. exit_error $do_backup_return
  95. }
  96.  
  97. # RUN!
  98. main
Runtime error #stdin #stdout 0.03s 5312KB
stdin
Standard input is empty
stdout
Error : Backup not mounted