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

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

Post by motoryzen »

trying axrusar's idea but except I don't need a separate system sound file to play

Code: Select all

#!/bin/bash
gnome-terminal -x bash -c "rsync -avhu --info=progress2 --delete --exclude=".cache" '/home/taco/' '/mnt/sdb1 rsyncbackup/'"
notify-send "backup of Main home partition has begun" && '/home/taco/bashscripts/archived bashscripts not used often/notifysend.sh'
= nope. the notify-send of " backup complete" still plays instantly as the first notify-send of " beginning backup" vanishes within 2 seconds ( that is via my Notifications time remain on screen setting)

Also tried replacing && with & as well as ; and only one spacebar press between the " backup begun" message and " backup complete" separate script.

same results.

also tried

Code: Select all

#!/bin/bash
gnome-terminal -x bash -c "rsync -avhu --info=progress2 --delete --exclude=".cache" '/home/tpcs/' '/mnt/sdb1 rsyncbackup/'"
notify-send "backup of Main home partition has begun" && wait && sync && wait &&
'/home/tpcs/bashscripts/archived bashscripts not used often/notifysend.sh'
Must I really have to have a separate sound play in addition to the notify-send just to accomplish this goal?

I even put the notify-send command into a separate text script file called notifysend.sh

Here's the most recent script that runs the backup script then the notifysend.sh script right after each other

Code: Select all

#!/bin/bash
'/home/taco/Mechanic/Backup home via Rsync to 4TB sdb1 ssd'
'/home/taco/bashscripts/archived bashscripts not used often/notifysend.sh'
Tried

Code: Select all

#!/bin/bash
'/home/tpcs/Mechanic/Backup home via Rsync to 4TB sdb1 ssd' &
'/home/tpcs/bashscripts/archived bashscripts not used often/notifysend.sh'
1. then

Code: Select all

#!/bin/bash
'/home/tpcs/Mechanic/Backup home via Rsync to 4TB sdb1 ssd' &&
'/home/tpcs/bashscripts/archived bashscripts not used often/notifysend.sh'
2. then

Code: Select all

#!/bin/bash
'/home/tpcs/Mechanic/Backup home via Rsync to 4TB sdb1 ssd'
&& '/home/tpcs/bashscripts/archived bashscripts not used often/notifysend.sh'
..noticed on taht one that it doesn't run the notifysend.sh script at all no matter how long I wait...( after I run the backup script the first time..each additional time takes not even 7 seconds total before the gnome-terminal window disappears showing the task is done )
3. wait

Code: Select all

#!/bin/bash
'/home/tpcs/Mechanic/Backup home via Rsync to 4TB sdb1 ssd'
wait
'/home/tpcs/bashscripts/archived bashscripts not used often/notifysend.sh'
4. wait

Code: Select all

#!/bin/bash
'/home/tpcs/Mechanic/Backup home via Rsync to 4TB sdb1 ssd' && wait &&
'/home/tpcs/bashscripts/archived bashscripts not used often/notifysend.sh'
5. wait and sync

Code: Select all

#!/bin/bash
'/home/tpcs/Mechanic/Backup home via Rsync to 4TB sdb1 ssd' && wait && sync && 
'/home/tpcs/bashscripts/archived bashscripts not used often/notifysend.sh'
uuug.
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
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 »

A few things:
A bash script generally runs commands one line at a time. It will not execute the next command until the current one is finished. The only exception is if you tell it to run something in the background by putting a & at the end of the command.
This is exactly as it behaves when you are typing into a terminal. You can see it in a terminal by the difference between the commands sleep 3 and sleep 3 &

If a line is getting too long, you can continue a command onto the next line by entering a backslash as the very last character of the line that need to be continued.

It can be very difficult to figure out where a script is - how far it has progressed. It is well worth remembering that the command set -x makes bash type every command with a + in front immediately before executing it. This lets you see what it's doing, which lines take time and which don't. I think it would help your understanding enormously if you did this near the top of your script, and watched.

& says to run the command on the left in background. && says to run the command on the left and only if there was no error then run the command on the right. Your example number 2 will not run the notify-send because the line is a syntax error - there is no command on the left of the &&.

This script will hopefully make some of the above clear:

Code: Select all

#!/bin/bash
set -x                          # So we can see what it's doing
notify-send "It's started..."   # This just sends the notification, doesn't wait

echo Sleeping in foreground...
sleep 10

echo Sleepi\
ng in background...
sleep 10 &

# This notify is too soon - the background sleep hasn't finished
notify-send "It's all over now"

wait    # for that background sleep
notify-send "It really IS all over now"
zenity --info --text="You won't miss this popup"
I would put the whole of your activity into one script maybe like this:

Code: Select all

#!/bin/bash
notify-send "starting..."
rsync blah blah blah       # Might take a while
sync                           # Won't return until all disk writes have been flushed
zenity --info --text="Backup finished" &             # Run in background so we can carry on and ring the bell
paplay /usr/share/sounds/LinuxMint/stereo/phone-incoming-call.ogg
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 »

A bash script generally runs commands one line at a time. It will not execute the next command until the current one is finished. The only exception is if you tell it to run something in the background by putting a & at the end of the command.
Not always.

If I run the script below... the notify-send " backup complete " command part of that script STILL executes instantly after the rsync command runs ( runs...not finishes completely )
I added numbers to the beginning of each command here just for line reference ..notice..no & in the script below

Code: Select all

#!/bin/bash
1. notify-send " begin backup
2. rsync -n -avhu --info=progress2 --delete --exclude=".cache" '/home/taco/' '/mnt/sdb1 rsyncbackup/'
3.  notify-send " backup completed"
Yes..from what all I've read...using the && is supposed to make the next ( aka in this case of importance...command 3 which is the notify-send " backup completed" deal wait to launch until command 2 finishes. but as I've shown already numerous times in this post...that doesn't happen.

Coggy , Also just tried your suggestion at the end of your comment 4:50 a.m. this morning
again...the numbers at the beginning of the script info I post here..are not inside the script..just there to show you all where each new line begins

Code: Select all

#!/bin/bash
1. notify-send "Main drive's home partition ....backup has begun"
2. gnome-terminal -x bash -c "rsync -n -avhu --info=progress2 --delete --exclude=".cache" '/home/taco/' '/mnt/sdb1 rsyncbackup/'"       #May take a while
3. sync
4. zenity --info --text="backup.... completed " &       # Run in background so we can carry on and ring the bell
5. paplay /usr/share/sounds/LinuxMint/stereo/phone-incoming-call.ogg
In this case, both the zenity ( centered on my screen when it pops up) notification AND that ringing sound....execute instantly the rsync command begins its backing up process
Won't return until all disk writes have been flushed
I just thought of something...I have write caching enabled in gnome-disks application for my main drive AND the target backup drive....does this conflict with the natural functions of sync command? And if yes..I wonder if that is the key ingredient/change I'm needing here. (shrugs)

Also tried adding a sync between 1 and 2 just to try it......also tried in addition to that..adding a sync between 4 and 5....same results.
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
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 »

Why are you starting rsync from inside gnome-terminal? I didn't put that in my suggested script.

As you have already found out, launching a gnome-terminal returns immediately. As I said earlier, if you do that then you can't tell when it ends. So the command you give to gnome-terminal will have to include ringing the bell when rsync has finished.

You could put the whole of my suggestion in a script and then tell gnome-terminal to run that I suppose.

Oh, and I posted that last post at 10:50 am. And I'm posting this while watching a late night sci-fi film full of rubber dinosaurs. I guess we're on different sides of the pond.
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 »

Why are you starting rsync from inside gnome-terminal? I didn't put that in my suggested script.
Because I actually want to SEE it work..instead of it running invisibly in the background
As I said earlier, if you do that then you can't tell when it ends. So the command you give to gnome-terminal will have to include ringing the bell when rsync has finished.
And I tried the ringing bell option..but guess what? Like I've already said...the ringing bell sounds off the INSTANT the rsync command executes ( not finishes..but begins the backing up process).
Oh, and I posted that last post at 10:50 am. And I'm posting this while watching a late night sci-fi film full of rubber dinosaurs. I guess we're on different sides of the pond.
:mrgreen: :lol: Ah...classic tv. strange yet good memories

So I suppose the next thing I'll do is remove the gnome-terminal e bash blah blah part of that rsync command in the script....and then just right click on the script and tell it to open via terminal and report back (shrugs)
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 »

Ok..so update. Tried the script below setting it to open with gnome-terminal.

Code: Select all

