Backup to an external server

With tar and SSH, you can pipe backup data over the internet, eliminating the need to store it on the server temporarily.


When you back up your VM or dedicated server, you might want to store the backups on an external server rather than locally. This is especially relevant when dealing with large backups and you don’t have enough space to keep them on‑premises.

This command opens an SSH session to the external server backup.mydomain.com and streams the tar archive directly into the file /backup/fullbackup.tar.gz.

Command
tar zcvf - / | ssh [email protected] "cat > /backup/fullbackup.tar.gz"

In other words, you no longer need to store the backup on the local hard drive temporarily.

Automation

If you want to run this in a crontab using an SSH key with a password, you need to use an SSH agent to unlock the key. To start the agent and add your SSH key:

Commands
eval $(ssh-agent -a ~/.ssh/my-ssh-agent.sock)
export SSH_AUTH_SOCK=~/.ssh/my-ssh-agent.sock
ssh-add ~/.ssh/id_ed22519

Now you can run it in a crontab like this, for example, every 12th hour:

0 */12 * * * export SSH_AUTH_SOCK=~/.ssh/my-ssh-agent.sock && tar zcvf - / | ssh [email protected] "cat > /backup/fullbackup.tar.gz"

The other way around

If you want to run a backup from the external server, you can do it like this (this command is run from the external server):

ssh [email protected] 'tar zvcf - /' > /backup/fullbackup.tar.gz

Last updated

Was this helpful?