2

I have a small backup bash script that I wrote for my computer at work. I have copied the script into /etc/rc0.d/ and called it K01backup so it is executed before anything else upon shutdown. It backs up all the data from my computer (running Ubuntu 14.04LTS) and my working copies and a virtual machine located on a separate internal SSD to an external hard drive and adds the log output to files in each folders.

Here is the script:

#!/bin/bash

LOG="/syncLog"
VMORIG="/media/SSDData/VM"
PROJORIG="/media/Data/Projects"
DESTROOT="/media/ExtData/Backups"
LOGVM=${DESTROOT}"/VM"${LOG}
LOGPROJ=${DESTROOT}"/Projects"${LOG}
ALLORIG="/"
DESTALL=${DESTROOT}"/All"
LOGALL=${DESTROOT}"/All"${LOG}


echo "STARTED" > ${LOGPROJ}
date +%d.%m.%Y/%H:%M:%S >> ${LOGPROJ}
rsync -avvx --progress --no-whole-file ${PROJORIG} ${DESTROOT} >> ${LOGPROJ}
echo "FINISHED" >> ${LOGPROJ}
date +%d.%m.%Y/%H:%M:%S >> ${LOGPROJ}

echo "STARTED" > ${LOGVM}
date +%d.%m.%Y/%H:%M:%S >> ${LOGVM}
rsync -avvx --progress --no-whole-file  ${VMORIG} ${DESTROOT} >> ${LOGVM}
echo "FINISHED" >> ${LOGVM}
date +%d.%m.%Y/%H:%M:%S >> ${LOGVM}

echo "STARTED" > ${LOGALL}
date +%d.%m.%Y/%H:%M:%S >> ${LOGALL}
rsync -avvx --progress --no-whole-file --exclude "/media/*" --exclude "/indel/*" ${ALLORIG} ${DESTALL} >> ${LOGALL}
echo "FINISHED" >> ${LOGALL}
date +%d.%m.%Y/%H:%M:%S >> ${LOGALL}

Then I ran sudo chmod +x /etc/rc0.d/K01backup to make it executable. At first the script took roughly an hour to execute and it all worked well. But for a little while now, I can see in the log files that the script started (presumably started again) an hour after I left work and all the files were already up to date, so it only took about a minute to run.

Does anybody know what I might have done wrong?

2

The README in that directory states that scripts in that directory are only called once on poweroff (and not on reboot).

With a simple test program

#!/bin/bash

LOG=/root/backup.log
date >> $LOG
echo $* >> $LOG

I noticed that one time the program was actually called twice, once without a parameter and once with the parameter 'stop'. I have however not been able to reproduce it.

I would suggest to log the actual invocation parameters to the program as well and in the script test for $1 being stop.

It is also more customary to put this program as backup in /etc/init.d and make a link from /etc/rc0.d/K01backup to that script, but that should not influence its operation in any way. Any tools managing such entries work with creating/deleting these links.

Based on trying out this basic script the OP found that there was a backup file from editing the file: /etc/rc0.d/K01backup~ that got executed as well. Putting the backup file in /etc/init.d/ from the start, and making a link would have prevented this from occuring (independent of whether there would be a /etc/init.d/backup~ file or not).

  • Yes, I am aware that the scripts in this directory are only executed on poweroff but not on reboot, that was on purpose. I will execute the test script during lunchtime (in ca. 1h) and add the results, thank you. Also I moved the files to /etc/init.d and created the symlinks. – David Wright Dec 9 '14 at 10:14
  • @DavidWright I had not played with this before and only realised that rebooting did not invoke the script at all, so that was more a note to myself (and any others trying this out). – Anthon Dec 9 '14 at 10:29
  • I tried executing the test script and changing the mode of the backup script to -x. Since shutdown still took far too long for just such a small test script I suspected that the backup script was still running. Upon closer inspection of all the rc*.d folders I realised that the backup file generated by the text editor /etc/rK01backup~ was also executable. Ever since I deleted it, only the test script was executed on shutdown, and it was only executed once. Thus the reason for the symbolic links! Thank you very much for your help. – David Wright Dec 9 '14 at 12:07
  • 1
    @DavidWright I will put that in the answer, that makes it easier for people to find out that this was the problem. All the more reasons to use links only in the /etc/rc0.d – Anthon Dec 9 '14 at 12:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.