Contact Sales Sitemap Customer Login

Automate Backups with SSHFS

The following guide (adapted with permission from'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

  1. 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)
  2. Enter cat ~/.ssh/ to view the contents of the new public key file created by ssh-keygen

Remote server: Add the new Local server SSH key

  1. Log in with the user account that the Local server will authenticate with
  2. Enter echo "<LOCAL_SERVER_PUBLIC_KEY>" >> ~/.ssh/authorized_keys (where <LOCAL_SERVER_PUBLIC_KEY> is the key text copied from ~/.ssh/ on the Local server)
  3. Enter chmod 640 ~/.ssh/authorized_keys to lock down your new configuration

Test Configuration

  1. 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

SSHFS Installation

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

  1. Remote Server: Enter mkdir /sshfs-destination to create a directory which will store files from the Local Server
  2. Local Server: Enter mkdir /sshfs-pointer to create a directory which will act as a pointer to the Remote Server via sshfs
  3. 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

Test Configuration

  1. Local Server: Enter mount /sshfs-pointer to enable access to the Remote Server's filesystem
  2. Local Server: Enter echo "Testing" >> /sshfs-pointer/file.txt
  3. 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
  4. 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.

Related Topics

Retrieved from ""
Recent Changes | RSS RSS Feed