rsync backup script w notify at END (( solved ))

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
motoryzen
Level 10
Level 10
Posts: 3497
Joined: Sun Dec 08, 2019 12:25 am

rsync backup script w notify at END (( solved ))

Post by motoryzen »

So my mission is when I launch my bashscript it runs everything, finishes in terminal and the instant that...
taco@taco:~$
reappears ( showing to me the tasks are done)...I want a sound to play

I came across this below.
viewtopic.php?t=388644

...and am Just trying to figure out how to make the system wait until taco@taco:~$ appears BEFORE it plays the sound effect.

But no matter what I've tried..it plays the sound the instead it the script via xdotool auto executes typing and pressing enter on my password to execute the rsync part..instead of and the end of it all.

Here's my script below:

Code: Select all

#!/bin/bash

dconf dump /org/cinnamon/desktop/keybindings/ > ~/my_keybindings 
wait
dconf dump / > cinnamon_desktop
wait
cp ~/my_keybindings '/mnt/GoPro 1TB Backup/Keyboard shortcuts backup/'
wait
cp ~/my_keybindings '/mnt/16TB Toshiba hdd/Keyboard shortcuts backup/'
wait
dconf dump / > cinnamon_desktop
wait
mv '/home/taco/Mechanic/cinnamon_desktop' '/home/taco/'
wait
cp '/home/taco/cinnamon_desktop' '/mnt/GoPro 1TB Backup/Cinnamon Desktop backup/'
wait
cp ~/dconf-settings.conf '/mnt/GoPro 1TB Backup/Dconf dash settings.conf/'
wait
xdotool keydown super sleep 0.25 key Return sleep 0.25 key Right sleep 0.2 key Up sleep 0.2 keyup super
wait
xdotool type "sudo rsync -avhu --info=progress2 --delete --exclude=".cache" '/home/taco/' '/mnt/sdb1 rsyncbackup/'" && xdotool key Return
wait
xdotool type "moto" && xdotool key Return & wait
paplay --playback --volume=45000 --file-format=ogg /usr/share/sounds/LinuxMint/stereo/desktop-login.ogg &
notify-send "achiveBackup" "Complete"
Last edited by LockBot on Mon Mar 25, 2024 10:00 pm, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Mint 21.2 Cinnamon 5.8.4
asrock x570 taichi ...bios p5.00
ryzen 5900x
128GB Kingston Fury @ 3600mhz
Corsair mp600 pro xt NVME ssd 4TB
three 4TB ssds
dual 1TB ssds
Two 16TB Toshiba hdd's
24GB amd 7900xtx vid card
Viewsonic Elite UHD 32" 144hz monitor
Coffeeee
Level 2
Level 2
Posts: 98
Joined: Sun Mar 13, 2022 8:30 am
Location: virgo supercluster, milky way galaxy, solar system, earth

Re: rsync backup script with sound at the END of it

Post by Coffeeee »

Ignore post , xdotool works differently, deleted
motoryzen
Level 10
Level 10
Posts: 3497
Joined: Sun Dec 08, 2019 12:25 am

Re: rsync backup script with sound at the END of it

Post by motoryzen »

I cleaned up/corrected the bashscript some

Code: Select all

#!/bin/bash

xdotool keydown super sleep 0.3 key Return sleep 0.25 key Right sleep 0.2 key Up sleep 0.2 keyup super
xdotool sleep 0.25 type  "dconf dump /org/cinnamon/desktop/keybindings/ > ~/my_keybindings"
xdotool sleep 1 key Return
xdotool sleep 0.33 type "dconf dump / > cinnamon_desktop"
xdotool sleep 1 key Return
xdotool sleep 0.5 type "cp ~/my_keybindings '/mnt/GoPro 1TB Backup/Keyboard shortcuts backup/' && cp ~/my_keybindings '/mnt/16TB Toshiba hdd/Keyboard shortcuts backup/'"
xdotool sleep 1 key Return
xdotool sleep 0.5 type "cp '/home/taco/cinnamon_desktop' '/mnt/GoPro 1TB Backup/Cinnamon Desktop backup/' && cp '/home/taco/cinnamon_desktop' '/mnt/16TB Toshiba hdd/'"
xdotool sleep 1. key Return
xdotool sleep 1 type "cp ~/dconf-settings.conf '/mnt/GoPro 1TB Backup/Dconf dash settings.conf/' && cp ~/dconf-settings.conf '/mnt/16TB Toshiba hdd/Dconf dash settings.conf/'"
xdotool sleep 1 key Return
xdotool sleep 0.5 type "sudo rsync -avhu --info=progress2 --delete --exclude=".cache" '/home/taco/' '/mnt/sdb1 rsyncbackup/'"
xdotool sleep 1 key Return
xdotool sleep 0.5 type "moto" && xdotool key Return
paplay --playback --volume=45000 --file-format=ogg /usr/share/sounds/LinuxMint/stereo/desktop-login.ogg && wait
notify-send "achiveBackup" "Complete"
Last edited by motoryzen on Fri Feb 02, 2024 11:05 pm, edited 1 time in total.
Mint 21.2 Cinnamon 5.8.4
asrock x570 taichi ...bios p5.00
ryzen 5900x
128GB Kingston Fury @ 3600mhz
Corsair mp600 pro xt NVME ssd 4TB
three 4TB ssds
dual 1TB ssds
Two 16TB Toshiba hdd's
24GB amd 7900xtx vid card
Viewsonic Elite UHD 32" 144hz monitor
motoryzen
Level 10
Level 10
Posts: 3497
Joined: Sun Dec 08, 2019 12:25 am

Re: rsync backup script with sound at the END of it

Post by motoryzen »

Also tried

Code: Select all

#!/bin/bash
xdotool keydown super sleep 0.3 key Return sleep 0.25 key Right sleep 0.2 key Up sleep 0.2 keyup super
wait
xdotool type "rsync -avhu --info=progress2 --delete --exclude=".cache" '/home/tpcs/' '/mnt/sdb1 rsyncbackup/'" && xdotool key Return && xdotool type "moto" && xdotool key Return
wait
notify-send "Main drive /home Backup complete"
But the notification pop up still pops up the instant the Return ( aka enter) key is simulated/pressed. I want it to wait on popping up until the terminal window finishes the progress of that command and returns to the taco@taco:~$
Mint 21.2 Cinnamon 5.8.4
asrock x570 taichi ...bios p5.00
ryzen 5900x
128GB Kingston Fury @ 3600mhz
Corsair mp600 pro xt NVME ssd 4TB
three 4TB ssds
dual 1TB ssds
Two 16TB Toshiba hdd's
24GB amd 7900xtx vid card
Viewsonic Elite UHD 32" 144hz monitor
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: rsync backup script with sound at the END of it

Post by AndyMH »

Try sync instead of wait, it should terminate when all disk activity is complete.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
motoryzen
Level 10
Level 10
Posts: 3497
Joined: Sun Dec 08, 2019 12:25 am

Re: rsync backup script with sound at the END of it

Post by motoryzen »

AndyMH wrote: Tue Sep 26, 2023 8:46 am Try sync instead of wait, it should terminate when all disk activity is complete.
That was a reasonable idea...unfortunately..that didn't matter. Tried sync only the line just above the notify-send "backup complete" message and replaced all of the wait commands with sync.

I say it didn't matter because I see the process continuing of the rsync command part even after the notification message disappears
Mint 21.2 Cinnamon 5.8.4
asrock x570 taichi ...bios p5.00
ryzen 5900x
128GB Kingston Fury @ 3600mhz
Corsair mp600 pro xt NVME ssd 4TB
three 4TB ssds
dual 1TB ssds
Two 16TB Toshiba hdd's
24GB amd 7900xtx vid card
Viewsonic Elite UHD 32" 144hz monitor
User avatar
all41
Level 19
Level 19
Posts: 9529
Joined: Tue Dec 31, 2013 9:12 am
Location: Computer, Car, Cage

Re: rsync backup script with sound at the END of it

Post by all41 »

for a sound notification after rysnc script finishes I use mpg123 (repositories) to play a
sound with no interface.
Then append && to the last line of the script,
and on the next line add mpg123 path/to/sound/file.mp3
mine for example:
echo "# Wait for sd write to complete" ; sleep 10 &&
mpg123 /mnt/storage/Downloads/complete.mp3
Everything in life was difficult before it became easy.
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: rsync backup script with sound at the END of it

Post by Koentje »

Or if you want to use a system OGA to play you can use this line:

gst-launch-1.0 playbin uri=file:///usr/share/mint-artwork/sounds/volume.oga
User avatar
Coggy
Level 5
Level 5
Posts: 642
Joined: Thu Mar 31, 2022 10:34 am

Re: rsync backup script with sound at the END of it

Post by Coggy »

I just add this to the end of the script: mpv /usr/share/sounds/freedesktop/stereo/complete.oga &>/dev/null
There are plenty of other sound files in that folder if you prefer.

EDIT
Ooh. I didn't know about paplay (see next post). We have choices.
Last edited by Coggy on Thu Sep 28, 2023 3:44 pm, edited 1 time in total.
User avatar
axrusar
Level 7
Level 7
Posts: 1515
Joined: Sat Jan 30, 2021 5:30 pm

Re: rsync backup script with sound at the END of it

Post by axrusar »

motoryzen wrote: Mon Sep 25, 2023 9:42 pm So my mission is when I launch my bashscript it runs everything, finishes in terminal and the instant that...
taco@taco:~$
reappears ( showing to me the tasks are done)...I want a sound to play
To simplify:

Your script (example)

Code: Select all

#!/bin/bash
rsync code here and anything else
/home/taco/mysound.sh &  # run mysound.sh in the background
notify-send "achiveBackup" "Complete"
sound script (mysound.sh):

Code: Select all

#!/bin/bash
sleep 5
paplay --playback --volume=45000 --file-format=ogg /usr/share/sounds/LinuxMint/stereo/phone-incoming-call.ogg
Adjust the delay (sleep) to your taste :lol:
Linux Mint Una Cinnamon 20.3 Kernel: 5.15.x | Quad Core I7 4.2Ghz | 24GB Ram | 1TB NVMe | Intel Graphics
Image
schellasu
Level 3
Level 3
Posts: 140
Joined: Sat Aug 05, 2023 6:41 am

Re: rsync backup script with sound at the END of it

Post by schellasu »

axrusar wrote: Thu Sep 28, 2023 3:04 pm

Code: Select all

#!/bin/bash
rsync code here and anything else
/home/taco/mysound.sh &  # run mysound.sh in the background
notify-send "achiveBackup" "Complete"
Are you sure, that that works?
I don't think so, sound would start after sleep time, independent how much time the backup takes.
I would execute the backup in the background, followed by a

Code: Select all

wait $!
which waits for terminating of the latest job sent to background
and continues, in your case, with playing a sound file.

The following example (part of ascript) starts a second script three times with different options/filters, one after the other:

Code: Select all

echo "##########    Doing typ2cee"
bash ~/PROG/l2o/lemnet2osmand10 -dgmf 1 &
jobs -l
wait $!
echo "##########    Done typ2cee"

echo "##########    Doing Chademo"
bash ~/PROG/l2o/lemnet2osmand10 -f 2 &
jobs -l
wait $!
echo "##########    Done Chademo"
[[ -f ~/TEMP/l2o/lemnet-chademo.obf ]] && mv ~/TEMP/l2o/lemnet-chademo.obf ~/TEMP/l2o/lemnet-dachl-chademo.obf

echo "##########    Doing CCS"
jobs -l
bash ~/PROG/l2o/lemnet2osmand10 -f 3 &
jobs -l
wait $!
echo "##########    Done CCS"
[[ -f ~/TEMP/l2o/lemnet-ccs.obf ]] && mv ~/TEMP/l2o/lemnet-ccs.obf ~/TEMP/l2o/lemnet-dachl-ccs.obf
Here are some infos about job control:
https://phoenixnap.com/kb/bash-wait-command
User avatar
axrusar
Level 7
Level 7
Posts: 1515
Joined: Sat Jan 30, 2021 5:30 pm

Re: rsync backup script with sound at the END of it

Post by axrusar »

schellasu wrote: Thu Sep 28, 2023 5:48 pm Are you sure, that that works?
I don't think so, sound would start after sleep time, independent how much time the backup takes.
Yes it works. I tested it myself.
Although maybe i am misunderstanding something in this request:
motoryzen wrote: Mon Sep 25, 2023 9:42 pm and the instant that...
taco@taco:~$
reappears ( showing to me the tasks are done)...I want a sound to play
So as the backup completes, the next command calling the external script with the sound is executed with the & at the end which makes it run asynchronously, there is a delay with the sleep while the rest of the main script is still being executed, the next line pops the "complete" message, taco@taco:~$ shows in the terminal as the main script has finished executing, and a few seconds later, the sound plays from the secondary script.
Linux Mint Una Cinnamon 20.3 Kernel: 5.15.x | Quad Core I7 4.2Ghz | 24GB Ram | 1TB NVMe | Intel Graphics
Image
schellasu
Level 3
Level 3
Posts: 140
Joined: Sat Aug 05, 2023 6:41 am

Re: rsync backup script with sound at the END of it

Post by schellasu »

axrusar wrote: Fri Sep 29, 2023 3:39 am
schellasu wrote: Thu Sep 28, 2023 5:48 pm Are you sure, that that works?
I don't think so, sound would start after sleep time, independent how much time the backup takes.
....So as the backup completes, the next command calling the external script with the sound is executed with the & at the end which makes it run....
I still think you are wrong!
The "next command calling the external script" is started IMMEDIATELY after start of rsync!
So it runs parallel, and not after!
Again: sound would start after sleep time, independent how much time the backup takes.
Delete the sleep to test.

You are thinking in the wrong direction.

Rsync in background
wait $! # waits for termination of the background job
program to play sound.

A different approach could be to check the exit status of rsync in that way:
rsync
if exit code ($?) ist 0 play OK-sound else play something-went-wrong-sound.

EDIT:
Seems that rsync itself stops execution until it has done its job:

Code: Select all

   rsync -L -r  -t -p -o -g -v --progress --stats -s /home/alf/FOTOS/ alf@Heinrich:/home/alf/FOTOS/TEST \
   | tee /home/alf/TEMP/FOTOS2Heinrich_"$DATE_TIME".log
   play /home/alf/Dokumente/Daten/WAVES/BUGLE3.WAV
Does play after rsync has terminated.
User avatar
axrusar
Level 7
Level 7
Posts: 1515
Joined: Sat Jan 30, 2021 5:30 pm

Re: rsync backup script with sound at the END of it

Post by axrusar »

schellasu wrote: Fri Sep 29, 2023 6:25 am
I still think you are wrong!
The "next command calling the external script" is started IMMEDIATELY after start of rsync!
Nope.. you are ABSOLUTELY WRONG!
The next command calling the external script is started AFTER the rsync command finishes doing the task. FYI in bash scripting, commands are executed one after another in line and as far as i see, the next command will not execute until the previous one is completed.
Need proof? I made a little video for you.
Here it is:

https://youtu.be/ui5WrsmyVe4

Same script i posted before...
First i show a sound test with the file alone.
Then i execute the script with an rsync job copying a few pictures to a slow external USB drive to show the sound is executed AFTER the script finished with the 2 seconds i set.

I haven't taken a close look at what motoryzen's script does and i could be wrong if using his example, but here i am showing the sound can be played AFTER the command prompt is shown again being that the script finished as described by the OP.
Linux Mint Una Cinnamon 20.3 Kernel: 5.15.x | Quad Core I7 4.2Ghz | 24GB Ram | 1TB NVMe | Intel Graphics
Image
User avatar
Coggy
Level 5
Level 5
Posts: 642
Joined: Thu Mar 31, 2022 10:34 am

Re: rsync backup script with sound at the END of it

Post by Coggy »

I think the problem is that xdotool returns as soon as it has simulated the Enter key. As far as the script is concerned, everything has been done. The side-effect of the GUI responding by starting an rsync transfer is not relevant. xdotool has done what was asked and exited.

I don't see why you are trying to animate a GUI to do an rsync transfer rather than just do it in the script, but if you want to do that, I think you need to tell the GUI to ring the bell, something like:

Code: Select all

xdotool sleep 0.5 type "sudo rsync -avhu --info=progress2 --delete --exclude=".cache" '/home/tpcs/' '/mnt/sdb1 rsyncbackup/' ; notify-send 'Main drive /home Backup complete'"
xdotool sleep 1 key Return
xdotool sleep 0.5 type "moto" && xdotool key Return
Or you may be able to just type separate commands like this, and let the keystrokes queue up until rsync finishes:

Code: Select all

xdotool sleep 0.5 type "sudo rsync -avhu --info=progress2 --delete --exclude=".cache" '/home/tpcs/' '/mnt/sdb1 rsyncbackup/'"
xdotool sleep 1 key Return
xdotool sleep 0.5 type "moto" && xdotool key Return
xdotool sleep 0.5 type "notify-send 'Main drive /home Backup complete"
xdotool sleep 1 key Return
schellasu
Level 3
Level 3
Posts: 140
Joined: Sat Aug 05, 2023 6:41 am

Re: rsync backup script with sound at the END of it

Post by schellasu »

axrusar wrote: Fri Sep 29, 2023 7:39 am
schellasu wrote: Fri Sep 29, 2023 6:25 am I still think you are wrong!
The "next command calling the external script" is started IMMEDIATELY after start of rsync!
Nope.. you are ABSOLUTELY WRONG!
The next command calling the external script is started AFTER the rsync command finishes doing the task. FYI in bash scripting, commands are executed one after another in line and as far as i see, the next command will not execute until the previous one is completed.
O.K. I was wrong here.
Bash starts command lines sequentially.
Except: you start a subshell (starting a script, or background process).
To wait in your script until the subshell terminates, you can use the wait command, otherwise the two scripts run parallel.