#!/bin/bash
notify-send "Main drive's home partition ....backup has begun" & 
dconf dump /org/cinnamon/desktop/keybindings/ > ~/my_keybindings & dconf dump / > cinnamon_desktop & cp ~/my_keybindings '/mnt/GoPro 1TB Backup/Keyboard shortcuts backup/' && cp ~/my_keybindings '/mnt/16TB Toshiba hdd/Keyboard shortcuts backup/' & cp '/home/tacoi/cinnamon_desktop' '/mnt/GoPro 1TB Backup/Cinnamon Desktop backup/' && cp '/home/taco/cinnamon_desktop' '/mnt/16TB Toshiba hdd/' & cp ~/dconf-settings.conf '/mnt/GoPro 1TB Backup/Dconf dash settings.conf/' && cp ~/dconf-settings.conf '/mnt/16TB Toshiba hdd/Dconf dash settings.conf/' & rsync -n -avhu --info=progress2 --delete --exclude=".cache" '/home/taco/' '/mnt/sdb1 rsyncbackup/'
gnome-terminal -x bash -c "echo 'backup of main drive has finished'" && '/home/taco/bashscripts/archived bashscripts not used often/notifysend.sh'
sync
I saw no terminal window open..nothing visually to show me it was working ( although when I right click on the coherent...resulting files/folders in their target destinations..and select Properties...it shows the timestamp had changed to the precise minute it popped up the finished message.

*** HOWEver :D ****

When I manually open a gnome-terminal window, left click drag the script file into it and press enter to execute it....SUCCESS..precisely to my goal.

Almost there ;)
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 »

Winner winner...chicken dinner!! I figured it out thanks to everyone's input regardless. :)

Code: Select all

#!/bin/bash
gnome-terminal --geometry=173x96-000
xdotool sleep 0.33 type 'notify-send "backup of main drive..... has begun"'
sync
xdotool key Return
sync
xdotool type "dconf dump /org/cinnamon/desktop/keybindings/ > ~/my_keybindings & dconf dump / > cinnamon_desktop & cp ~/my_keybindings '/mnt/GoPro 1TB Backup/Keyboard shortcuts backup/'"
sync
xdotool key Return
sync
xdotool type "cp ~/my_keybindings '/mnt/16TB Toshiba hdd/Keyboard shortcuts backup/'"
sync
xdotool key Return
#  so far so good
sync
xdotool type "cp '/home/taco/cinnamon_desktop' '/mnt/GoPro 1TB Backup/Cinnamon Desktop backup/'"
sync
xdotool key Return
sync
xdotool type "cp '/home/taco/cinnamon_desktop' '/mnt/16TB Toshiba hdd/'"
# all accounted for so far
sync
xdotool key Return
sync
xdotool type "cp ~/dconf-settings.conf '/mnt/GoPro 1TB Backup/Dconf dash settings.conf/'"
sync
xdotool key Return
# all accounted for so far
sync
xdotool type "cp ~/dconf-settings.conf '/mnt/16TB Toshiba hdd/Dconf dash settings.conf/'"
sync
xdotool key Return
sync
xdotool type "rsync -avhu --info=progress2 --delete --exclude=".cache" '/home/taco/' '/mnt/sdb1 rsyncbackup/'"
sync
xdotool key Return
sync
xdotool type 'notify-send " backup of main drive....complete " '
sync
xdotool key Return
sync
It launches a gnome terminal window, puts it in the size ( good enough at least...just barely shy of window tiling it to half screen to the right- shrugs - ), types and executes every single individual backing up command including rsync, and each .." piece " of command works one at a time one right after the other letting the former finish first before moving to the next command in line...... including..the final notify-send message pop up !!

Giggidee.
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 w notify at END (( solved ))

Post by motoryzen »

Update 10-6-23

I like this one better

Code: Select all

