Introduction
Before using BorgBackup I used Autofs for mounting the Hetzner Storage Box automatically and simply rsyncing data there and back. Even with the possibility of encrypting CIFS traffic with SMBv3, I am still used to sshfs, and am lazy enough to automate my mounts.
This tutorial will show you how to set up automatic mounting of a storage box. We will set up a folder /srv and its automatically mounted subfolder /srv/storagebox. The latter folder will be automatically mounted as soon as it is accessed and will be automatically unmounted after a while.
We will mount using sshfs via fuse. This is a special kind of mount which basically translates filesystem access over a ssh connection.
Prerequisites
Besides having a Linux server and a Hetzner Storage Box, you will need to set up SSH keys for the storage box.
This tutorial is based on Debian/Ubuntu. It works the same on SUSE or RedHat based distros, though the package names could differ.
Step 1 - Install packages
Install necessary packages. Use sudo or root.
$ apt install fuse sshfs autofs
Step 2 - Prepare folders
$ mkdir /srv
We only create the base folder, NOT the folder that gets mounted automatically.
Step 3 - Setup autofs
Step 3.1 - Main configuration
The defaults are usually fine, there are three basic settings in /etc/autofs.conf
:
master_map_name = etc/auto.master
The main config file which defines all maps (a map is basically one automount directory).
timeout = 300
Number of seconds of inactivity on the folder before it gets unmounted.
browse_mode = no
Disabling browse_mode hides the mount folders. E.g. if you do a ls /srv
you won't see the folder storagebox/
. After doing ls /srv/storagebox
, it appears. Enabling browse mode will have an impact on performance of ls
.
Please note that on RedHat based distros some of these settings are abstracted and defined in /etc/sysconfig/autofs
!
Step 3.2 - auto.master file
Even though you can place mappings in /etc/auto.master.d
I personally prefer adding the entries directly to this file.
So simply add the following line before the last line +auto.master
/srv /etc/auto.storagebox
This means that the map in /etc/auto.storagebox
will be mounted in /srv
.
Step 3.3 - auto.storagebox file
The file /etc/auto.storagebox
contains the folder to be mounted (relative to the base folder) and the mount options.
storagebox -fstype=fuse,rw,nodev,nonempty,noatime,allow_other,max_read=65536,IdentityFile=/root/.ssh/id_rsa_storagebox :sshfs\#iholu@holu.your-backup.de\:
The complete line explained:
- storagebox
- The name of the folder where the storage box is mounted (/srv/storagebox)
- -fstype=fuse
- User fuse as fstype, following are the mount options
- rw
- ReadWrite access as opposed to ro, ReadOnly
- nodev
- No special devices (e.g. /proc) can be seen on this mount. For security reasons, though probably not neccessary on the storagebox.
- nonempty
- Mount can even work if the target folder is not empty
- noatime
- sshfs does not handle atime (time of last access of a file) properly
- allow_other
- All users, not just root, can use the mounted folder
- max_read=65536
- Maximum size of a read request.
- IdentityFile=/root/.ssh/id_rsa_storagebox
- The ssh file used to access the storage box (See prerequisites)
Step 4 - Start autofs
$ systemctl start autofs
Step 5 - Test autofs
Simply ls
the folder or cd
to it
$ ls /srv/storagebox
$ cd /srv/storagebox
You should now see the contents of the storage box.
Conclusion
By following this tutorial you have now set up automatic mounting of a Hetzner Storage Box.