First of all, I'm old(er), I'm old-school, and I just love using Linux, terminal and generally being in control of the box and my data. So there might be tons fantastic, AI-powers, LLM engine, ketamine infused, smarter backup solutions out there. But using this kind of setup has saved my a.. many times.
Part of this tutorial was posted also on https://ftpgrid.com/tutorials/sftp-backup-script-with-ssh-keys.
In this tutorial I'll show you how to create a fast and easy file backup solution on Linux, UNIX, or even Mac OS, using just ftpGrid and some super simple terminal commands. We don't manage stuff like retention or partial backup in this script, event though it could easily be extended to have such features. I'll go through:
- Creating a secure SSH key for connecting with the SFTP edge service.
- Setting up a new account to use the key
- Testing your SFTP connection
- Writing a script to zip and upload a directory
In this tutorial I assume a certain knowledge of using a basic terminal, and if a command is missing being able to perform an apt install missing-command command.
Generate an ssh-ed25519 Key Pair
SSH keys are the modern, secure way to authenticate with servers – especially in automated scripts. I would always recommend against using password when possible.
Open your terminal and run:
ssh-keygen -t ed25519 -C "your_email@example.com"
- Press Enter to accept the default file location (~/.ssh/id_ed25519)
- Optionally add a passphrase (leave blank for non-interactive scripts)
This gives you:
- ~/.ssh/id_ed25519 → your private key (keep it safe!)
- ~/.ssh/id_ed25519.pub → your public key
Create Your ftpGrid Account and Upload Your Public Key
- Sign up or log in to https://app.ftpgrid.com/auth/login
- Go to your account dashboard at https://app.ftpgrid.com/app/ftpaccounts
- Create a new FTP Account. "FTP Account" is just our word for an edge service account and is used for both FTP and SFTP.
You can view the contents of you public key by running in bash:
cat ~/.ssh/id_ed25519.pub
- Enter a meaningful name like backup
- Copy paste the contents from ~/.ssh/id_ed25519.pub into the field "SSH Public key (usually id_rsa.pub)"
- Save the user
- Please note the full username required to login uses you account prefix.backup
Test the SFTP Connection in Your Terminal
Try connecting to the server manually to confirm everything is set up correctly, where edgeN is your assigned edge server, for instance edge7.ftpgrid.com:
sftp -i ~/.ssh/id_ed25519 prefix.backup@edgeN.ftpgrid.com
You should land in the remote shell without being asked for a password. Try running ls or put testfile.txt to test upload capability.
If you are prompted for a password, you might have forgotten the account prefix.
Write a Simple Bash Script for Automated Backups
Here’s a minimal bash script which creates our backup:
- Compresses a directory into a .zip file
- Adds today’s date to the filename
- Uploads the file to your ftpGrid account via SFTP
Make sure to replace:
- SRC_DIR with the directory you need to backup
- SFTP_USER with the user you created
- SFTP_HOST with your assigned edge service.
#!/bin/bash
# CONFIG
SRC_DIR="/path/to/your/data"
BACKUP_NAME="backup-$(date +%Y-%m-%d).zip"
TMP_PATH="/tmp/$BACKUP_NAME"
SFTP_USER="prefix.backup"
SFTP_HOST="edgeN.ftpgrid.com"
SSH_KEY="$HOME/.ssh/id_ed25519"
REMOTE_DIR="/backups"
# Compress the directory
zip -r "$TMP_PATH" "$SRC_DIR"
# Upload to ftpGrid
sftp -i "$SSH_KEY" "$SFTP_USER@$SFTP_HOST" << EOF
mkdir $REMOTE_DIR
cd $REMOTE_DIR
put "$TMP_PATH"
bye
EOF
# Optional: Remove local zip after upload
rm "$TMP_PATH"
Make the script executable and do a test run:
chmod +x backup-to-ftpgrid.sh
./backup-to-ftpgrid.sh
Then you can run it manually or add it to a daily cron job for automated backups.
Without going into too much detail, a cronjob running this backup daily at 4.00(AM) would look something like this:
0 4 * * * /path/to/backup-to-ftpgrid.sh > /dev/null 2>&1
Wrapping Up
With SSH keys and a few lines of bash, you’ve created a secure, automated backup workflow using SFTP. No more typing passwords. No more forgetting to upload your files. Just simple, reliable backups — every day.
Need help? Reach out to us using our contact page, or through our build in helpdesk, we’re happy to assist. If you haven't signed up yet you can do so here.