You are not logged in.

#1 2018-04-26 20:31:16

pmozzati
Member
From: Monselice (ITA)
Registered: 2018-01-18
Posts: 9

[SOLVED] Sending mail through s-nail and GMail

Hi to all,
I'm trying to write a backup bash script that sends me a notification via mail when it does the job. I decided to perform the back-up to Google Drive using rclone. I scheduled the job using  a systemd timer file. Every step seems to work properly except for the notification through mail. I use the address related to the Google Drive account in order to send the mail.

Here the code I used to configure s-nail in the script:

echo -e "Il backup del $TODAY è stato eseguito regolarmente" | mailx -:/ -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S mta=smtp://smtp.gmail.com:587 -S from="myaddress@gmail.com" -S smtp-auth-user="myaddress@gmail.com" -S  smtp-auth-password="per_app_password here" -S ssl-verify=ignore -S nss-config-dir=/root/certs -Sttycharset=utf-8 -Ssendcharset=it_IT.UTF-8   -s "Backup del $TODAY" my_other_address@yxxxxx.com

Giving the above command in a terminal produces the expected result. Running it in the script never sends me a mail. I can't figure out what is the difference. Maybe I'm missing some basic knowledge about scripting in bash.
Any advice would be useful.

Last edited by pmozzati (2018-05-01 15:09:57)

Offline

#2 2018-04-26 20:36:58

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,701

Re: [SOLVED] Sending mail through s-nail and GMail

pmozzati wrote:

I decided to perform the back-up to Google Drive using rclone. I scheduled the job using  a systemd timer file.

The script is not running as you.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

#3 2018-04-29 20:17:56

pmozzati
Member
From: Monselice (ITA)
Registered: 2018-01-18
Posts: 9

Re: [SOLVED] Sending mail through s-nail and GMail

Thanks ewaller for your quick reply. I run the command in the post #1 as root and the mail was sent. Did you menan this? 
The whole scrips runs fine except for mail's notification. I put the config file for rclone in /root. So I guess the script can access root folder when it executes. Also certs folder (used by mailx) is in /root.

Here the script's code:

#!/bin/bash
gotodate ()
{ # use 0 for current day. no argument = +1 day
date -d "$1 days" +%Y%m%d
}


TODAY=$(gotodate 0)
TWODAYSAGO=$(gotodate -3)

LASTBACKUPFILE=/root/lastbackup.log
RCLONECONFIGFILE=/root/.config/rclone/rclone.conf
LOGFILE=/root/safe.log


if [ ! -e "$LASTBACKUPFILE" ]; then
echo "$TODAY" >> "$LASTBACKUPFILE"
fi

LASTBACKUP=$(tail -n 1 "$LASTBACKUPFILE")


if [[ $TWODAYSAGO < $LASTBACKUP ]]; then
	rclone --config="$RCLONECONFIGFILE" sync /home/documents safe:current
	echo "Sync eseguito in data $TODAY" >> "$LOGFILE"
else
	rclone --config="$RCLONECONFIGFILE" sync /home/documents safe:current --backup-dir safe:"old_${TODAY}"
	
	echo -e "Il backup del $TODAY è stato eseguito regolarmente" | mailx -:/ -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S mta=smtp://smtp.gmail.com:587 -S from="backup_account@gmail.com" -S smtp-auth-user="backup_account@gmail.com" -S  smtp-auth-password="password_here" -S ssl-verify=ignore -S nss-config-dir=/root/certs -Sttycharset=utf-8 -Ssendcharset=it_IT.UTF-8   -s "Backup del $TODAY" other_address@yxxxxx.com

	
	if [ $? -ne  0 ]; then
		echo "per qualche motivo non è stata mandata la mail" >> /root/error_mail.log
		echo "$USER" >> /root/error_mail.log
		echo "$LOGIN" >> /root/error_mail.log
		echo "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ " >> /root/error_mail.log
		echo "" >> /root/error_mail.log		
	fi

	echo "$TODAY" >> "$LASTBACKUPFILE"
	echo "Backup completo eseguito in data $TODAY" >> "$LOGFILE"
fi

exit 0

I run this script through systemd service:

cat /etc/systemd/system/keepsafe.service 
[Unit]
Description=Backup important data once a day on saveyourthingshere[at]gmail[dot]com

[Service]
Type=oneshot
ExecStart=/usr/bin/keepsafe.sh

[Install]
WantedBy=multi-user.target

and timer unit file

cat /etc/systemd/system/keepsafe.timer 
[Unit]
Description=Runs keepsafe.service once a day

[Timer]
# Time to wait after booting before we run first time
OnBootSec=5min
# Time between running each consecutive time
OnUnitActiveSec=1d
Unit=keepsafe.service

[Install]
WantedBy=multi-user.target

After the service had run I find in /root foder the file dead.letter:

# cat dead.letter 
From root Sun Apr 29 17:18:53 2018
Date: Sun, 29 Apr 2018 17:18:53 +0200
From: backupmail@gmail.com
To: other_address@yandex.com
Subject: Backup del 20180429
Message-ID: <20180429151853.pK6Yl%backupmail@gmail.com>
User-Agent: mail v14.9.10
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Il backup del 20180429 =C3=A8 stato eseguito regolarmente

this string =C3=A8 replaces the character è in the body of the mail.

I'd like to know who actually runs a system script. Is the root user? Where is the right place to put the configuration file in for a system script?
Many thanks in advance.
Paolo

Last edited by pmozzati (2018-04-30 20:28:01)

Offline

#4 2018-04-30 20:36:49

pmozzati
Member
From: Monselice (ITA)
Registered: 2018-01-18
Posts: 9

Re: [SOLVED] Sending mail through s-nail and GMail

Update: run the script /usr/bin/keepsafe.sh as root and worked. I think this problem should concern who executes a system's script. I can't go further for now. Should I post the question in System Administration category?

Offline

#5 2018-04-30 23:29:19

ajbibb
Member
Registered: 2012-02-12
Posts: 142

Re: [SOLVED] Sending mail through s-nail and GMail

I think the System Administration sub-fourm is now locked for new questions.  When ewaller said the script is not running as you, it also means (I think) it is not running as root, or a least not with root's environment.  I had what sounds like the exact same problem a year or two or three back with a backup script called by a systemd timer.  I think scripts called that way the are run as a system process with no environment set up for the process.  I tried calling the mail program with the full path and program name and that was not sufficient to overcome the environment apparently not existing.

You may have noticed a lot of weasel words in the preceding paragraph.  They are there mainly because it is what I think was happening, I never really found out.  My solution was horrible, but worked for my specific use case.  It involved calling the script from an su command in the service file and passing the user (root) and root password to the su command.  In other words password hard coded into the service file.  I have been hesitating to even write that because it is in general a terrible idea, but as I said, it works in my specific case and allowed me to move onto other things.  The systemd/timers wiki page  contains some paragraphs (section 6.3) about sending emails from a systemd timer,   The wiki page probably describes the proper way to do what you want.

Offline

#6 2018-05-01 15:09:32

pmozzati
Member
From: Monselice (ITA)
Registered: 2018-01-18
Posts: 9

Re: [SOLVED] Sending mail through s-nail and GMail

@ajbibb
I'm actually astonished. I did noting but rewriting the  mail command in this form:

echo -e "Il backup del $TODAY è stato eseguito regolarmente" | mailx -v  -:/ -S expandaddr -Sv15-compat -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S mta=smtp://address:password@smtp.gmail.com:587 -S from="address@gmail.com"  -S ssl-verify=ignore -S nss-config-dir=/root/certs -S ttycharset=utf-8 -S sendcharset=it_IT.UTF-8 -s "Backup del $TODAY" paolo.mozzati@yandex.com

I set the expandaddr variable to pass the text body as echo command pipe (read in man mailx) and it works. I didn't try to run the script as a specific user. Can you achieve the job done usin User option in systemd unit file?
As for now I marked the post as solved. But i don't actually know why. I would read the paragraph you mentioned! thanks

Offline

Board footer

Powered by FluxBB