5

I want to do a nightly "hotclone" of server A to server B. It's recommended that server B be running minimal daemons during this action so I want to create a "minimal" runlevel which pretty much only runs sshd and switch to/from that runlevel in mid-script, ie:

# do some stuff ...

# take services down
telinit 2

# do backup ...

# bring services back up
telinit 3

# do post-backup stuff ...

I want to do a runlevel switch rather than explicitly starting/stopping services so I can use the same script on a number of differently configured machines (different services, some on systemv-init some on systemd). I want to keep it all in one script for maintainability rather than creating a bunch of new initd/systemd start/stop scripts.

So the nature of the question is: * Will telinit stop the script (assume that cron is in both runlevels)? * Does telinit wait for completion or return immediately? * If the above works is it "safe" to do or are the potential problems with the approach? * Is there a better way available using a single script (again I'd prefer to avoid installing a bunch of new init scripts so this thing is largely "self-contained" and portable)

  • This would be really easy to test - why haven't you done it in a lab or on a cheap disposable VM ? – Iain Mar 20 '14 at 6:39
  • 1
    I can but it wouldn't give me a defineative answer to the "is it safe " part of the question until something goes horribly wrong. This script needs to run on live production boxes. I assume someone here has done something like this before. – SpliFF Mar 20 '14 at 6:45
  • 2
    I don't think you'll get a definitive answer and even if someone says it works for them there's no guarantee it will work for you. You have to test it anyway as your environment is different from anyone else's so you may have an edge case. In my experience with SF questions generally get answers fairly quickly if people have 'done something like this' before too so I would guess it's not a common thing. – Iain Mar 20 '14 at 7:49
8
+50

I created the following script

#!/bin/bash
date
who -r
/sbin/telinit 2
who -r
/sbin/telinit 3
who -r
date

and installed it in cron

* * * * * /home/iain/test &>>/tmp/test.out

It's output

Thu Mar 20 03:06:01 EDT 2014
         run-level 3  2014-03-20 03:05                   last=2
         run-level 2  2014-03-20 03:06                   last=3
         run-level 3  2014-03-20 03:06                   last=2
Thu Mar 20 03:06:01 EDT 2014

As you can see the script keeps running. As to the safety of this only you can decide based on your testing in your environment.

  • 1
    Thanks for the who -r example. I didn't know about that one. – SpliFF Mar 24 '14 at 1:21
5

On an older HP-UX system, we used to do cold backups of our databases nightly using runlevel changes. We had the database start and stop at runlevel 4, and basically did an telinit 3, took snapshots of the disks, telinit 4, started backing up the disks. It's a slightly different strategy than your looking at, but for all intents and purposes, it'll behave the same.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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