2019-10-09
Backup Work dir to git before shutdown
unix.stackexchange
Question

I am using Ubuntu 16.04. I have a directory named Work which I want to backup to github everytime I poweroff my computer. I have written the backup script and it's working fine but I cannot run it before shutting down. Please help. Here are the contents of backup_work.sh

cd /home/kaustab/Work
git add .
git commit -m "Daily Backup"
mkdir /home/kaustab/test 
git push origin master
echo "Backed up"
read -n 1 -s -r -p "Press any key to continue"
Answer
1

Thank you all for all your help but I have managed to solve this. What I did was edit the shutdown.desktop in /usr/share/applications and changed the exec to my script. This is what my modified shutdown.desktop file looks like.

[Desktop Entry]
Name=Shutdown
Comment=Backup and power off the computer
GenericName=Shut Down
Exec=gnome-terminal -e /home/kaustab/.scripts/backup_work.sh
Terminal=false
Type=Application
Categories=Utility
Icon=/usr/share/unity/icons/shutdown_highlight.png
NotShowIn=GNOME-Flashback;
X-AppStream-Ignore=true
X-Ubuntu-Gettext-Domain=session-shortcuts

At the end of the backup_work.sh script I have added the line gnome-session-quit --power-off to give me the options of the power menu. Thanks to QIS for pointing out to use ssh instead of https to connect to github. I will be trying that later.

Backup Work dir to git before shutdown
See more ...