Skip to main content

Simple automatic sftp backup to a remote server

- Create private / public key pair if you don't have one already.
-- **ssh-keygen**
-- Because you'll be using this in a script, you do not want to enter a passphrase.

- Load your public key into authorized_keys file for the correct user on your target server.
-- **ssh-copy-id USERNAME@TARGET.EXAMPLE.COM**
-- Accept the host key if this is your first time connecting to this server.
-- Enter the password for the remote server when prompted.
-- If all goes well, you should see _Number of key(s) added: 1_

- If you didn't use the default name 'id_rsa' for your key pair add **-i ~/.ssh/KEYFILE** immediately after sftp in the following command.

- Save this command as a shell script.
-- **sftp USER@TARGET.EXAMPLE.COM:/ftp/docker/volumes/ <<< $'put -pR /var/lib/docker/volumes/***

- Set the correct permissions on the script file.
-- **chmod 755 SCRIPTFILE**

- Place the script in /etc/cron.daily OR weekly/monthly/whatever.

#### Sample script with notifications to the console and via ntfy

#!/bin/bash

##Full volume backup with completion status notifications via ntfy

#perform backup
sftp USERNAME@TARGET.EXAMPLE.COM:/ftp/docker/volumes/ <<< $'put -pR /var/lib/docker/volumes/*'


#failure notifications
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    curl -d "Docker Volume Backup failed with code [$EXITVALUE]." https://ntfy.EXAMPLE.COM/Backups
    echo "Backup failed with exit code [$EXITVALUE]"
    exit $EXITVALUE
fi


#success notifications
curl -d "Docker Volume Backup was successful." https://ntfy.EXAMPLE.COM/Backups
echo "Backup was successful"


exit $EXITVALUE