Announcement

Collapse
No announcement yet.

Some other way to run my backup script at shutdown?

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [SOLVED] Some other way to run my backup script at shutdown?

    I posted elsewhere on these Forums a tutorial to set up a system for backing up using rdiff-backup.
    It is now only partly working in Kubuntu 15.04.
    The hourly backups proceed successfully, but the shutdown backup just won't go.
    I've been trying to set up a new backup script to run at shutdown.
    The method I wanted to use appears to be redundant, probably since the changes to K/Ubuntu.
    So putting a script into init.d then making links to it in rc.0 and rc.6 no longer works.
    After a swift Google I see no alternatives...
    but there must be some other way to do this, I just don't see it yet.

    These things always turn into voyages of discovery. I'll report anything of significance that I glean in my interweb investigation.

    #2
    You might want to try a systemd service file. Kubuntu uses systemd now, right?

    Code:
    [COLOR=#333333][FONT=sans-serif][Unit][/FONT][/COLOR][COLOR=#333333][FONT=sans-serif]Description=Your description
    Before=shutdown.target[/FONT][/COLOR]
    [COLOR=#333333][FONT=sans-serif][Service]
    ExecStart=/bin/true
    ExecStop=your backup script, bin, blob, executable, whatever
    RemainAfterExit=yes[/FONT][/COLOR]
    [COLOR=#333333][FONT=sans-serif][Install]
    WantedBy=multi-user.target[/FONT][/COLOR]


    Write it as root and put it in /etc/systemd/system (may need some help with Kubuntu's directory structure) and name it whatever.service
    Then sudo systemctl enable whatever.service
    And sudo systemctl start whatever.service

    There are tons and tons of how-to's for systemd services, especially from fedora and arch.
    I do not personally use Kubuntu, but I'm the tech support for my daughter who does.

    Comment


      #3
      Thanks Buddlespit, I've never written or executed a service file but I'll look into that. As far as I understand it, Kubuntu does now use systemd, so I better get used to it, I suppose.
      Anything I discover that could be of use I will report here.

      Comment


        #4
        This is another good read, for those wishing to convert their sysv init.d scripts to systemd:
        http://0pointer.de/blog/projects/sys...-admins-3.html
        I do not personally use Kubuntu, but I'm the tech support for my daughter who does.

        Comment


          #5
          This is information for system administrators - more than a little over my head.
          I will persevere, looking more for advice for numpties, it must be out there, systemd has become so popular.
          ...and my backups still need to be made at shutdown.

          Comment


            #6
            I opened kate,
            Code:
            kdesudo kate
            then entered this text:
            Code:
            [COLOR=#333333][FONT=sans-serif][Unit][/FONT][/COLOR][COLOR=#333333][FONT=sans-serif]Description=Backup at Shutdown
            Before=shutdown.target[/FONT][/COLOR]
            [COLOR=#333333][FONT=sans-serif][Service]
            ExecStart=/bin/true
            ExecStop=/path/to/local-backup.sh
            RemainAfterExit=yes[/FONT][/COLOR]
            [COLOR=#333333][FONT=sans-serif][Install]
            WantedBy=multi-user.target[/FONT][/COLOR]

            and saved it as /etc/systemd/system/backup.service

            I then entered in terminal
            Code:
            [COLOR=#333333][FONT=sans-serif]sudo systemctl enable backup.service[/FONT][/COLOR]

            which responded with
            Failed to execute operation: Bad message
            I know my script local-backup.sh does work. It is performing hourly backups with no problem.
            I'm still searching for something that will allow backup at shutdown.










            Last edited by bobbicat; May 28, 2015, 05:11 AM.

            Comment


              #7
              Try to enable it again and when it fails, type:
              Code:
              sudo journalctl -xe
              and post the output, please?
              I do not personally use Kubuntu, but I'm the tech support for my daughter who does.

              Comment


                #8
                I have been trying variations on that service (is that what its called?)

                This is the closest I got to it working:
                Heres the code:
                Code:
                [Unit]
                Description=Backup before shutdown
                DefaultDependencies=no
                Before=shutdown.target reboot.target halt.target
                
                [Service]
                ExecStart=/bin/bash /mnt/backup/info/local-backup.sh --stop
                Type=oneshot
                
                [Install]
                WantedBy=multi-user.target
                then I ran as you suggested:
                Code:
                sudo systemctl enable local-backup.service
                and got this response:
                Created symlink from /etc/systemd/system/multi-user.target.wants/local-backup.service to /etc/systemd/system/local-backup.service.
                I then ran this:
                sudo systemctl start local-backup.service
                and found that I had successfully made a backup

                However, when shutting down or rebooting, no backup was made.

                I've re-introduced the above code, re-run start local-backup.service and now run the code you asked me to perform.

                here's the result of

                Code:
                sudo journalctl -xe
                May 28 15:52:40 masked org.kde.kded5[3740]: kf5.kded: kded module "notificationhelper" still uses .desktop files ("kded/notificationhelper.desktMay 28 15:52:40 masked org.kde.kded5[3740]: kf5.kded: kded module "proxyscout" has already been found using JSON metadata, please don't install
                May 28 15:52:40 masked org.kde.kded5[3740]: kf5.kded: kded module "kded_baloosearch_kio" still uses .desktop files ("kded/baloosearchfolderupdat
                May 28 15:52:51 masked sudo[3723]: pam_unix(sudo:session): session closed for user root
                May 28 15:53:10 masked sudo[3918]: MYID : TTY=pts/1 ; PWD=/home/MYID ; USER=root ; COMMAND=/bin/systemctl enable local-backup.service
                May 28 15:53:10 masked sudo[3918]: pam_unix(sudo:session): session opened for user root by MYID(uid=0)
                May 28 15:53:10 masked polkitd(authority=local)[845]: Registered Authentication Agent for unix-process:3919:133502 (system bus name :1.1824 [/us
                May 28 15:53:10 masked systemd[1]: Configuration file /etc/systemd/system/local-backup.service is marked world-inaccessible. This has no effect
                May 28 15:53:10 masked systemd[1]: Reloading.
                May 28 15:53:10 masked systemd[1]: Configuration file /etc/systemd/system/local-backup.service is marked world-inaccessible. This has no effect
                May 28 15:53:10 masked systemd[1]: Started CUPS Scheduler.
                -- Subject: Unit cups.service has finished start-up
                -- Defined-By: systemd
                -- Support: http://lists.freedesktop.org/mailman.../systemd-devel
                --
                -- Unit cups.service has finished starting up.
                --
                -- The start-up result is done.
                May 28 15:53:10 masked sudo[3918]: pam_unix(sudo:session): session closed for user root
                May 28 15:53:10 masked polkitd(authority=local)[845]: Unregistered Authentication Agent for unix-process:3919:133502 (system bus name :1.1824, o
                May 28 15:53:22 masked sudo[3952]: MYID : TTY=pts/1 ; PWD=/home/MYID ; USER=root ; COMMAND=/bin/systemctl start local-backup.service
                May 28 15:53:22 masked sudo[3952]: pam_unix(sudo:session): session opened for user root by MYID(uid=0)
                May 28 15:53:22 masked polkitd(authority=local)[845]: Registered Authentication Agent for unix-process:3953:134745 (system bus name :1.1842 [/us
                May 28 15:53:22 masked systemd[1]: Starting Backup before shutdown...
                -- Subject: Unit local-backup.service has begun start-up
                -- Defined-By: systemd
                -- Support: http://lists.freedesktop.org/mailman.../systemd-devel
                --
                -- Unit local-backup.service has begun starting up.
                May 28 15:53:33 masked bash[3958]: No increments older than Tue May 26 15:53:33 2015 found, exiting.
                May 28 15:53:33 masked systemd[1]: Started Backup before shutdown.
                -- Subject: Unit local-backup.service has finished start-up
                -- Defined-By: systemd
                -- Support: http://lists.freedesktop.org/mailman.../systemd-devel
                --
                -- Unit local-backup.service has finished starting up.
                --
                -- The start-up result is done.
                May 28 15:53:33 masked polkitd(authority=local)[845]: Unregistered Authentication Agent for unix-process:3953:134745 (system bus name :1.1842, o
                May 28 15:53:33 masked sudo[3952]: pam_unix(sudo:session): session closed for user root
                May 28 15:54:06 masked sudo[4028]: MYID : TTY=pts/1 ; PWD=/home/MYID ; USER=root ; COMMAND=/bin/journalctl -xe
                May 28 15:54:06 masked sudo[4028]: pam_unix(sudo:session): session opened for user root by MYID(uid=0)
                May 28 15:54:25 masked sudo[4028]: pam_unix(sudo:session): session closed for user root
                May 28 16:05:07 masked sudo[4987]: MYID : TTY=pts/1 ; PWD=/home/MYID ; USER=root ; COMMAND=/bin/journalctl -xe
                May 28 16:05:07 masked sudo[4987]: pam_unix(sudo:session): session opened for user root by MYID(uid=0)
                Last edited by bobbicat; May 28, 2015, 10:24 AM.

                Comment


                  #9
                  Let's try this from a different direction. Can you copy your script to /usr/lib/systemd/system-shutdown/, make sure it's executable, and try a reboot and then another shutdown/reboot?
                  I do not personally use Kubuntu, but I'm the tech support for my daughter who does.

                  Comment


                    #10
                    there is no /usr/lib/systemd/system-shutdown/ folder

                    usr/lib/systemd/ contains /catalog/ /network/ /user/ and /user-generators/


                    I will create directory /usr/lib/systemd/system-shutdown/ and place /local-backup.sh within it.

                    I will also edit the service to point to that address.

                    After these actions:
                    Reboot does not create a backup, neither does shutdown.

                    [also did check - local-backup.sh is executable]
                    Last edited by bobbicat; May 29, 2015, 02:24 AM.

                    Comment


                      #11
                      My bash backup script calls on two files, an include and an exclude file, listing what to backup and what to ignore. Is this relevant to setting up a service file?

                      Comment


                        #12
                        Crap... I'm working blind here. There's a reason why I was told to just help the distro I use. Any systemd gurus using Kubuntu, feel free to cut in! I'm off all weekend and can help as needed!

                        OK, back to the service file. Could you run:
                        Code:
                        sudo [COLOR=#333333][FONT=Ubuntu Beta]systemd-analyze verify [/FONT][/COLOR][FONT=sans-serif]local-backup.service[/FONT]
                        and post it here? That should point us in the right direction as to why the service unit file is failing at shutdown. Also, and this shouldn't be affecting anything, the service file is being marked world-inaccessible, so lets also try to change the permissions to rw/r/r
                        Code:
                        sudo chmod 0644 /etc/systemd/system/local-backup.service
                        And just for ****s~n~giggles, could you post the contents of your script local-backup.sh?
                        I do not personally use Kubuntu, but I'm the tech support for my daughter who does.

                        Comment


                          #13
                          I have to get out for an hour - but will be back to try this out.
                          In the mean time here's the files as I've been using them prior to systemd:

                          ----------------------------------------------------------------------------------
                          info v.1.3
                          ----------------------------------------------------------------------------------
                          reference:
                          http://www.kirya.net/articles/backup...-rdiff-backup/
                          ----------------------------------------------------------------------------------
                          ----------------------------------------------------------------------------------
                          Code:
                          sudo apt-get install rdiff-backup

                          Code:
                          sudo mkdir /mnt/backup
                          sudo chown -R user:user /mnt/backup
                          mkdir /mnt/backup/info
                          mkdir /mnt/backup/storage
                          sudo blkid
                          kdesudo kate /etc/fstab
                          UUID=???   /mnt/backup     ext4    noatime,defaults    0   2

                          ----------------------------------------------------------------------------------
                          ----------------------------------------------------------------------------------
                          write and save the following scripts to /mnt/backup/info:
                          ----------------------------------------------------------------------------------


                          ----------------------------------------------------------------------------------
                          /mnt/backup/info/backup.exclude
                          ----------------------------------------------------------------------------------


                          Code:
                          /home/user/.adobe/
                          /home/user/.audacity-data/
                          /home/user/.cache/
                          /home/user/.config/
                          /home/user/.dbus/
                          /home/user/.gconf/
                          /home/user/.hplip/
                          /home/user/.java/
                          /home/user/.kde/
                          /home/user/.local/
                          /home/user/.macromedia/
                          /home/user/.nv/
                          /home/user/.sane/
                          /home/user/.thumbnails/
                          **.iso
                          ----------------------------------------------------------------------------------
                          /mnt/backup/info/backup.include
                          ----------------------------------------------------------------------------------


                          Code:
                          /home/user/

                          ----------------------------------------------------------------------------------
                          /mnt/backup/info/local-backup
                          ----------------------------------------------------------------------------------


                          Code:
                          #!/bin/sh
                          #
                          # local-backup v1.3 
                          # script by bob
                          # to make a simple hourly backup
                          # make this script executable and place in /mnt/backup/info
                          #
                          # edit /etc/crontab to adjust timing
                          # 0 *    * * *    root    cd / && run-parts --report /etc/cron.hourly
                          #
                          # to create the backup at shutdown:
                          #
                          # sudo cp /etc/cron.hourly/local-backup /etc/init.d
                          # sudo ln -s /etc/init.d/local-backup /etc/rc0.d/K10local-backup
                          # sudo ln -s /etc/init.d/local-backup /etc/rc6.d/K10local-backup
                          #
                          # data files are in /mnt/backup/info/
                          # output will be saved to backup drive /mnt/backup/storage
                          #
                          
                          
                          set -e
                          bash /mnt/backup/info/local-backup.sh
                          
                          
                          # make this script executable and place in /mnt/backup/info

                          ----------------------------------------------------------------------------------
                          /mnt/backup/info/local-backup.sh
                          ----------------------------------------------------------------------------------


                          Code:
                          #!/bin/bash
                          #
                          # local-backup.sh v1.3
                          # script by bob 
                          # purpose - to make simple backups to a dedicated folder
                          # 
                          # make this script executable and place in /mnt/backup/info
                          #
                          # backup drive is at /mnt/backup/storage/
                          # data files are in /mnt/backup/info
                          # 
                          
                          
                          /usr/bin/rdiff-backup --exclude-globbing-filelist /mnt/backup/info/backup.exclude \
                              --include-globbing-filelist /mnt/backup/info/backup.include \
                              --exclude / / /mnt/backup/storage/ 
                          
                          
                          # The following line removes restores which have been current for over [2] days
                          
                          
                          /usr/bin/rdiff-backup --remove-older-than 2D --force /mnt/backup/storage/
                          
                          
                          # make this script executable and place in /mnt/backup/info
                          ----------------------------------------------------------------------------------
                          ----------------------------------------------------------------------------------
                          Give execute permissions to your scripts
                          ----------------------------------------------------------------------------------


                          Code:
                          chmod +x /mnt/backup/info/local-backup
                          chmod +x /mnt/backup/info/local-backup.sh

                          ----------------------------------------------------------------------------------
                          ----------------------------------------------------------------------------------
                          place a copy of local-backup in /etc/cron.hourly to get hourly backups
                          ----------------------------------------------------------------------------------


                          Code:
                          sudo cp /mnt/backup/info/local-backup /etc/cron.hourly



                          ----------------------------------------------------------------------------------
                          ----------------------------------------------------------------------------------
                          edit /etc/crontab so that backups are on the hour
                          as always backup up your crontab - save as /etc/crontab.backup
                          ----------------------------------------------------------------------------------


                          Code:
                          kdesudo kate /etc/crontab
                          then edit the hourly line to set required minutes
                          Code:
                          0  *    * * *    root    cd / && run-parts --report /etc/cron.hourly
                          ----------------------------------------------------------------------------------
                          ----------------------------------------------------------------------------------
                          wait until the hour has past and check that everything is running smoothly
                          by running this code, which will will report on backups made:
                          ----------------------------------------------------------------------------------


                          Code:
                          sudo rdiff-backup -l /mnt/backup/storage

                          ----------------------------------------------------------------------------------
                          ----------------------------------------------------------------------------------
                          making a backup at shutdown - not possible since systemd - not know how as yet
                          copy script from /etc/cron.hourly to /etc/init.d
                          ----------------------------------------------------------------------------------


                          Code:
                          sudo cp /etc/cron.hourly/local-backup /etc/init.d
                          sudo ln -s /etc/init.d/local-backup /etc/rc0.d/K10local-backup
                          sudo ln -s /etc/init.d/local-backup /etc/rc6.d/K10local-backup
                          ----------------------------------------------------------------------------------
                          ================================================== ====================================
                          ----------------------------------------------------------------------------------
                          Displaying Versions of a Backup
                          ----------------------------------------------------------------------------------


                          The existing versions of the backup can be displayed using the following command.


                          Code:
                          sudo rdiff-backup -l /mnt/backup/storage/

                          ----------------------------------------------------------------------------------
                          ----------------------------------------------------------------------------------
                          Restoring
                          ----------------------------------------------------------------------------------


                          The restoring process is very well explained in the official documentation: http://www.nongnu.org/rdiff-backup/e...s.html#restore


                          Just an example to restore one directory from the version backed up 10 days ago in a given directory:


                          Code:
                          rdiff-backup -r 10D user@backserv::/remote/ ./local/

                          Remember it is very important to test your backups!


                          ----------------------------------------------------------------------------------
                          ----------------------------------------------------------------------------------
                          NOTES
                          ----------------------------------------------------------------------------------
                          Entries in cron.hourly are run by the run-parts mechanism (man run-parts for more info).
                          And run-parts is choosy about what filenames it considers valid.
                          [a-zA-Z0-9_-] are the valid characters, so the '.' makes it invalid.
                          ----------------------------------------------------------------------------------
                          When adding a job to /etc/cron.hourly ( or .daily, .weekly, etc), always test afterwards that run-parts will actually run it by issuing the command:
                          run-parts --test /etc/cron.hourly
                          ----------------------------------------------------------------------------------
                          /etc/cron.hourly/local-backup
                          ----------------------------------------------------------------------------------
                          –remove-older-than 1Y – Remove backups older than one year
                          –remove-older-than 1M – Remove backups older than one month
                          –remove-older-than 1W – Remove backups older than one week
                          –remove-older-than 1h – Remove backups older than one hour
                          –remove-older-than 1m – Remove backups older than one minute
                          –remove-older-than 1s – Remove backups older than one second


                          Code:
                          rdiff-backup –force –remove-older-than 15W /mnt/backup/storage
                          ----------------------------------------------------------------------------------

                          Comment


                            #14
                            This is my current local-backup.service at /etc/systemd/system/:

                            Code:
                            [Unit]
                            Description=Backup before shutdown
                            DefaultDependencies=no
                            Before=shutdown.target reboot.target halt.target
                            
                            [Service]
                            ExecStart=/bin/bash /mnt/backup/info/local-backup.sh --stop
                            Type=oneshot
                            
                            [Install]
                            WantedBy=multi-user.target
                            As you suggested I ran:
                            Code:
                            [FONT=monospace]sudo systemd-analyze verify local-backup.service[/FONT]
                            resulting in:
                            Configuration file /etc/systemd/system/local-backup.service is marked executable. Please remove executable permission bits. Proceeding anyway.
                            Unit home.mount is bound to inactive unit dev-sdb1.device. Stopping, too.
                            I removed the exec permissions and ran again:
                            Code:
                            [FONT=monospace]sudo systemd-analyze verify local-backup.service[/FONT]
                            which this time gave:
                            Unit mnt-backup.mount is bound to inactive unit dev-sdc1.device. Stopping, too.
                            Unit boot-efi.mount is bound to inactive unit dev-sda1.device. Stopping, too.
                            sda1 is the /boot/efi fat32 partition on a solid state drive
                            sdb1 is user/home partition on a hard drive
                            sdc1 is on a hard drive where /mnt/backup/ is situated

                            I ran again after a reboot:

                            Code:
                            sudo systemd-analyze verify local-backup.service
                            and got the same response.
                            Last edited by bobbicat; May 29, 2015, 11:10 AM.

                            Comment


                              #15
                              Thanks, Buddlespit, I appreciate your efforts. I'm sure you need some time away from this and like you say, someone who is on the same platform as me would probably see things more plainly.
                              Who knows, I might just stumble across an answer, its happened before. You've given me some pointers, could be it will be enough.
                              So many thanks, please don't feel obliged. I get the situation.

                              Comment

                              Working...
                              X