daily pastebin goal
10%
SHARE
TWEET

transientlog.sh

quakeman Jan 4th, 2017 24 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: transientlog
  4. # Required-Start:
  5. # Required-Stop:
  6. # X-Start-Before:   $syslog
  7. # X-Stop-After:     $syslog
  8. # X-Interactive:    yes
  9. # Default-Start:    2 3 4 5
  10. # Default-Stop:     0 1 6
  11. # Short-Description:    Keeps /var/log in RAM
  12. # Description: Moves the contents of /var/log to RAM during boot
  13. #              and keeps it there until shutdown/reboot, when it
  14. #              copies the contents back to permanent storage.
  15. ### END INIT INFO
  16.  
  17. # Version 1.0
  18. # Author: Matteo Cortese <matteo_cortese@fastwebnet.it>
  19. #
  20. # Inspired by ramlog by Jan Andrejkovic.
  21. #
  22. # This program is free software; you can redistribute it and/or modify
  23. # it under the terms of the GNU General Public License as published by
  24. # the Free Software Foundation; either version 2 of the License, or
  25. # (at your option) any later version.
  26.  
  27. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  28. NAME=transientlog
  29. DESC="Transient log directory"
  30. LOCKFILE=/var/lock/$NAME.lock
  31. VARLOG=/var/log
  32. RSYNC="rsync"
  33. RSYNCCONF="--recursive --delete-before --links --checksum --perms --partial --times --group --owner --devices --specials"
  34.  
  35. # Read configuration variable file if it is present
  36. [ -r /etc/default/$NAME ] && . /etc/default/$NAME
  37.  
  38. # Load the VERBOSE setting and other rcS variables
  39. . /lib/init/vars.sh
  40.  
  41. # Define LSB log_* functions.
  42. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  43. . /lib/lsb/init-functions
  44.  
  45. if [ -z "$VARLOGPERM" ]; then
  46.     var=`dirname $VARLOG`
  47.     log=`basename $VARLOG`
  48.     VARLOGPERM="$var/permanent$log"
  49.     unset var log
  50. fi
  51.  
  52.  
  53. do_start()
  54. {
  55.     # Return
  56.     #   0 if transient log has been started
  57.     #   1 if transient log was already running
  58.     #   2 if transient log could not be started
  59.  
  60.     [ -f $LOCKFILE ] && return 1
  61.  
  62.     # Check if I'm root
  63.     [ `id -u` -eq 0 ] || return 2
  64.  
  65.     # If VARLOG does not exist?
  66.     [ -d $VARLOG ] || return 2
  67.  
  68.     # VARLOGPERM either does not exist (first invocation)
  69.     # or is empty (left from previous invocation).
  70.     #
  71.     [ -d $VARLOGPERM ] || mkdir -p $VARLOGPERM || return 2
  72.  
  73.     # Populate the tmpfs
  74.     $RSYNC $RSYNCCONF $VARLOGPERM/ $VARLOG
  75.     if [ $? -eq 0 ]; then
  76.         # Success!
  77.         touch $LOCKFILE
  78.         return 0
  79.     fi
  80.  
  81.     # Something went wrong...
  82.     return 2
  83. }
  84.  
  85. do_stop() {
  86.     # Return
  87.     #   0 if daemon has been stopped
  88.     #   1 if daemon was already stopped
  89.     #   2 if daemon could not be stopped
  90.     #   other if a failure occurred
  91.  
  92.     [ -f $LOCKFILE ] || return 1
  93.  
  94.     # Check if I am root
  95.     [ `id -u` -eq 0 ] || return 2
  96.  
  97.     # Merge back to permanent storage
  98.     $RSYNC $RSYNCCONF $VARLOG/ $VARLOGPERM
  99.  
  100.     rm -f $LOCKFILE
  101.  
  102.     return 0
  103. }
  104.  
  105. do_reload() {
  106.     # Return
  107.     #   0 if transient log has been reloaded
  108.     #   1 if transient log was not running
  109.     #   2 if transient log could not be reloaded
  110.  
  111.     [ -f $LOCKFILE ] || return 1
  112.  
  113.     # Check if I am root
  114.     [ `id -u` -eq 0 ] || return 2
  115.  
  116.     # Merge back to permanent storage
  117.     $RSYNC $RSYNCCONF $VARLOG/ $VARLOGPERM
  118.     touch $LOCKFILE
  119.  
  120.     return 0
  121. }
  122.  
  123. case "$1" in
  124.   start)
  125.     [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
  126.     do_start
  127.     status=$?
  128.     case $status in
  129.         0|1) [ "$VERBOSE" != no ] && echo 10 >> /temp/start.log && log_end_msg 0 ;;
  130.         2) [ "$VERBOSE" != no ] && echo 11 >> /temp/start.log && log_end_msg 1 ;;
  131.     esac
  132.     exit $status
  133.     ;;
  134.   stop)
  135.     [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  136.     do_stop
  137.     status=$?
  138.     case $status in
  139.         0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  140.         2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  141.     esac
  142.     exit $status
  143.     ;;
  144.   status)
  145.     if [ -f $LOCKFILE ]; then
  146.         echo "$DESC is running"
  147.     else
  148.         echo "$DESC is stopped"
  149.     fi
  150.     exit 1
  151.     ;;
  152.   reload)
  153.     log_daemon_msg "Reloading $DESC" "$NAME"
  154.     do_reload
  155.     status=$?
  156.     log_end_msg $status
  157.     exit $status
  158.     ;;
  159.   *)
  160.     echo "Usage: $0 {start|stop|status|reload}" >&2
  161.     exit 3
  162.     ;;
  163. esac
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