SHARE
TWEET

Untitled

a guest Jun 25th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # Variables (aka config)
  4. # Script executed after making backup of /var/log (none for no script to execute)
  5. passScript=""
  6. # Remove syslog, auth.log, kern.log, after backup? ("true"/"false")
  7. removeAfterVarlog="true"
  8. # Remove backup tar from /tmp. Good when uploading with passScript ("true"/"false")
  9. removeAfterTar="false"
  10.  
  11. err_exit () {
  12.     echo "Exiting..."
  13.     exit
  14. }
  15.  
  16. # Check if we can read /var/log files
  17. if [ -d "/var/log" ]; then
  18.     echo "/var/log Exists"
  19. else
  20.     echo "[ERR] /var/log Does not exist!"
  21.     err_exit
  22. fi
  23.  
  24. for i in "auth.log" "kern.log" "syslog"
  25. do
  26.     if [ -r "/var/log/$i" ]; then
  27.         echo "/var/log/$i Is readable"
  28.     else
  29.         echo "[ERR] /var/log/$i Does not exist, or is not readable!"
  30.         err_exit
  31.     fi
  32. done
  33.  
  34. # Check if we have every command we need
  35. for i in "du" "md5sum" "tail" "tar" "xz"
  36. do
  37.     if [ -x "$(command -v $i)" ]; then
  38.         echo "Found $i"
  39.     else
  40.         echo "[ERR] $i Not found, or not executable!"
  41.         err_exit
  42.     fi
  43. done
  44.  
  45. # Check how much space will tar take
  46. tarSpace=$(du -hc /var/log | tail -1)
  47. echo "Backup will take around $tarSpace"
  48.  
  49. # Check if /tmp exists, check if theres no another file with same name, and write a tar archive
  50. if [ ! -d "/tmp" ]; then
  51.     echo "[ERR] /tmp Does not exist! What the hell?!"
  52.     err_exit
  53. fi
  54.  
  55. echo "Making tar archive of /var/log..."
  56. tarFileName="$(date "+%d.%m.%y_%R")_varlog.tar"
  57. echo "Name: /tmp/$tarFileName"
  58. if [ -f /tmp/$tarFileName ]; then
  59.     echo "[ERR] /tmp/$tarFileName exists!"
  60.     err_exit
  61. fi
  62. tar cvf /tmp/$tarFileName /var/log/*
  63. xz /tmp/$tarFileName
  64. tarFileName="$tarFileName.xz"
  65.  
  66. # Check if /tmp/tar is made properly
  67. if [ ! -f /tmp/$tarFileName ]; then
  68.     echo "[ERR] /tmp/$tarFileName does not exist! Something went wrong!"
  69.     err_exit
  70. fi
  71.  
  72. # Make md5sum of backup tar
  73. printf "md5: "
  74. md5sum /tmp/$tarFileName
  75.  
  76. # Execute another script with tar filename passed
  77. ## This is used for remote backup, or whatever you want to do with backup file
  78. if [ ! $passScript = "" ]; then
  79.     echo "Running $passScript"
  80.     $passScript "/tar/$tarFileName"
  81. else
  82.     echo "No passScript specified"
  83. fi
  84.  
  85. # Remove things from /var/log       **CRITICAL**
  86. if [ $removeAfterVarlog = "true" ]; then
  87.     echo "Removing old logs..."
  88.     rm /var/log/kern.log* /var/log/auth.log* /var/log/syslog*
  89. fi
  90.  
  91. # Remove backup file
  92. if [ $removeAfterTar = "true" ]; then
  93.     echo "Removing backup tar..."
  94.     rm /tmp/$tarFileName
  95. fi
  96.  
  97. # Done
  98. echo "Done, have a nice day"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top