Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Ubuntu Server - Shutdown script

  1. #11
    Join Date
    Apr 2008
    Location
    Far, far away
    Beans
    2,148
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Ubuntu Server - Shutdown script

    With that Upstart config you will get the same results because you didn't put in a dependency. Also your runlevels line is wrong. Your config will do your backup at system start and shutdown. (0 is shutdown, 6 is reboot, 2345 is more what you want).

    That is, you would need to make the mysql config dependent on your backup config so that it doesn't shutdown until your backup is done. But probably easier for you is to simply modify (make backup copy first) the /etc/init/mysql.conf config.

    On my server it doesn't currently have a pre-stop script and that's what you would want. Add a pre-stop script that calls your backup. It will run before mysql stop trigger happens. It already has a pre-start and post-start but those get triggered when mysql is started.

    Alternately you could add a dependency but that's something I've not tried myself. If you look at that mysql.conf you'll see it has a start dependency like this,

    Code:
    start on (net-device-up
              and local-filesystems
    	  and runlevel [2345])
    This makes mysql dependent on those 3 conditions being met. You could alter the "stop on" conditions to make it only stop when your backup is completed. Then you set your backup conf to only start on runlevel 0 (shutdown). mysql wouldn't shutdown until both runlevel 016 and your backup is done. I'm not sure if the kernel will wait on mysql though. That would require more reading on whether upstart requires all jobs to complete or whether after some timeout it just kills everything.

    Anyway, I think the condition would be something like this,

    Code:
    stop on (runlevel [016]
            and backup stopped)
    Where backup is the name of your backup job started by backup.conf

    ---
    All of this seems rather tricky. Looking briefly at the apcupsd docs I see that when your ups detects a problem and when it goes onto battery there is a call made each time to /etc/apcupsd/apccontrol with an argument. You would likely find it better and much easier to trigger your backup from the "powerout" message. According to my reading (I don't have one) there is no default action for these messages but you could simply start your backup by adding a section like this to the /etc/apcupsd/apccontrol script.

    Code:
    if [ "$1" = "powerout" ]; then
      dobackup
    fi
    As long as your UPS gives you enough time between detecting the power out, to going onto battery and then to shutdown.
    Last edited by BkkBonanza; July 21st, 2012 at 12:41 PM.
    Send tips to: 17raXAGM42vZX21Vcb5HDwq2GMLmVwN4qd

  2. #12
    Join Date
    Jan 2010
    Beans
    60

    Re: Ubuntu Server - Shutdown script

    Thanks for the help BkkBonanza.

    I thought it would be easier to accomplish this. I'll need some more reading.

    After further tests with mysql conf file and/or apcupsd I come back to tell the results and maybe ask for more help.

  3. #13
    Join Date
    Jul 2012
    Beans
    2

    Re: Ubuntu Server - Shutdown script

    Quote Originally Posted by andreiflorescu View Post
    I am not an expert.
    I found this solution.
    I would like to hear comments on it.
    Maybe it is not the right one.

    I wrote a script to dump the database on shutdown, and put it in /etc/init.d/ ...
    Before the dump operation I started the mysql daemon. And it worked.
    I use ubuntu 11.10.

    something like:

    service mysql start
    mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
    service mysql stop

    I wrote two files

    /etc/init.d/backup_mysql
    /etc/init.d/backup_mysql.sh

    owner and permission for this two files
    -rwxr-xr-x 1 root root 187 2012-07-23 07:51 /etc/init.d/backup_mysql
    -rwxr-xr-x 1 root root 542 2012-07-23 07:56 /etc/init.d/backup_mysql.sh

    content of this two files
    1.) /etc/init.d/backup_mysql

    #!/bin/bash
    #backup database anatomie

    echo "begin database backup"
    mysqldump -u root --password='password' --opt anatomie > /home/andrei/backupMYSQL/backup.sql
    echo "end database backup"

    2.) /etc/init.d/backup_mysql.sh

    #!/bin/bash
    #backup database anatomie

    #redirect stdout and stderr to file /home/andrei/erori
    #for this script

    #status of mysql daemon and start mysql
    service mysql status 2>/home/andrei/erori 1>>/home/andrei/erori
    echo "start mysql daemon" >>/home/andrei/erori
    service mysql start 2>>/home/andrei/erori 1>>/home/andrei/erori

    #run scrip backup_mysql that backs up the database anatomie
    /etc/init.d/backup_mysql 2>>/home/andrei/erori 1>>/home/andrei/erori

    #stop mysql daemon
    service mysql stop 2>>/home/andrei/erori 1>>/home/andrei/erori

    - made appropriated links
    sudo update-rc.d backup_mysql.sh start 10 0 6 .

    - created the directory /home/andrei/backupMYSQL

    For me this worked.
    I am realy interested.
    Does it work for you ?

  4. #14
    Join Date
    Jan 2010
    Beans
    60

    Re: Ubuntu Server - Shutdown script

    Hello andrei. Thx for your help.

    Since Upstart is the replacement of init.d, I got interested in learning it. So I am firstly making the tests BkkBonanza suggested. Soon I 'll make your tests too.

    After some reading on Upstart I got what BkkBonanza meant. Instead of making a new job, I could work on mysql's one. So the first thing I tried, as suggested, was to put a pre-stop script on mysql that calls my backup script. It worked. Now the resulting file wasn't void anymore.

    Since this dump takes just 1 second or so, I still don't know if system will wait for the other part of the process, taht is to upload the resulting dump file via ssh, which would take about 5 min.

    This will be my next test. To put the ssh upload on the pre-stop script.

    Soon I come back with the results.

  5. #15
    Join Date
    Jan 2010
    Beans
    60

    Re: Ubuntu Server - Shutdown script

    I made one test and it seems it worked! I just had a problem with permissions on the uploaded file, but that is a problem related to the rsync command itself, not with the Upstart script.

    I will learn to generate the pass keys for ssh and make some tests.

  6. #16
    Join Date
    Jan 2010
    Beans
    60

    Re: Ubuntu Server - Shutdown script

    I made further tests. Now I understood how rsa keys works and generated the needed ones for the passwordless ssh upload of the mysql dump.

    After that, I had another problem with the rsync command, because I have spaces on folder names. After solving this, all worked as expected.

    So, concerning the specific problem of the shutdown script, a pre-stop condition on mysql Upstart script, as suggested by BkkBonanza, was all that was needed.

    Due to rsync compression, my upload is actually taking less than a minute, so I still don't know if other problems could happen if pre-stop condition took too much time.

    I will make some last tests, if all works as worked now this is solved. Thank you all for your help.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •