Skip to content

Instantly share code, notes, and snippets.

@mfherbst
Created September 18, 2018 10:43
Show Gist options
  • Save mfherbst/c38d5acb0b6c48e90054c53d805f73e7 to your computer and use it in GitHub Desktop.
Save mfherbst/c38d5acb0b6c48e90054c53d805f73e7 to your computer and use it in GitHub Desktop.
#!/bin/bash
### BEGIN INIT INFO
# Provides: varFolders
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 1 2 3 4 5
# Default-Stop: 0 6
# Short-Description: Backup and restore var folder structures
### END INIT INFO
#
# Script Version 1.0 (14/09/2013)
#
##################################################################################
# Licence:
#
# (C) 2013 Michael F. Herbst <info@michael-herbst.com>
#
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# It is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License can be found
# at <http://www.gnu.org/licenses/>.
#
##################################################################################
# Usage:
#
# run with "stop" to create a tar archive representing the structure of a sub-
# directory of var. This means that no actual files, only symbolic links and
# folders are kept. Run with "start" to restore this structure back to the original
# location.
#
# The idea is that this allows write-intensive subfolders of var (eg. log, spool .. )
# to be mounted as ramdisks using tmpfs in a SSD-only system to reduce the amounts
# of writes the SSD suffers.
#
# DIRS are the subfolers of var which are subject to this treatment. With the
# KEEPFILES the user can control if only the structure or everything should be
# stored. (Eg. if something is debugged and files should be kept across power cyles)
#
# The script also checks for presence of /etc/var_folders_keep_files. If this file
# is present the script will automatically set KEEPFILES="y".
#
# If KEEPFILES="n" the archive will only be updated if the names of the subfolders /
# links have changed or if subfolders / links have been created / deleted. Other
# changes (eg permission changes, owner changes, ...) won't trigger an update
# automatically. You can however force an update at the next invocation of "stop"
# by calling varFolders with the option "force-update".
#
##################################################################################
# Settings:
KEEPFILES="n" #Keep the files or only the folder structure?
DIRS="log spool" #dirs under /var to extract folder structure/files from
ARFILE="var_folders.tar" #Archive to keep stuff in (stored under /var/local)
##################################################################################
. /lib/lsb/init-functions
set -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin
PROG=varFolders
initialise(){
#set DIRS, DIR1 and KEEPFILES
local DTMP=
for DIR in $DIRS; do
if [ "$DIR" == "local" ]; then
echo "Can't store the directory structure of /var/local!"
exit 1
fi
[ -z "$DIR1" ] && DIR1="$DIR" #The first valid dir
[ -d "/var/$DIR" ] && DTMP="$DTMP $DIR"
done
DIRS="$DTMP"
if [ -z "$DIRS" ]; then
echo "No valid directory in DIRS"
exit 1
fi
#We go to /var
cd /var
mkdir -p /var/local
[ -e /etc/var_folders_keep_files ] && KEEPFILES="y"
true
}
update_archive() {
if [ "$KEEPFILES" == "y" ]; then
update_archive_keepfiles
return $?
fi
update_archive_normal
return $?
}
update_archive_keepfiles() {
#Create tar with all files
tar cf /var/local/"$ARFILE" $DIRS
chmod 640 /var/local/"$ARFILE"
find $DIRS -type d -o -type l > /var/local/"$ARFILE".list
}
update_archive_normal() {
TMP=`mktemp`
find $DIRS -type d -o -type l > "$TMP"
SUMNEW=`cat "$TMP" | sha512sum`
SUMOLD=
[ -f /var/local/"$ARFILE".list ] && SUMOLD=`cat /var/local/"$ARFILE".list | sha512sum`
if [[ "$SUMOLD" == "$SUMNEW" ]]; then
rm "$TMP"
return
fi
mv "$TMP" /var/local/"$ARFILE".list
tar cf /var/local/"$ARFILE" --no-recursion `cat /var/local/"$ARFILE".list`
chmod 640 /var/local/"$ARFILE"
}
force_update() {
#removing the list file will force the update if stop is called
rm -f "/var/local/${ARFILE}.list"
}
extract_archive() {
[ ! -f /var/local/"$ARFILE" ] && return 1
#Archive has already been extracted:
[ -f /var/${DIR1}/archive_extracted ] && return 0
#Extract:
tar xf /var/local/"$ARFILE"
touch /var/${DIR1}/archive_extracted
}
##################################################################################
case $1 in
start)
log_begin_msg "Starting $PROG"
if ! initialise; then
log_end_msg 1
exit 1
fi
if ! extract_archive; then
log_end_msg 1
exit 1
fi
log_end_msg 0
;;
stop)
log_begin_msg "Stopping $PROG"
if ! initialise; then
log_end_msg 1
exit 1
fi
if ! update_archive; then
log_end_msg 1
exit 1
fi
log_end_msg 0
;;
force-update)
initialise || exit 1
force_update
exit $RET
;;
restart|force-reload|status)
exit 0
;;
*)
echo "Usage: $0 {start|stop|restart|force-reload|force-update|status}" >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment