Problem with Udev Rule for making backups

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
emil_pavlov

Problem with Udev Rule for making backups

Post by emil_pavlov »

I have created a udev to make backups on my external hdd, whenever it is inserted.

Code: Select all

ACTION=="add", SUBSYSTEMS=="usb", ATTRS{serial}=="5743415A4131323437343733", RUN+="/home/emil/.exhddbakcup.sh"
The backup script is quite simple:

Code: Select all

#!/bin/bash
{
touch /home/emil/thisworks.txt
su emil alt-notify-send "Backup message" "USB Backup device detected"
sleep 150
R=$(cat /home/emil/.backupperform)
if [ "$R" = "1" ]; then
	su emil alt-notify-send "Backup Message" "It has been a while since the last backup, beginning backup."
	nice -n 17 ionice -c2 -n6 /usr/bin/backintime  --backup-job >/dev/null 2>&1
	echo '0' > /home/emil/.backupperfom
	su emil alt-notify-send "Backup Message" "Your backup has completed"
fi
} &
alt-notify-send is a script which i found on the internet, and it basically allows to send message to other users through notify, here is the code:

Code: Select all

#!/bin/sh
user=`whoami`
pids=`pgrep -u $user gnome-panel`
title=$1
text=$2
timeout=$3
 
if [ -z "$title" ]; then
        echo You need to give me a title >&2
        exit 1
fi
if [ -z "$text" ]; then
        text=$title
fi
if [ -z "$timeout" ]; then
        timeout=60000
fi
 
for pid in $pids; do
        # find DBUS session bus for this session
        DBUS_SESSION_BUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS \
                /proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'`
        # use it
        DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
        notify-send "$title" "$text"
done
After a few hours of debugging I couldn´t get this to work. If I execute the script from terminal everything is fine. When I look at udevadm it show that the script has been execute, but I neither see any message nor the file thisworks.txt is created.

Do you have some ideas?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Problem with Udev Rule for making backups

Post by Pilosopong Tasyo »

Replace:

Code: Select all

su emil alt-notify-send "..." "..."
With:

Code: Select all

su emil DISPLAY=:0 alt-notify-send "..." "..."
See if that will work.
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
emil_pavlov

Re: Problem with Udev Rule for making backups

Post by emil_pavlov »

No still nothing; and the file 'thisworks.txt' is not created, which means that the problem lays somewhere else.
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Problem with Udev Rule for making backups

Post by Pilosopong Tasyo »

Unfortunately, I'm not familiar with using udev rules. But if I were to implement this, I'll just write a script to monitor the /media folder for the presence of the designated removable media, and execute the backup script once the media is attached to the computer. Then use cron to schedule the monitoring script to run at regular intervals. Is this approach more practical?
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
emil_pavlov

Re: Problem with Udev Rule for making backups

Post by emil_pavlov »

Yes, I guess it is. Thank you anyway.
Locked

Return to “Scripts & Bash”