Hello,
I just finished a first step after struggling a bit with systemd…
Everything is here : GitHub - jsiobj/emonpi at var-log-files
Restoring /var/log file structure at boot
I created a small script and associated service which restores the /var/log/ file structure at boot.
The service is configured to run quite early in the boot process, before the end of basic.target
which is before most “apps” services (using WantedBy=basic.target
) but after all fs are mounted (using After=local-fs.target
)
I found a nice cp
trick : --attributes-only
which only copies directories and files with proper attributes but with no data, that is only create empty files. That is a quick and efficient way of backing up and restoring the structure.
Here is boot process logs summary
Dec 8 22:01:29 emonpi rsyslogd: [origin software="rsyslogd" swVersion="8.4.2" x-pid="522" x-info="http://www.rsyslog.com"] start
Dec 8 22:01:29 emonpi systemd-modules-load[112]: Inserted module 'i2c_bcm2708'
Dec 8 22:01:29 emonpi systemd-modules-load[112]: Inserted module 'i2c_dev'
Dec 8 22:01:29 emonpi systemd[1]: Mounted Configuration File System.
...
Dec 8 22:01:29 emonpi systemd[1]: Reached target Local File Systems.
...
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/apache2’ -> ‘/var/log/apache2’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/apache2/error.log’ -> ‘/var/log/apache2/error.log’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/emoncms.log’ -> ‘/var/log/emoncms.log’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/logrotate’ -> ‘/var/log/logrotate’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/mosquitto’ -> ‘/var/log/mosquitto’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/mqtt_input.log’ -> ‘/var/log/mqtt_input.log’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/mysql’ -> ‘/var/log/mysql’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/mysql/error.log’ -> ‘/var/log/mysql/error.log’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/mysql.log’ -> ‘/var/log/mysql.log’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/ntp_update.log’ -> ‘/var/log/ntp_update.log’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/oem-varlog.flag’ -> ‘/var/log/oem-varlog.flag’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/openhab’ -> ‘/var/log/openhab’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/redis’ -> ‘/var/log/redis’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/redis/redis-server.log’ -> ‘/var/log/redis/redis-server.log’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/service-runner.log’ -> ‘/var/log/service-runner.log’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/supervisor’ -> ‘/var/log/supervisor’
Dec 8 22:01:29 emonpi varlog-init.sh[290]: ‘/home/pi/emonpi/logtools/log.template/supervisor/supervisord.log’ -> ‘/var/log/supervisor/supervisord.log’
Dec 8 22:01:29 emonpi systemd[1]: Started Creates required directories and file in /var/log.
...
Dec 8 22:01:29 emonpi systemd[1]: Reached target Basic System.
Dec 8 22:01:29 emonpi systemd[1]: Starting Mosquitto MQTT Broker...
Dec 8 22:01:29 emonpi systemd[1]: Started Mosquitto MQTT Broker.
Dec 8 22:01:29 emonpi systemd[1]: Starting Node-RED graphical event wiring tool....
Dec 8 22:01:29 emonpi systemd[1]: Started Node-RED graphical event wiring tool..
So it seems to work as expected : log files are ready before apps services startup.
I have created a ~/emonpi/logtools
directory where there is
-
log.template which contains the “default” set of log files
-
oem-varlog.install : a (very) simple install script for the service
-
oem-varlog.service : the systemd unit file
-
varlog-init.sh : the service script which is doing all the work (I finally created a script because of the limitations of systemd interpreter)
On startup, 2 directories (there structures and empty files) are copied :
-
~/emonpi/logtools/log.template
which is the default set of logs
-
~/data/log.template
which is (will be) the backed up file structure from actual /var/log
That way, default log files are in the RO part so the user can’t mess it up and actual log files are stored in the RW fs. Using --attributes-only
combine with -n
(no overwrite) makes it a very light process.
But… as of now, backup of structure does not occurs on system shutdown or reboot and I have hard time to track down the issue… as I have no persistent log !
Anyway… I’ll keep trying. If ou have any clue of the reason why it won’t work, please let me know.
Full log files backup
Digging a little bit more into logrotate, although it cannot use another fs to rotate logs, it has a postrotate
feature which could be used to run some rsync for instance. It is to be noted that rotated files should not / can not be moved from their origin directory as it will probably prevent logrotate to work properly.
But thinking about it anyway, it is probably not possible to backup all log files on shutdown, especially syslog, as you may not be able to back it up late enough in the shutdown process. But still, it shoud work well for apps logs.
That’s all for today. Any comment is welcome.
JS