What is sshfs?
Sshfs is a fuse-based filesystem client used generally for mounting remote directories on local server using ssh connection.
If you have multiple websites that has lots of assets i.e. images, pdf, videos or some other files that you store on your web server it self then it is not considered as a good practise.
If you keep your assets on the same server and for some reason your server is down or can not recover you loose valuable assets.
Now a days developer prefers to use s3 bucket to store such assets. If you are not comfortable using s3 bucket and wondering how you could create your own bucket so that you can use just like s3 bucket then follow this tutorial.
How to create your own s3 like bucket?
Let's take a practical example, imagine that you are a developer or linux administrator and you need to maintain two website and their assets.
You purchased three linux servers A, B and C. Your server A and B used for your websites however you want your server c to act like s3 bucket where you want to store assets for your server A and B.
Mounting remote file system on server A
Let's login to server a and create a directory that actually points to server c file system:
# log on to server A
$ ssh user@server-a-ip
# install sshfs tool
$ sudo apt-get install sshfs
# go to your website root dir
$ cd /var/www
# make a directory called bucket
$ sudo mkdir bucket
# point your bucket folder to your server c file system
$ sudo sshfs -o allow_other,defer_permissions user@server-c-ip:/ /var/www/bucket
# or if you use ssh key to login to remote server C then run
$ sudo sshfs -o allow_other,defer_permissions,IdentityFile=~/.ssh/id_rsa user@server-c-ip:/ /var/www/bucket
$ exit
Mounting remote file system on server B
Let's login to server b and create a directory that actually points to server c file system:
# log on to server B
$ ssh user@server-b-ip
# install sshfs
$ sudo apt-get install sshfs
# go to your website root directory
$ cd /var/www
# make a directory called bucket
$ sudo mkdir bucket
# point your bucket folder to server c file system
$ sudo sshfs -o allow_other,defer_permissions user@server-c-ip:/ /var/www/bucket
# or if you use ssh key to login to remote server C then run
$ sudo sshfs -o allow_other,defer_permissions,IdentityFile=~/.ssh/id_rsa user@server-c-ip:/ /var/www/bucket
$ exit
You are now all set with your new s3 like bucket. What happen next? Each time you upload a file on server A and B to /var/www/bucket folder it will copy that file and put it on server C.
So basically, your server C will act as a s3 bucket and will contain files from both servers A and B.
Permanently Mounting the Remote File System
When you mount a file system it lasts until your server is not shutdown or restarted. You have to follow the mounting steps all over again if server restarts or rebooted.
In order to prevent this happening you can permanently mount your remote filesystem to your local server. In order to do this change log on to your both server A and B and make following changes:
# open config file
$ sudo nano /etc/fstab
# at the bottom of /etc/fstab add following line
sshfs#user@server-c-ip:/ /var/www/bucket
Save this changes and restart your server A and B. It should be noted that permanently mounting your VPS file system locally is a potential security risk.
If your local machine is compromised it allows for a direct route to your droplet. Therefore it is not recommended to setup permanent mounts on production servers.