#!/bin/bash
timeout 2.5 zenity --info --text="backup process has begun"
xdotool key super+Return sleep 0.25 keydown super sleep 0.2 key Right sleep 0.2 keyup super
sync
xdotool type "dconf dump /org/cinnamon/desktop/keybindings/ > ~/my_keybindings && dconf dump / > cinnamon_desktop"
sync
xdotool key Return
sync
xdotool type "cp ~/my_keybindings '/mnt/GoPro 1TB Backup/Keyboard shortcuts backup/'"
sync
xdotool key Return
sync
xdotool type "cp ~/my_keybindings '/mnt/16TB Toshiba hdd/Keyboard shortcuts backup/'"
sync
xdotool key Return
sync
xdotool type "cp '/home/taco/cinnamon_desktop' '/mnt/GoPro 1TB Backup/Cinnamon Desktop backup/'"
sync
xdotool key Return
sync
xdotool type "cp '/home/taco/cinnamon_desktop' '/mnt/16TB Toshiba hdd/'"
sync
xdotool key Return
sync
xdotool type "cp ~/dconf-settings.conf '/mnt/GoPro 1TB Backup/Dconf dash settings.conf/'"
sync
xdotool key Return
sync
xdotool type "cp ~/dconf-settings.conf '/mnt/16TB Toshiba hdd/Dconf dash settings.conf/'"
sync
xdotool key Return
sync
xdotool type "sudo su"
sync
xdotool sleep 0.5 key Return
sync
xdotool sleep 0.5 type "moto"
sync
xdotool sleep 0.5 key Return
sync
xdotool sleep 0.5 type "rsync -avhu --info=progress2 --delete /var/ '/mnt/GoPro 1TB Backup/root directories backup/varbackup/'"
sync
xdotool key Return
sync
xdotool sleep 0.5 type "rsync -avhu --info=progress2 --delete /etc/ '/mnt/GoPro 1TB Backup/root directories backup/etcbackup/'"
sync
xdotool key Return
sleep 1 echo "now for /home backup process to begin"
sync
xdotool sleep 0.5 type "rsync -avhu --info=progress2 --delete --exclude=".cache" '/home/taco/' '/mnt/sdb1 rsyncbackup/'" 
sync
xdotool key Return
sync
xdotool type 'timeout 2.5 zenity --info --text="backup process....completed "'
sync
xdotool key Return
sync
xdotool type "exit"
xdotool key Return
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 w notify at END (( solved ))

Post by motoryzen »

** Update.. 2-2-24 **

Code: Select all

#!/bin/bash
timeout 2 zenity --info --text="backup process has begun"
xdotool keydown super sleep 0.25 key Return sleep 0.3 key Right sleep 0.25 key Up sleep 0.25 keyup super
xdotool type 'echo "... now for /home backup process to begin"'
xdotool sleep 0.75 key Return
sync
xdotool sleep 0.5 type "dconf dump /org/cinnamon/desktop/keybindings/ > ~/my_keybindings && dconf dump / > cinnamon_desktop"
sync
xdotool key Return
sync
xdotool type "cp ~/my_keybindings '/mnt/GoPro 1TB Backup/Keyboard shortcuts backup/'"
sync
xdotool key Return
sync
xdotool type "cp ~/my_keybindings '/mnt/16TB Toshiba hdd/Keyboard shortcuts backup/'"
sync
xdotool key Return
sync
xdotool type "cp '/home/tpcs/cinnamon_desktop' '/mnt/GoPro 1TB Backup/Cinnamon Desktop backup/'"
sync
xdotool key Return
sync
xdotool type "cp '/home/tpcs/cinnamon_desktop' '/mnt/16TB Toshiba hdd/'"
sync
xdotool key Return
sync
xdotool type "cp ~/dconf-settings.conf '/mnt/GoPro 1TB Backup/Dconf dash settings.conf/'"
sync
xdotool key Return
sync
xdotool type "cp ~/dconf-settings.conf '/mnt/16TB Toshiba hdd/Dconf dash settings.conf/'"
sync
xdotool key Return
sync
xdotool type "sudo su"
sync
xdotool sleep 0.5 key Return
sync
xdotool sleep 0.5 type "moto"
sync
xdotool sleep 0.5 key Return
sync
xdotool sleep 0.5 type "cp -r /usr/share/icons/ '/media/tpcs/USB STICK/usr share icons backup/'"
sync
xdotool sleep 0.5 key Return
sync
xdotool sleep 0.5 type "rsync -avhu --info=progress2 --delete /var/ '/mnt/GoPro 1TB Backup/root directories backup/varbackup/'"
sync
xdotool key Return
sync
xdotool type "echo ' . .*** ....  now to backup /etc folder.............****......................................................***** ...................****..................***....... ' "
xdotool key Return
sync
xdotool sleep 0.5 type "rsync -avhu --info=progress2 --delete /etc/ '/mnt/GoPro 1TB Backup/root directories backup/etcbackup/'"
sync
xdotool key Return
sync
xdotool sleep 0.5 type "rsync -avhu --info=progress2 --delete --exclude=".cache" '/home/taco/' '/mnt/sdb1 rsyncbackup/'" 
sync
xdotool key Return
sync
xdotool sleep 0.5 type " rm '/home/taco/Mechanic/cinnamon_desktop' "
sync
xdotool key Return
sync
xdotool type 'timeout 2 zenity --info --text="backup process....completed "'
sync
xdotool key Return
sync
xdotool type "exit"
xdotool key Return
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”