Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Backup script.
  5. # The backups are stored in /var/backups/local and the log
  6. # in /var/logs/backup.log
  7. #
  8. # Parameters:
  9. # $1 - File where all the references are stored.
  10. #
  11. # Not tested with folders with spaces.
  12. #
  13. {
  14. CACHE_REFERENCES="";
  15.  
  16. # Log
  17. BACKUP_LOG="/var/log/backup.log";
  18. #BACKUP_LOG="backup.log";
  19.  
  20. # Fichero por defecto.
  21. #REFERENCES_FILE="/etc/default/references.backup";
  22. REFERENCES_FILE="references.backup";
  23.  
  24. # Directorio de backups.
  25. BACKUP_DIR="/var/backups/local";
  26.  
  27. # Fecha de ejecucion
  28. NOW=`date '+%Y%m%d%H%M'`;
  29.  
  30. #
  31. # Backup del directorio/fichero.
  32. # Param: Directorios o ficheros. Con secuencias de escape.
  33. #
  34. function backup_files() {
  35.  
  36. tar cpfz "$BACKUP_DIR$NOW.backup.tar.gz" $1 1> /dev/null 2>> "$BACKUP_LOG";
  37.  
  38. return $?;
  39.  
  40. }
  41.  
  42. #
  43. # Lectura del fichero de referencias.
  44. # Param: Fichero de referencias.
  45. # Return: 0 si se ha leido correctamente. 1 otherwhise.
  46. #
  47. function read_references() {
  48.  
  49. if [ ! -f "$1" ]; then {
  50. echo "[$NOW] File $1 not found. Backup failed." >> "$BACKUP_LOG";
  51. return 1;
  52. } fi;
  53.  
  54. COUNT=0;
  55. ERRORS=0;
  56.  
  57. while read LINE; do {
  58.  
  59. FILE=`echo "$LINE" | grep '^\ *[^#].*$'`;
  60. if [ "$FILE" == "" ]; then {
  61. continue;
  62. } elif [ -e "$FILE" ]; then {
  63. if [ "$CACHE_REFERENCES" != "" ]; then {
  64. CACHE_REFERENCES="$CACHE_REFERENCES $FILE";
  65. } else {
  66. CACHE_REFERENCES="$FILE";
  67. } fi;
  68.  
  69. [ $? -eq 0 ] && echo "[$NOW] Added file $FILE to backup." >> "$BACKUP_LOG";
  70. } else {
  71. echo "[$NOW] File $FILE not found. Not added." >> "$BACKUP_LOG";
  72. let "ERRORS=$ERRORS+1";
  73. } fi;
  74.  
  75. let "COUNT=$COUNT+1";
  76.  
  77. } done < "$1";
  78.  
  79. echo "[$NOW] Read $COUNT files/directories." >> "$BACKUP_LOG";
  80. [ $ERRORS -ne 0 ] && echo "[$NOW] Found $ERRORS errors." >> "$BACKUP_LOG";
  81.  
  82. [ $COUNT -ne $ERRORS ] && return 0;
  83. return 1;
  84. }
  85.  
  86. #
  87. # Main
  88. #
  89. {
  90.  
  91. if [ ! -f "$BACKUP_LOG" ]; then {
  92. touch "$BACKUP_LOG";
  93. } fi;
  94.  
  95. if [ "$1" != "" ]; then {
  96. REFERENCES_FILE="$1";
  97. } fi;
  98.  
  99. echo "[$NOW] Init backup with the references in $REFERENCES_FILE" >> "$BACKUP_LOG";
  100.  
  101. read_references "$REFERENCES_FILE";
  102.  
  103. [ $? -eq 0 ] && backup_files "$CACHE_REFERENCES";
  104.  
  105. if [ $? -ne 0 ]; then {
  106. echo "[$NOW] Backup failed." >> "$BACKUP_LOG";
  107. exit 1;
  108. } fi;
  109.  
  110. echo "[$NOW] Backup successful." >> "$BACKUP_LOG";
  111. exit 0;
  112.  
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement