You are not logged in.
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.comGiving 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
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
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 0I 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.targetand 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.targetAfter 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 regolarmentethis 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
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
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
@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.comI 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