So why do you start playing in an own script and in background?
Why not set the play-comand-line as last line in the first script?

BTW: Motoryzen does a lot I don't understand :(
User avatar
axrusar
Level 7
Level 7
Posts: 1515
Joined: Sat Jan 30, 2021 5:30 pm

Re: rsync backup script with sound at the END of it

Post by axrusar »

schellasu wrote: Fri Sep 29, 2023 10:56 am
Why not set the play-comand-line as last line in the first script?
Yes i did not try that, i just started looking for a solution thinking "outside the box" (or outside the current terminal ) trying to get the terminal showing the prompt again so THEN the sound plays after from the other parallel script.
Motoryzen probably went on vacations, or is eating popcorn and laughing at our newbieness trying to work this out :lol:
Linux Mint Una Cinnamon 20.3 Kernel: 5.15.x | Quad Core I7 4.2Ghz | 24GB Ram | 1TB NVMe | Intel Graphics
Image
motoryzen
Level 10
Level 10
Posts: 3497
Joined: Sun Dec 08, 2019 12:25 am

Re: rsync backup script with sound at the END of it

Post by motoryzen »

Bash starts command lines sequentially.
Except: you start a subshell (starting a script, or background process).
To wait in your script until the subshell terminates, you can use the wait command, otherwise the two scripts run parallel.
Except I already tried that and it doesn't prevent the notify-send from running instantly as the rsync command is still running or just began running.
The "next command calling the external script" is started IMMEDIATELY after start of rsync!
So it runs parallel, and not after!
That is exactly what I've confirmed so far..trying the sync idea.
Mint 21.2 Cinnamon 5.8.4
asrock x570 taichi ...bios p5.00
ryzen 5900x
128GB Kingston Fury @ 3600mhz
Corsair mp600 pro xt NVME ssd 4TB
three 4TB ssds
dual 1TB ssds
Two 16TB Toshiba hdd's
24GB amd 7900xtx vid card
Viewsonic Elite UHD 32" 144hz monitor
motoryzen
Level 10
Level 10
Posts: 3497
Joined: Sun Dec 08, 2019 12:25 am

Re: rsync backup script with sound at the END of it

Post by motoryzen »

Motoryzen probably went on vacations, or is eating popcorn and laughing at our newbieness trying to work this out :lol:
:lol: Na...Since Mid June I've been working my aspirin off doing Uber Eats delivers in my area trying to pump extra cash...Thus why imo I haven't been as active on here as I usually am. And so far, I've had a great girlfriend since 8-1-23. (shrugs).
BTW: Motoryzen does a lot I don't understand :(
*Makes embarrassed Shrek grin *

Well, I'm sure there are plenty in the world that might say the same thing involving my strange mental paths I take to try finding the answer to a problem :lol:
Mint 21.2 Cinnamon 5.8.4
asrock x570 taichi ...bios p5.00
ryzen 5900x
128GB Kingston Fury @ 3600mhz
Corsair mp600 pro xt NVME ssd 4TB
three 4TB ssds
dual 1TB ssds
Two 16TB Toshiba hdd's
24GB amd 7900xtx vid card
Viewsonic Elite UHD 32" 144hz monitor
motoryzen
Level 10
Level 10
Posts: 3497
Joined: Sun Dec 08, 2019 12:25 am

Re: rsync backup script with sound at the END of it

Post by motoryzen »

I don't see why you are trying to animate a GUI to do an rsync transfer rather than just do it in the script
(shrugs)..I feel nerdy watching xdotool speed type out the command and auto executing Enter/return. etc.

BUT...if I can execute any rsync command in the terminal without xdotool having to type it out and auto pressing Enter and you have a way I can accomplish this mission, I'm all eyes. I'm a little lost at the moment but time to backtrack on comments here to see what I haven't tried yet.
Mint 21.2 Cinnamon 5.8.4
asrock x570 taichi ...bios p5.00
ryzen 5900x
128GB Kingston Fury @ 3600mhz
Corsair mp600 pro xt NVME ssd 4TB
three 4TB ssds
dual 1TB ssds
Two 16TB Toshiba hdd's
24GB amd 7900xtx vid card
Viewsonic Elite UHD 32" 144hz monitor
Locked

Return to “Scripts & Bash”