From 7d825982ae5126033e8a2ce2165cab591b7b32a1 Mon Sep 17 00:00:00 2001 From: Nick Daly Date: Wed, 30 Nov 2016 11:24:41 -0600 Subject: [PATCH 1/4] Write sync status to HDD_LOG/log2ram.log. - Store sync status directly in the HDD log, to make sure we know about it if something fails. This won't help if the HDD_LOG variable is borked, but it'll help with everything else. - Made sync a function to avoid repeating five lines three times. - log2ram now requires (cp or rsync) and tee, mount, umount. --- log2ram | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/log2ram b/log2ram index 53ec5ec..032962b 100644 --- a/log2ram +++ b/log2ram @@ -2,37 +2,34 @@ HDD_LOG=/var/log.hdd/ RAM_LOG=/var/log/ +LOG2RAM_LOG="${HDD_LOG}log2ram.log" SIZE=40M USE_RSYNC=false +sync () { + if [ "$USE_RSYNC" = true ]; then + rsync -aXWv --delete --links $HDD_LOG $RAM_LOG 2>&1 | tee -a $LOG2RAM_LOG + else + cp -rfup $HDD_LOG -T $RAM_LOG 2>&1 | tee -a $LOG2RAM_LOG + fi +} + case "$1" in start) [ -d $HDD_LOG ] || mkdir $HDD_LOG mount --bind $RAM_LOG $HDD_LOG mount --make-private $HDD_LOG mount -t tmpfs -o nosuid,noexec,nodev,mode=0755,size=$SIZE log2ram $RAM_LOG - if [ "$USE_RSYNC" = true ]; then - rsync -aXWv --delete --links $HDD_LOG $RAM_LOG - else - cp -rfup $HDD_LOG -T $RAM_LOG - fi + sync ;; stop) - if [ "$USE_RSYNC" = true ]; then - rsync -aXWv --delete --links $RAM_LOG $HDD_LOG - else - cp -rfup $RAM_LOG -T $HDD_LOG - fi + sync umount -l $RAM_LOG umount -l $HDD_LOG ;; write) - if [ "$USE_RSYNC" = true ]; then - rsync -aXWv --delete --links $RAM_LOG $HDD_LOG - else - cp -rfup $RAM_LOG -T $HDD_LOG - fi + sync ;; esac From e238849069946f00f85402408c40090e5a78104c Mon Sep 17 00:00:00 2001 From: Nick Daly Date: Wed, 30 Nov 2016 11:41:41 -0600 Subject: [PATCH 2/4] Sync the ramlog back to disk, hourly. - Since the cron job could be called before the user reboots after install (or if log2ram is disabled), we check that the hdd log location exists before syncing. - Copy the log2ram.hourly sync script to cron's hourly directory on install. --- install.sh | 6 ++++-- log2ram | 3 +++ log2ram.hourly | 3 +++ 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 log2ram.hourly diff --git a/install.sh b/install.sh index 94e7b9b..08aea60 100644 --- a/install.sh +++ b/install.sh @@ -7,8 +7,10 @@ then cp log2ram /usr/local/bin/log2ram chmod a+x /usr/local/bin/log2ram systemctl enable log2ram - + cp log2ram.hourly /etc/cron.hourly/log2ram + chmod +x /etc/cron.hourly/log2ram + echo "Reboot to activate log2ram" -else +else echo "You need to be ROOT (sudo can be used)" fi diff --git a/log2ram b/log2ram index 032962b..297d514 100644 --- a/log2ram +++ b/log2ram @@ -7,6 +7,9 @@ SIZE=40M USE_RSYNC=false sync () { + [ -d $HDD_LOG ] || echo "ERROR: $HDD_LOG doesn't exist! Can't sync." + [ -d $HDD_LOG ] || exit 1 + if [ "$USE_RSYNC" = true ]; then rsync -aXWv --delete --links $HDD_LOG $RAM_LOG 2>&1 | tee -a $LOG2RAM_LOG else diff --git a/log2ram.hourly b/log2ram.hourly new file mode 100644 index 0000000..df43424 --- /dev/null +++ b/log2ram.hourly @@ -0,0 +1,3 @@ +#! /bin/sh + +/usr/local/bin/log2ram write From ac75c3dac1df6ac4519adb71c86901789beadae4 Mon Sep 17 00:00:00 2001 From: Nick Daly Date: Wed, 30 Nov 2016 12:29:28 -0600 Subject: [PATCH 3/4] Merge in the split-sync changes from master. --- log2ram | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/log2ram b/log2ram index 297d514..53a7341 100644 --- a/log2ram +++ b/log2ram @@ -6,7 +6,18 @@ LOG2RAM_LOG="${HDD_LOG}log2ram.log" SIZE=40M USE_RSYNC=false -sync () { +syncToDisk () { + [ -d $HDD_LOG ] || echo "ERROR: $HDD_LOG doesn't exist! Can't sync." + [ -d $HDD_LOG ] || exit 1 + + if [ "$USE_RSYNC" = true ]; then + rsync -aXWv --delete --links $RAM_LOG $HDD_LOG 2>&1 | tee -a $LOG2RAM_LOG + else + cp -rfup $RAM_LOG -T $HDD_LOG 2>&1 | tee -a $LOG2RAM_LOG + fi +} + +syncFromDisk () { [ -d $HDD_LOG ] || echo "ERROR: $HDD_LOG doesn't exist! Can't sync." [ -d $HDD_LOG ] || exit 1 @@ -17,22 +28,23 @@ sync () { fi } + case "$1" in start) [ -d $HDD_LOG ] || mkdir $HDD_LOG mount --bind $RAM_LOG $HDD_LOG mount --make-private $HDD_LOG mount -t tmpfs -o nosuid,noexec,nodev,mode=0755,size=$SIZE log2ram $RAM_LOG - sync + syncFromDisk ;; stop) - sync + syncToDisk umount -l $RAM_LOG umount -l $HDD_LOG ;; write) - sync + syncToDisk ;; esac From 8d440eab6c71e23c4aed0e183edc63d8555645cc Mon Sep 17 00:00:00 2001 From: Nick Daly Date: Wed, 30 Nov 2016 13:09:39 -0600 Subject: [PATCH 4/4] Exclude log2ram.log from rsync so rsync doesn't delete it on sync. --- log2ram | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) mode change 100644 => 100755 log2ram diff --git a/log2ram b/log2ram old mode 100644 new mode 100755 index 53a7341..535613b --- a/log2ram +++ b/log2ram @@ -5,15 +5,16 @@ RAM_LOG=/var/log/ LOG2RAM_LOG="${HDD_LOG}log2ram.log" SIZE=40M USE_RSYNC=false +LOG_OUTPUT="tee -a $LOG2RAM_LOG" syncToDisk () { [ -d $HDD_LOG ] || echo "ERROR: $HDD_LOG doesn't exist! Can't sync." [ -d $HDD_LOG ] || exit 1 if [ "$USE_RSYNC" = true ]; then - rsync -aXWv --delete --links $RAM_LOG $HDD_LOG 2>&1 | tee -a $LOG2RAM_LOG + rsync -aXWv --delete --exclude log2ram.log --links $RAM_LOG $HDD_LOG 2>&1 | $LOG_OUTPUT else - cp -rfup $RAM_LOG -T $HDD_LOG 2>&1 | tee -a $LOG2RAM_LOG + cp -rfup $RAM_LOG -T $HDD_LOG 2>&1 | $LOG_OUTPUT fi } @@ -22,9 +23,9 @@ syncFromDisk () { [ -d $HDD_LOG ] || exit 1 if [ "$USE_RSYNC" = true ]; then - rsync -aXWv --delete --links $HDD_LOG $RAM_LOG 2>&1 | tee -a $LOG2RAM_LOG + rsync -aXWv --delete --exclude log2ram.log --links $HDD_LOG $RAM_LOG 2>&1 | $LOG_OUTPUT else - cp -rfup $HDD_LOG -T $RAM_LOG 2>&1 | tee -a $LOG2RAM_LOG + cp -rfup $HDD_LOG -T $RAM_LOG 2>&1 | $LOG_OUTPUT fi }