A short How-To:
History:
I am a big proponent of doing things yourself rather than grabbing the latest "tool" some else provides. To this end, several years ago I wrote a custom script to automate the taking of snapshots and making backups of my BTRFS subvolumes rather than trying to use "snapper" or other available tools written by someone else. Especially since when snapper was released a lot of people complained about it filling up their drives with snapshots. The script I wrote is called "dailysnapshots" but it has morphed into a snapshot-backup-update script it this point. Kind of an all-purpose maintenance tool.
The need to log:
Since this script runs at 5:30am when my system is usually idle, I wanted to be able to review what it has done - especially since I added updating my system to it. So with one line in your bash script, you can log activity and output to /var/log/syslog by using this as the first line in your script:
The "-t $(basename $0)" part puts the name of the bash script in the log lines so I can tell which are from this script. The "-s" and "2>&1" make sure activity along with standard and error output are logged. Here's some sample output:
The downside to this is a few hundred lines of logging from this script are buried in syslog which can be tens of thousands of lines. Not a big deal since I can just "grep" for the script name and get the few hundred lines I want to see.
Improving on the above:
I decided this week to look into separating the 'dailysnapshot' activity from syslog to make it simpler to review. It turns out there is a fairly easy way to do this. As I understand it, there are 8 "local" logs reserved for system use - local0 to local7. They are not used in *buntu systems so free for us to use.
The steps:
File ownership is a critical piece. I made the log file then correctly set ownership:
Step 2:
To add the file to logger, edit /etc/rsyslog.d/50-default.conf and add the log to the bottom of the file:
and restart the logger service:
Now you can test to make sure it going to work:
Note the absence of "sudo" here - "syslog" is the file owner so sudo is not needed.
Step 3:
I want this log rotated like all the others, so to do that I edited /etc/logrotate.d/rsyslog and added my log file to the list right below /var/log/messages and before the first bracket:
Step 4:
Finally, I edited my script so it logs to the new log file instead of syslog. Since I now have my own log the script name is redundant, so I removed the script name from the log entry to make the lines in the log a bit shorter:
Now the morning activities of my script are logged and easily retrieved.
History:
I am a big proponent of doing things yourself rather than grabbing the latest "tool" some else provides. To this end, several years ago I wrote a custom script to automate the taking of snapshots and making backups of my BTRFS subvolumes rather than trying to use "snapper" or other available tools written by someone else. Especially since when snapper was released a lot of people complained about it filling up their drives with snapshots. The script I wrote is called "dailysnapshots" but it has morphed into a snapshot-backup-update script it this point. Kind of an all-purpose maintenance tool.
The need to log:
Since this script runs at 5:30am when my system is usually idle, I wanted to be able to review what it has done - especially since I added updating my system to it. So with one line in your bash script, you can log activity and output to /var/log/syslog by using this as the first line in your script:
Code:
exec 1> >(logger -s -t $(basename $0)) 2>&1 # Log the script activity
Code:
Jun 10 05:36:04 office dailysnapshots: Delete subvolume (commit): '/subvol/snapshots/@KDEneon_backup-new' Jun 10 05:36:05 office dailysnapshots: Create a snapshot of '/subvol/@KDEneon' in '/subvol/snapshots/@KDEneon_daily_0' Jun 10 05:36:05 office dailysnapshots: Delete subvolume (commit): '/subvol/snapshots/@KDEneon_home_backup-new' Jun 10 05:36:05 office dailysnapshots: Create a snapshot of '/subvol/@KDEneon_home' in '/subvol/snapshots/@KDEneon_home_daily_0'
Improving on the above:
I decided this week to look into separating the 'dailysnapshot' activity from syslog to make it simpler to review. It turns out there is a fairly easy way to do this. As I understand it, there are 8 "local" logs reserved for system use - local0 to local7. They are not used in *buntu systems so free for us to use.
The steps:
- Create the log file with correct ownership.
- Add the file to logger
- Add the log to log rotate
- Add the log to your script
File ownership is a critical piece. I made the log file then correctly set ownership:
Code:
sudo touch /var/log/dailysnapshots.log sudo chown syslog:adm /var/log/dailysnapshots.log
To add the file to logger, edit /etc/rsyslog.d/50-default.conf and add the log to the bottom of the file:
Code:
local7.* /var/log/dailysnapshots.log
and restart the logger service:
Code:
sudo systemctl restart syslog
Code:
logger -p local7.info "Sample log entry using logger command"
Code:
cat /var/log/dailysnapshots.log Jun 11 10:58:57 office stuart: Sample log entry using logger command
I want this log rotated like all the others, so to do that I edited /etc/logrotate.d/rsyslog and added my log file to the list right below /var/log/messages and before the first bracket:
Code:
/var/log/syslog /var/log/mail.info /var/log/mail.warn /var/log/mail.err /var/log/mail.log /var/log/daemon.log /var/log/kern.log /var/log/auth.log /var/log/user.log /var/log/lpr.log /var/log/cron.log /var/log/debug /var/log/messages /var/log/dailysnapshots.log { rotate 4 weekly missingok notifempty compress delaycompress sharedscripts postrotate /usr/lib/rsyslog/rsyslog-rotate endscript }
Finally, I edited my script so it logs to the new log file instead of syslog. Since I now have my own log the script name is redundant, so I removed the script name from the log entry to make the lines in the log a bit shorter:
Code:
exec 1> >(logger -s -p local7.info) 2>&1 # Log to custom log