tar via SSH

How to backup files over ssh ?

A nice way of backuping files from one machine to an other is a zipped tar file transferred via ssh.

# tar zcvf - /var/www | ssh root@backupserver "cat /backup/www-backup.tar.gz"

Your output will be something like:

tar: Removing leading `/' from member names
/var/www/
/var/www/index.html
/var/www/css
...
...
...
Password: <enter your backup server password>

Instead of “cat” you can use “dd” instead:

tar cvzf - /var/www| ssh backup@backupserver "dd of=/backup/www-data.tar.gz"

To restore  your tar file:

change to your restore folder and execute:

# ssh backup@backupserver "cat /backup/20110305|-www-data.tgz" | tar zxvf -

For continous backup you can use my sample script:

#!/bin/bash
#
# to automate that script please prepeare a ssh key authentication
#

# declaire variables
BCKPSOU=$1
BCKPNAM=$2
BCKPUSR="backup"
BCKPSRV="192.168.27.200"
BCKPTAR="/home/backup"
DATE=$(date "+%Y%m%d-%H%M")
# start backup
tar zcvf - $BCKPSOU | ssh $BCKPUSR@$BCKPSRV "cat > $BCKPTAR/$DATE-$BCKPNAM.tgz"

To use that script just copy it into an file called backup.sh

chmod 755 backup.sh
# ./backup <What to safe> <name on backup server>
./backup /var/www www-data

If zou want to untar over the network just use:

ssh root@backupserver “cat file.tar” | tar -zxvf -

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *