Mount only at backup time? [SOLVED]

Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
momist
Level 4
Level 4
Posts: 248
Joined: Mon May 21, 2012 3:30 pm
Location: Lancashire, Northwest England

Mount only at backup time? [SOLVED]

Post by momist »

I know, there are zillions of answers here about how to mount partitions. Sorry, but I can't find anything like this question.
I have a separate HD just for my backups, and I like to use BackInTime to automatically back up my files once a day. Is it possible to get BackInTime, or the scheduler, to mount the HD just to do the backup and then unmount it when it has finished? If so, how, please.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
momist : a follower of the Greek god Momer.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Mount only at backup time?

Post by rene »

I'm not a backintime user but if you check the manpage (man backintime from a terminal), also available online at:

http://manpages.ubuntu.com/manpages/xen ... ime.1.html

then you will find it to mention a ~/.config/backintime/user-callback script that gets called with a third argument of 7 and 8 for respectively "mount all necessary drives" and "unmount all drives". Example scripts are available from for example:

https://github.com/bit-team/user-callback

with specifically

https://github.com/bit-team/user-callba ... ck.default

the most basic one, easily adapted for any use.

[EDIT] Edited "first" to "third" (argument) in the above.
User avatar
Flemur
Level 20
Level 20
Posts: 10097
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Mount only at backup time?

Post by Flemur »

Is it possible to get BackInTime, or the scheduler, to mount the HD just to do the backup and then unmount it when it has finished? If so, how, please.
Put a line in /etc/fstab for the partition you want to mount/unmount and use "noauto" as below:

Code: Select all

LABEL=LBAK    /mnt/LBAK    ext4    noauto,user   0 0
"noauto" = don't mount automatically. (LBAK=LinuxBAcKup) /mnt/LBAK has to exist.

You can mount it with, among other things,

Code: Select all

mount /mnt/LBAK
and unmount with

Code: Select all

umount /mnt/LBAK
I have another partition that I only mount for backups, which is NTFS, the main difference is you have to be root/sudo to mount it (which is the default linux ntfs functionality for some reason, and hard or impossible to fix).

So your script would be something like
- mount backup partition
- run BackInTime
- unmount backup partition
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
momist
Level 4
Level 4
Posts: 248
Joined: Mon May 21, 2012 3:30 pm
Location: Lancashire, Northwest England

Re: Mount only at backup time?

Post by momist »

Thanks for your reply @rene
rene wrote:I'm not a backintime user but if you check the manpage (man backintime from a terminal), also available online at:

http://manpages.ubuntu.com/manpages/xen ... ime.1.html

then you will find it to mention a ~/.config/backintime/user-callback script that gets called with a third argument of 7 and 8 for respectively "mount all necessary drives" and "unmount all drives". Example scripts are available from for example:
<snip>
I have looked at the links you gave, and that seems to be exactly what I need, but I am really struggling to understand how to use it. It seems from looking around that the "user-callback" process is a recognised process, so much so that no-one bothers to explain _how_ it should be used. I can see the structure and the parameters etc., but just how do I use this with backintime?

Any further help welcome! Thanks.
momist : a follower of the Greek god Momer.
momist
Level 4
Level 4
Posts: 248
Joined: Mon May 21, 2012 3:30 pm
Location: Lancashire, Northwest England

Re: Mount only at backup time?

Post by momist »

Thanks for that @Flemur.
Deleted post*

EDIT: *Apologies, I was looking for directories, of course the files are there in /etc. Sorry.
momist : a follower of the Greek god Momer.
momist
Level 4
Level 4
Posts: 248
Joined: Mon May 21, 2012 3:30 pm
Location: Lancashire, Northwest England

Re: Mount only at backup time?

Post by momist »

Flemur wrote: <snip>
(LBAK=LinuxBAcKup) /mnt/LBAK has to exist.

You can mount it with, among other things,

Code: Select all

mount /mnt/LBAK
and unmount with

Code: Select all

umount /mnt/LBAK
OK Flemur, I can see this working but I still have a few problems with it.
1. I can see no entry in crontab referring to backintime, even though I have set it up (under root) to do a backup daily at 22:00. I can't see how backintime gets initiated, so I don't know how to trigger the script at the right time.
2. I currently have no entries under /mnt. When I mount the partition BackupDrive on the extra hard drive, the entry appears in /media/ian. The SSD and main HD must be mounted somewhere else. There are lots of entries under /dev/disk showing uuid etc. The id of the backup HD is shown there under /dev/disk/by-id/ but all the entries in /dev/disk appear to be symlinks elsewhere. The other entries currently in /etc/fstab give uuid's, names are only given after that. I suspect I will have to find the uuid of my backup partition. If I do that, will mount and umount still work with the name or will I have to use the uuid?
momist : a follower of the Greek god Momer.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Mount only at backup time?

