To avoid editing files at the same time of backup scheduled by cron, I figure out a way:

  • Before backup, send a desktop notification such as notify-send -t 0 "Ready for backup", to let me prepare (save my editing to files, and plug in external hard drive as backup destination), and when I am ready, I will click okay on the notification, and backup starts.

  • Once backup finishes, optionally notify me with backup command's exit value and time taken for backup, so I can resume editing.

How can I do that in bash and Ubuntu? This contains a smaller problem: how to schedule desktop notification in cron?

Feel free if you have a better way to achieve my goal at the beginning.

My attempt.

I added * * * * * /home/t/mybackup.sh to crontab -e, where I wrote a bash script /home/t/mybackup.sh wrapping backup and notifications:

#! /usr/bin/env bash                                                                                                                                          
export DISPLAY=:0
/usr/bin/notify-send -t 0 "Ready for backup?" &&
    dest=/tmp/`date +%Y.%m.%d_%H:%M:%S`&&
    mkdir -p "$dest" &&
    timetake=`time /usr/bin/rsync -a /home/t/program_files/dir/ "$dest"/  2>"$dest"/rsync-errors`; exitvalue=`echo $?`
/usr/bin/notify-send -t 0 "Finished backup!" "time taken: " $timetake " exit value:" $exitvalue

It has the following problems:

  • I want to make the first notification "Ready for backup?" wait for my response before backup. i.e. Ideally, if I click "OK", then it can continue to backup. If I click "Cancel", then it shouldn't run backup and the following commands in the script.

    But in my current script, the first notify-send doesn't wait for my response and continue to backup.

    Even if I could make it wait for my response, I don't know how to code in my script to pick up my response of whether I click "OK" or "Cancel".

  • the second notify-send for "Finished backup! ..." doesn't appear after backup finishes.

  • If your fileystems - or at least your important one(s) - are on LVM then rsnapshot can be configured to run an rsync backup from an LVM snapshot. – roaima Apr 21 '15 at 23:27
  • not on LVM. so I will stop changing anything during backup. – Tim Apr 21 '15 at 23:31
  • If you have to be sitting there to click the ok button anyhow, then why bother with scheduling? Just run the backup manually. – psusi Apr 23 '15 at 2:33
  • because I often forget. @psusi – Tim Apr 23 '15 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.