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)

SpliFF
  • 394
  • 2
  • 8
  • 24
  • This would be really easy to test - why haven't you done it in a lab or on a cheap disposable VM ? – user9517 Mar 20 '14 at 06: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 06: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. – user9517 Mar 20 '14 at 07:49

2 Answers2

8

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.

user9517
  • 115,471
  • 20
  • 215
  • 297
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.

Hunter Eidson
  • 493
  • 5
  • 8