The following guide (adapted with permission from Dork-E.com's Back up a cPanel server via SSHFS) explains how to automate filesystem backups from one Linux server to another Linux server with the SSHFS FUSE implementation.
The server with files that must be backed up will be referred to as Local and the server which the files will be saved to will be referred to as Remote. This guide will assume that you have root-level privileges or sudo access on Local and user-level privileges (or better) on Remote.
SSH Key Pair Authentication
Local Server: Create an SSH Key Pair
- Log in and enter ssh-keygen at the bash prompt to create an SSH key pair
Note: Do not enter a password for this key (if you do, the password you enter will be required to authenticate and you will not be able to automate the backup process)
- Enter cat ~/.ssh/id_rsa.pub to view the contents of the new public key file created by ssh-keygen
Remote server: Add the new Local server SSH key
- Log in with the user account that the Local server will authenticate with
- Enter echo "<LOCAL_SERVER_PUBLIC_KEY>" >> ~/.ssh/authorized_keys (where <LOCAL_SERVER_PUBLIC_KEY> is the key text copied from ~/.ssh/id_rsa.pub on the Local server)
- Enter chmod 640 ~/.ssh/authorized_keys to lock down your new configuration
- Enter ssh <USER>@<REMOTE_SERVER> (where <USER> is the username configured with your SSH public key and <REMOTE_SERVER> is the domain or IP address of the Remote Server)
- If you are automatically authenticated (no password prompt displays) you have successfully configured SSH key pair authentication and may proceed to the next step
- If you encounter any errors or are prompted for a password, run ssh -v <USER>@<REMOTE_SERVER> to display verbose error messages and troubleshoot
Install and Configure SSHFS
An sshfs package exists for all major Linux distributions - you can use the package management utility for your distribution to install sshfs:
Configure fstab to use SSHFS
- Remote Server: Enter mkdir /sshfs-destination to create a directory which will store files from the Local Server
- Local Server: Enter mkdir /sshfs-pointer to create a directory which will act as a pointer to the Remote Server via sshfs
- Local Server: Enter echo "sshfs#user@remote_server:/sshfs-destination /sshfs-pointer fuse noauto,compression=yes,nonempty" >> /etc/fstab to associate the /sshfs-pointer directory on the Local Server with the /sshfs-destination directory on the Remote Server
- Local Server: Enter mount /sshfs-pointer to enable access to the Remote Server's filesystem
- Local Server: Enter echo "Testing" >> /sshfs-pointer/file.txt
- Remote Server: Enter cat /sshfs-destination/file.txt
- If the word "Testing" displays when you read the contents of the /sshfs-destination/file.txt file on the Remote Server, your setup was successful and you can proceed to the next section to automate backups
- If you encounter any errors, review the error message to determine which step(s) need to be revisited
- Local Server: Enter unmount /sshfs-pointer to free up the connection to the Remote Server
Note: You will need to use mount/unmount before using the Remote Server's filesystem
Automate Backup Creation
You can select from a variety of backup strategies once you have a working SSHFS filesystem. Use cron to automate backup tasks.
To ensure the integrity of your backups under the widest range of situations, avoid writing over existing backup files on the Remote Server and, instead, create a new backup file (or directory) every time the backup process runs.