Post by rene »

Unfortunately I need to first make sure you are running Mint 18; the above linked backintime manpage is for Ubuntu 16.04 / Mint 18 and specifically the 7 and 8 reasons to the user-callback script require backintime 1.1, whereas Ubuntu 14.04 / Mint 17.x has version 1.0 in the repositories. Even if running Mint 18, verify your version of backintime through the output of

Code: Select all

backintime --version
Assuming however that you have a new enough version of backintime installed...

The user-callback "process" is not some sort of standard process, mind you. Expected to be found in the directory ~/.config/backintime (the initial tilde means your home directory), it is the name of a program/script to be supplied by you that the main backintime program calls at various points during a backup run. That is, let us grab the "template script" as linked above with for example,

Code: Select all

wget -O ~/.config/backintime/user-callback https://raw.githubusercontent.com/bit-team/user-callback/master/user-callback.default
and set it executable with

Code: Select all

chmod +x ~/.config/backintime/user-callback
Then edit it to provide your requested functionality. I.e., something like

Code: Select all

...
    6) #backintime-qt4 (GUI) closed
        ;;
    7) #Mount drives
        /bin/mount /mnt/LBAK
        exit $?
        ;;
    8) #Unmount the drives
        /bin/umount /mnt/LBAK
        exit $?
        ;;
esac
...
in which I refer to Flemur's reply as to giving yourself the possibility to mount your device as your user rather than needing sudo and a password (-entry possibility).

More than not using backintime myself I in fact run 17.3 and have not tested any of this. I hope this works for you though? It should be the case that backintime then mounts/unmounts your device itself during a run.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Mount only at backup time?

Post by rene »

momist wrote:2. I currently have no entries under /mnt. When I mount the partition BackupDrive on the extra hard drive, the entry appears in /media/ian.
That is a bit confusingly formulated. Explicit "mounting" of said partition would consist of you making it accessible somewhere explicitly and not of it "appearing" somewhere. I take it you mean that when you switch on (?) the backup drive its only/backup partition is currently automatically mounted on /media/ian/BackupDrive? And there is no current entry for the partition in your /etc/fstab? If respectively yes and no, you can as per Flemur's post add a line

Code: Select all

LABEL=BackupDrive	/mnt/backup	auto		noauto,user
to your /etc/fstab, create the mountpoint with

Code: Select all

sudo mkdir /mnt/backup
and then mount/unmount the partition with, as user and without sudo,

Code: Select all

(u)mount /mnt/backup
If there's trouble you may need to replace the "auto" type in /etc/fstab with the actual file system type such as ext4.

In the context of my own user-callback post above, at that point backintime can also when running as your user at that point mount your BackupDrive. Cron configuration is perpendicular to all of this -- but do note that we are setting up things to run as your user, user "ian" it seems, and would as such use ian's crontab for this and not roots.
momist
Level 4
Level 4
Posts: 248
Joined: Mon May 21, 2012 3:30 pm
Location: Lancashire, Northwest England

Re: Mount only at backup time?

Post by momist »

Explicit "mounting" of said partition would consist of you making it accessible somewhere explicitly and not of it "appearing" somewhere. I take it you mean that when you switch on (?) the backup drive its only/backup partition is currently automatically mounted on /media/ian/BackupDrive? And there is no current entry for the partition in your /etc/fstab?
Hi rene. Thanks for thinking so hard about this for me.
Yes, I'm using Mint 18 (Cinnamon), and my backintime is a new enough version. Mint Sarah has a handy menu entry under Preferences/Disks that lets you see what storage is connected to the PC and lets you mount/unmount at will. I was mounting the HD with the BackupDrive partition that way, when it appears under /media/ian/

I have now made an /etc/fstab entry as suggested by Flemur, and had to first find the UUID to make it work. The entry is now

Code: Select all

UUID=24486257-4702-46d1-ac72-8e4405a1840a /BackupDrive		ext4	noauto,user   0		0
It no longer appears in /media/ian if mounted by the menu entry, and is listed in the file system permanently, but appears as an empty folder if the disk has not been mounted. It works as expected if it is mounted from the menu, and can be mounted with with

Code: Select all

 sudo mount /BackupDrive
when it takes a little while to spin up.

My Backintime is set to run as root (there is a choice in the UI). That way, all users data can be backed up, as well as some entries in the /home folder which are owned by root. The Backintime program runs successfully as root (without user input) when/if the drive is mounted, so all I now need is to make a root entry in cron to mount the drive first. Backintime only allows daily occurrences 'on the hour', so I need a cron entry to trigger the mount at 21:59. I could use another entry to unmount it after half an hour, as it will never need more time than that. That has led me into investigating cron, anacron, and crontab. This is a whole new kettle of fish, as my /etc/crontab file has no entries other than calling anacron. I have so far failed to map out the process that calls Backintime using cron, although sudo crontab -l shows it's entry is present - I just can't find where! The whole cron/anacron system seems very convoluted (and system specific). There are various files in /var/spool wich are involved, but I havn't yet got my head around it.

Using the callback method would be far cleaner, and permit flexibility for setting backup to occur at different times, but it looks as complicated as the cron system to understand. I will give it a go and see what happens.
momist : a follower of the Greek god Momer.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Mount only at backup time?

Post by rene »

As to the "Disks" mounting-method; ah, I see.
momist wrote:It works as expected if it is mounted from the menu, and can be mounted with with

Code: Select all

 sudo mount /BackupDrive
when it takes a little while to spin up.
Looking good then. And you can in fact drop the "sudo" now that you have given mount permission for the partition to mere users with the "user" flag in /etc/fstab -- although that part of things is not in fact relevant now that you specified that you are running backintime as root. You could remove the "user" again from /etc/fstab or just leave it as is.
Momist wrote:My Backintime is set to run as root (there is a choice in the UI).
On 17.3 there's no UI choice as such but two separate menu entries for a user and a root instance of backintime. I checked due to this being relevant for where the user-callback script will be expected to reside . On 17.3 I in fact need to place the script in /root/.config/backintime/ for the root instance to call it, and assuming that hasn't changed for the newer backintime, tweak the above to read

Code: Select all

sudo wget -O /root/.config/backintime/user-callback https://raw.githubusercontent.com/bit-team/user-callback/master/user-callback.default
sudo chmod +x /root/.config/backintime/user-callback
gksudo gedit /root/.config/backintime/user-callback
and make it read

Code: Select all

...
    6) #backintime-qt4 (GUI) closed
        ;;
    7) #Mount drives
        /bin/mount /BackupDrive
        exit $?
        ;;
    8) #Unmount the drives
        /bin/umount /BackupDrive
        exit $?
        ;;
esac
...
It is a little unfortunate that the Mint 18 version is somewhat different; it means I can't specifically test the 7 and 8 cases, and if it uses "sudo" for a root backup run, it may in fact be the case that you need the script in /home/<user>/.config/backintime after all as per original reply. Experiment if it doesn't work with the user-callback script in /root/.config/backintime/.

But I would say that this should not count as complex.
momist
Level 4
Level 4
Posts: 248
Joined: Mon May 21, 2012 3:30 pm
Location: Lancashire, Northwest England

Re: Mount only at backup time?

Post by momist »

Note to self: Be more precise in your language.
Thanks again rene. You are correct, the choice of backintime as user or as root is in the two entries in the Mint menu structure, which is what I should have said. Once the UI is open as one or the other, there is no further choice. I don't believe this is different than in Mint 17.3
I was using Backintime during the time I ran Mint 16 and 17, but always as 'user' and it annoyed me that other users were not included in the backups, and nor were some items in /home/ian which were owned by root. My backup drive failed at the exact time that I started a fresh install of Mint 18, together with the use of a new SSD for the system files and /home. I have sym links for larger users of /home such as documents, music, photos etc., which puts those files on my remaining hard drive. I bought another old used hard drive for backups (cheap) but it is a very noisy thing that clunks a lot in use, and I can hear the disks spinning, which is why I want it to only mount when the backup system is using it.
I put 'user' in the fstab entry so that I can check up and see the backups, as user, should I need to. Of course that is with read only permission then, as the backups are owned by root. If I needed them, I would do so as superuser.

I don't have time just now to go through what you have told me, so I will do that as soon as I can, and get back to you with success (or failure) as and when I can. Thank you again for your time.
Ian
momist : a follower of the Greek god Momer.
Habitual

Re: Mount only at backup time?

Post by Habitual »

some code I wrote 4 years ago.

Code: Select all

if mountpoint -q /media/backup1
  then
    echo "Backup Drive #1 is mounted."

       mkdir /media/backup1/rsync/log
       rsync -verbose -a {/home,/var} /media/backup1/rsync > /media/backup1/rsync/log/$LOG.txt
       mutt -s "Backup to Backup Drive 1" -a /media/backup1/rsync//log/$LOG.txt -- myemail@domain.com

else
        echo "Backup Drive #1 not mounted."
fi
Get some!
momist
Level 4
Level 4
Posts: 248
Joined: Mon May 21, 2012 3:30 pm
Location: Lancashire, Northwest England

Re: Mount only at backup time?

Post by momist »

@rene, you are a star! I managed to find a half hour to sit and think may way through what you suggested, and then make the changes you said, and it works when I start backintime. Thank you so much for your assistance, and I now know what callback is and how it is used. Only need a final test to see if it works at 22:00 local time tonight, and I am very happy. I will not be here to check on that at that time, but I will look for the new backup when I get in later.

@Habitual, thanks for your input, but I think the solution provided by rene, and the callback function of backintime, is what I was after, and it appears to be working.
momist : a follower of the Greek god Momer.
Habitual

Re: Mount only at backup time? [SOLVED]

Post by Habitual »

Glad it worked out.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Mount only at backup time?

Post by rene »

Thanks for reporting back; good to hear.

Let me comment on a detail: you may have wondered about me explicitly specifying /bin/mount and /bin/umount rather than just mount and umount. There's near zero chance you actually need that but executing binaries such as mount without specifying their full path of course depends on their location being part of the current $PATH, i.e., on the current environment. I with the explicit specification of /bin/ aimed to emphasize that you should in situations like this, where one program/script is called by another, make few assumptions about the latter's environment, said environment being fully determined by the former.

The above bit is not important to the issue at hand; feel free to pay it little attention, certainly when it's not clear. It's good to have it once heard said though; awareness of environmental differences might at some point save you some head-scratching when you're debugging something.
mintjelly
Level 1
Level 1
Posts: 8
Joined: Fri Dec 18, 2015 6:01 am

Re: Mount only at backup time?

Post by mintjelly »

momist wrote: Sat Aug 20, 2016 12:18 pm Thanks for your reply @rene
rene wrote:I'm not a backintime user but if you check the manpage (man backintime from a terminal), also available online at:

http://manpages.ubuntu.com/manpages/xen ... ime.1.html

then you will find it to mention a ~/.config/backintime/user-callback script that gets called with a third argument of 7 and 8 for respectively "mount all necessary drives" and "unmount all drives". Example scripts are available from for example:
<snip>
I have looked at the links you gave, and that seems to be exactly what I need, but I am really struggling to understand how to use it. It seems from looking around that the "user-callback" process is a recognised process, so much so that no-one bothers to explain _how_ it should be used. I can see the structure and the parameters etc., but just how do I use this with backintime?

Any further help welcome! Thanks.
Hello momist,

I am struggling for about 12 hours in total trying to figure out how to use "user-callback". I see you've figured it out.
I've already set chmod +x and placed it in .config/backintime/

The part I can't figure out is what to edit in the file "user-callback" to make it work.
Would you be so kind to show me a copy of your file as you set it up.

The only difference, I'm not running BIT as root like you are. I just need it mount > backup > unmount as user.

Any thing you can add to point me in the right direction would be so much appreciated.

Thank you
momist
Level 4
Level 4
Posts: 248
Joined: Mon May 21, 2012 3:30 pm
Location: Lancashire, Northwest England

Re: Mount only at backup time? [SOLVED]

Post by momist »

Hi mintjelly. I can try to find it for you, but I can't promise any success. Time moves on, and my wife has persuaded me to ditch the old noisy giant PC and I've now got a sleek new laptop with only a 256GB SSD. My backups are now on the old HDDs in caddies, which I have to plug in and manually use to do backups. This might change, if I can figure out how to make BIT see that the drive is mounted and then auto backup, but I haven't applied myself to that yet.
First look at my old home folder doesn't show it up. [Can you remind me where BIT keeps that file please?]

EDIT:
Sorry, that's obvious when I read through the thread. Now here's a strange thing. My backup copy of my home folder, dated Sept 2016, shows the ~/.config/backintime folder as empty. I can't figure why that would be. I'll keep digging.

EDIT2:
I suspect that what is going on here, is that BackInTime was running as root, and therefore the config file is in the root folder structure, which I don't have backed up. My backups only show what was in my home folder and all the links out of that into bulk storage - i.e. photos, music etc. I never backed up the root structure, as this would simply be re-installed together with the operating system, if it ever needed re installing. Maybe I should have foreseen that this could be a problem, but I didn't. Sorry. If I can find a device to access the old SSD I still have, it should be in there somewhere, in the root folders.
momist : a follower of the Greek god Momer.
momist
Level 4
Level 4
Posts: 248
Joined: Mon May 21, 2012 3:30 pm
Location: Lancashire, Northwest England

Re: Mount only at backup time? [SOLVED]

Post by momist »

@mintjelly

Whew, that took some finding. The following was located on my old SSD at [root]/.config/backintime user-callback listed as a program (because it is executable).

Code: Select all

 #!/bin/bash
#    Copyright (c) 2012-2015 Germar Reitze
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


#Script should return 0 if everything is alright. Returncode !0 will cancle
#the running snapshot (BIT version >1.1.0).

# Modified 24 August 2016 by Ian (momist), as per instructions from @rene on 
# forums.linuxmint.com

profile_id="$1"
profile_name="$2"
reason="$3"

case $reason in
    1) #Backup process begins
        ;;
    2) #Backup process ends
        ;;
    3) #A new snapshot was taken
        snapshot_id="$4"
        snapshot_name="$5"
        ;;
    4) #There was an error
        errorcode="$4"
        case $errorcode in
            1) #ERROR The application is not configured
              ;;
            2) #ERROR A 'take snapshot' process is already running
              ;;
            3) #ERROR Can't find snapshots folder (is it on a removable drive ?)
              ;;
            4) #ERROR A snapshot for 'now' already exist
              ;;
        esac
        ;;
    5) #backintime-qt4 (GUI) started
        ;;
    6) #backintime-qt4 (GUI) closed
        ;;
    7) #Mount drives
		 /bin/mount /BackupDrive
			exit $?
        ;;
    8) #Unmount the drives
		/bin/umount /BackupDrive
		exit $?
        ;;
esac
I hope that solves it for you! Good luck.
Ian
momist : a follower of the Greek god Momer.
mintjelly
Level 1
Level 1
Posts: 8
Joined: Fri Dec 18, 2015 6:01 am

Re: Mount only at backup time? [SOLVED]

Post by mintjelly »

Hello momist,

Thank you taking the time to locate and post your user-callback. Sorry you had to go through much effort. I hope it comes back to you 10 fold.

Anyway. looking at your user-callback I see only the edit you had to make was:

Code: Select all

 7) #Mount drives
                 /bin/mount /BackupDrive
                        exit $?
        ;;
    8) #Unmount the drives
                /bin/umount /BackupDrive
                exit $?
The rest of the file required no editing. Right? So, The only other thing is whether I'll need to edit /etc/fstab/ so BIT can mount target drive as "user" instead of requiring sudo "root" privilege AND MAYBE, I'll need to define a mount point in fstab too. Hmmm.

I'll go ahead and give this a whirl.

Thank you again.

EDIT:
Wow.... I was over complicating it. I did the edit to "user-callback as per your sample and then only had to make the above mentioned changes to "fstab" and it works.
When I manually run BIT it automatically mounts target drive. Next is to see if:
a) when anacron runs BIT, the drive will mount? AND
b) whether the drive will umount automatically when the backup is finished.

Thanks momist,
Will let you know on a) and b)
mintjelly
Level 1
Level 1
Posts: 8
Joined: Fri Dec 18, 2015 6:01 am

Re: Mount only at backup time? [SOLVED]

Post by mintjelly »

mintjelly wrote: Wed Nov 28, 2018 1:28 am Hello momist,

Thank you taking the time to locate and post your user-callback. Sorry you had to go through much effort. I hope it comes back to you 10 fold.

Anyway. looking at your user-callback I see only the edit you had to make was:

Code: Select all

 7) #Mount drives
                 /bin/mount /BackupDrive
                        exit $?
        ;;
    8) #Unmount the drives
                /bin/umount /BackupDrive
                exit $?
The rest of the file required no editing. Right? So, The only other thing is whether I'll need to edit /etc/fstab/ so BIT can mount target drive as "user" instead of requiring sudo "root" privilege AND MAYBE, I'll need to define a mount point in fstab too. Hmmm.

I'll go ahead and give this a whirl.

Thank you again.

EDIT:
Wow.... I was over complicating it. I did the edit to "user-callback as per your sample and then only had to make the above mentioned changes to "fstab" and it works.
When I manually run BIT it automatically mounts target drive. Next is to see if:
a) when anacron runs BIT, the drive will mount? AND
b) whether the drive will umount automatically when the backup is finished.

Thanks momist,
Will let you know on a) and b)

Hello Momist,

Everything works as expected now. Including a) and b)
:D

Take care!
Locked

Return to “Storage”