[HOWTO] save log files when using no changes / always fresh approach in Port 3,4,5.

Post tutorials, HOWTO's and other useful resources here.
User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[HOWTO] save log files when using no changes / always fresh approach in Port 3,4,5.

Post#1 by Rava » 23 Oct 2022, 20:43

Maybe you are like me, wanting to have no changes and saving all settings only manually, having the system forget everything else. IT Dementia by choice, so to speak, at least locally where you have the power doing so.

But then you encounter an issue and want at least the tail part of e.g. /var/log/messages saved?

E.g. in my case because of ongoing issues with suspend.

The 1st script goes back to the moons now long gone, during the development phase of port 5.0

The 2nd script I added recently when Port 5.0 finale was released.

The 1st script works on its own, but only with adding the 2nd script I now am finally happy with how it all works together, since the 2nd script removes all older entries while adding basic info /var/log/messages would not provide, while letting /var/log/messages be still appended into the now saved to a harddrive file.

To make it more easy for you, I will change the paths that are hardcoded in my script. Please edit these to suit your needs.

At least the 2nd script is meant to be in the folder on some harddisk where you want the log saved to survive a possible system crash.

Both scripts can sit on any file system, you can use them on VFAT, NTFS, ext[234], any device the root user can write to.

Cave! To really be prepared for any kind of crash or system failure you might encounter and want to catch as much logging info about it is best you alter the standard way Porteus boots and starts up your chosen DE:
By using the "3" cheatcode as in init 3 as in "Slackware standard mode - multiuser with network" as in: boot into the virtual text console and not into your GUI.


Interestingly some folks also prefer my manual startx approach like here https://ikkiboot.tuxfamily.org/en/news.html
In case of Porteus startup problem in UEFI, choose Text Mode as startup choice, connect with the guest/guest account and then launch the GUI with the startx command.
(highlighting by me)

You will thus end up at this text prompt instead:

Code: Select all

===============================================================================
 Welcome to Porteus                                                        v5.0
===============================================================================

 The system is up and running now.

 Login as root with password toor or as guest with password guest.
 If you're new to Porteus, visit http://www.porteus.org/ for more info.

 After you login, try the following commands:

 mc ....... to start Midnight Commander (edit/copy/move/create/delete files)
 init 4 ... to run Xwindow system

 Other commands you may find useful:

 activate ..... to insert (install) Porteus module into the system on the fly
 deactivate ... to remove (uninstall) Porteus module from the root filesystem
 pns-tool ..... to setup internet connection

 When finished, use "poweroff" or "reboot" command and wait until it completes
===============================================================================

porteus login:
1st log in as root (password is "toor" when you not changed that) and cd into the directory on your hard drive that you choose as target for your logging. I recommend a directory that is not otherwise used, let's say it is /mnt/sda2/backup/

There you find the two scripts (actually, they are 3 - one for logging /var/log/messages - you have to be root for doing this) [one for logging /home/guest/.xfce4-session.verbose-log - I explain that later, you can do that as guest] and one for altering the log the 1st script created by only adding newest info and deleting older unneeded stuff since you have been able to run the script, meaning the system could not have crashed.



tail+tee_VLM_sda2.sh
VLM is short for "/var/log/messages"; sda2 means it is meant to save the log onto sda2.

Code: Select all

#!/bin/bash
mydate=$(date +%Y-%m-%d)
echo mydate suffix I am using: $mydate
myfile=/mnt/sda2/backup/var_log_messages_$mydate
echo myfile I am using: $myfile
echo $(date +%d.%m.%Y\ %H:%M:%S) started: $0 >>$myfile
echo '# cat /etc/porteus/*' >>$myfile
cat /etc/porteus/* >>$myfile
echo '# uname -r' >>$myfile
uname -r >>$myfile
tail -fn 10 /var/log/messages|tee -a $myfile
The tail means all is output onto the virtual console but also written to the file.
recall that the date suffix is the date your system booted when you run tail+tee_VLM_sda2.sh first thing after bootup.
It will be running in the background and not bother you, it will keep running even when X will exit, only when all programs are terminated (either by init, e.g. a reboot or shutdown, or via REISUB, or due to electricity blackout then the logging will stop because then tee will also have stopped. But you will have the very last log that happened and got reported into /var/log/messages saved in /mnt/sda2/backup/
Let us presume today as in date +%Y-%m-%d is 2022-10-21 - thus your created log file will be /mnt/sda2/backup/var_log_messages_2022-10-21

Even when you keep your system on and only use suspend once in a while and the date changes, say to "2022-10-23" the log file will still be var_log_messages_2022-10-21 since that was the date you started tail+tee_VLM_sda2.sh.

Now you want to start your DE.
What I do: I switch to another virtual console [e.g. via Alt+Cursor-Right - or Alt+F2] log in as guest and type

Code: Select all

startx
and press enter and my DE will start.
You can also log in as guest and use "init 4" instead, then your usual greetings and GUI log in screen will appear as set up by your chosen DE.

Be it as it may, now you have a root virtual console running in the background while also having started your usual chosen DE.

Now to the 2nd script.
If you use a NTFS or VFAT partition you can do that from a normal guest terminal emulator. In XFCE, that would be xfce4-terminal .
If you use a Linux kind of partition, only root can alter the file so you also need to be root for the 2nd script.

If /mnt/sda2/backup is on a Linux partition (ext2/3/4 etcetera): Open a new tab in your favourite terminal emulator and type

Code: Select all

su -
and give your root password (that's "toor" by default)
Now you are root and stay as root. Navigate to /mnt/sda2/backup and run the 2nd script -cat-into-var_log_messages.sh - like so:

Code: Select all

root@porteus:/mnt/sda2/backup# ./cat-into-var_log_messages.sh 2022-10-21
cat-into-var_log_messages.sh needs to be in the folder where you also have your log file that tail+tee_VLM_sda2.sh created or else it would not find it. Therefore you need to execute it via ./cat-into-var_log_messages.sh and not as cat-into-var_log_messages.sh since that folder will not be in your $PATH.

cat-into-var_log_messages.sh

Code: Select all

#!/bin/bash
echo cat-into-var_log_messages.sh V0.2  #V0.2 added dx / fx / sx

# functions
dx () 
{ 
    echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;
    /bin/df -Tm $* | grep -vE 'tmpfs|/mnt/live/run|squashfs'
}

function sx () 
{ 
    echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;
    { 
        read firstLine;
        echo "$firstLine";
        while read f t s u p; do
            let "s2 = $s / 1024";
            let "u2 = $u / 1024";
            printf '%-40s%-16s%-8s%-8s%-8s\n' $f $t $s2 $u2 $p;
        done
    } < /proc/swaps
}

function fx ()
{	echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;
	/bin/free -m
}

# this is fxsx:
#fx|head -n 4;t;sx|tail -n 3

if [ $# -ne 1 ]; then
	echo "ERROR: give one parameter- the date suffix of the var_log_messages_ file 
you want to write to
e.g. 2022-03-25"
	exit 1
fi
myfile=var_log_messages_$1
echo myfile is: $myfile
if [ -w $myfile ]; then
	echo File found, writeable… overwriting it with listing of activated modules and fxsx # and dx /
	cat /etc/porteus/* >$myfile
	uname -r >>$myfile
	ls /mnt/live/memory/images/ >> $myfile
	dx / >> $myfile
	fx|head -n 4;sx|tail -n 3 >> $myfile
	echo '____ What follows is cat $myfile: ______________________________________________'
	cat $myfile
else
	echo File not found or not writeable. Abort!
	exit 2
fi
As you can see, what it writes into the log file differs in one aspect, the one line of

Code: Select all

echo $(date +%d.%m.%Y\ %H:%M:%S) started: $0 >>$myfile
is missing.

Cave!
function sx is written for my needs, and it presumes there are 2 swap devices - one swap partition on the harddrive and one via /dev/zram0


When you have more than two or only one, you have to adjust the script (the "sx|tail -n 3 part) accordingly.

If you have none, remove the function sx () part and alter the line

Code: Select all

fx|head -n 4;sx|tail -n 3 >> $myfile
into

Code: Select all

fx|head -n 4 >> $myfile
I hope these scripts are helpful to some of you.
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[HOWTO] save log files when using no changes / always fresh approach in Port 3,4,5.

Post#2 by Rava » 13 Dec 2022, 19:44

I created a helper script for handling the calling of ./cat-into-var_log_messages.sh more convenient, and also calling at its end this:

Code: Select all

ls -oa --color=auto --time-style=long-iso $myfile
It uses a shortened version of how the target file is addressed - not by the long way as ./cat-into-var_log_messages.sh is called:
./cat-into-var_log_messages.sh YYYY-MM-DD
e.g.

Code: Select all

./cat-into-var_log_messages.sh 2022-12-13
but shortened:
./cat+ls-helper.sh MM-DD
e.g.

Code: Select all

./cat+ls-helper.sh 12-13
and the year is taken from the current system date/time.

Of course that will fail if you started the logging e.g. on Dec 31st and now is Jan 1st. In that case you need to either stop the logging from the virtual terminal and start it anew so that it uses the current year once more, and then you can also again use ./cat+ls-helper.sh.

Or you have to only use e.g. this
./cat-into-var_log_messages.sh 2022-12-31 ; ls -oa --color=auto --time-style=long-iso var_log_messages_2022-12-31
that I also had to use every time until I created the helper script that does the calling of cat-into-var_log_messages.sh and also does the ls of the newly created log file.

This is the helper script I called cat+ls-helper.sh - it has some verbose info output to make it easier to spot any errors that could occur.

Code: Select all

#!/bin/bash
if [ $# -ne 1 ]; then
	echo "ERROR: give one parameter- the month and day suffix part of of the 
var_log_messages_ file you want to write to and list
 format is MM-DD -- e.g. 03-14 for March the 14th.

The year is taken from the system date/time."
	exit 1
fi
mysuffix=$(date +%Y-)$1
echo "mysuffix is: $mysuffix"
myfile=var_log_messages_$mysuffix
echo "myfile is  : $myfile"

if [ -w $myfile ]; then
	echo File found, writeable… will call ./cat-into-var_log_messages.sh $mysuffix
else
	echo File not found or not writeable. Abort!
	exit 2
fi
./cat-into-var_log_messages.sh $mysuffix
ls -oa --color=auto --time-style=long-iso $myfile
Cheers!
Yours Rava

Post Reply