Changelog
- 2013-06-18 Adding change rbackup user’s login shell section.
- 2013-06-14 A few steps updated, thanks in part to contributors. Notably, the
rbackup
user doesn’t necessarily need to be created on the backup server (uno) as described originally. - 2011-01-07 Initial post.
Introduction
The Mission:
To rsync and/or rsnapshot both normal and protected/restricted files from one server to another over ssh without enabling remote root access to either server while maintaining original file attributes and permissions. Whew.
Cast of Characters
- uno: the server which will store the backups, run rsync or rsnapshot.
- zero: the server to be backed up, with root readable files (/etc for example).
Prerequisites
- root on both uno and zero, hopefully via sudo and not by remote root ssh access!
Configuration
The command examples here are specific to Debian and Ubuntu, so adjust to your distribution accordingly (though little if any adjustment should be necessary).
Server: uno (backup server)
Become root, create an ssh key pair:
1
|
|
When naming the key, don’t accept the default. Instead, enter
1
|
|
We’re naming the key pair with ‘rbackup’ in it to keep track of what the keys are for.
As root, create a ssh config file:
1
|
|
In root’s .ssh/config file:
1 2 3 4 5 |
|
Save the file, set permissions:
1
|
|
While still root, copy the public key id-rbackup_rsa.pub
somewhere publicly
accessible, such as your regular user’s home directory.
As your regular user (which already has ssh access to zero), send this public key from uno to zero:
1
|
|
Server: zero (server to be backed up)
Create new backup user, lets call it rbackup
:
1
|
|
Make uno’s public key id-rbackup_rsa.pub
available to user rbackup.
Login as rbackup, create a .ssh directory, set permissions, create an
authorized_keys
file:
1 2 3 4 5 6 7 |
|
Now we want to limit the use of this authorized key by allowing connections only from uno, and allowing one command only. Edit the key, and add something like this to the beginning of the key:
1 2 |
|
While still user rbackup, create a script named validate_rsync.sh
in your
home directory:
1 2 |
|
Contents of validate_rsync.sh
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Make sure that validate_rsync.sh
is executable by user (and group) rbackup:
1
|
|
As your regular user (which can sudo):
1
|
|
Add this line to the bottom:
1
|
|
For a little added security, set the rbackup user’s login shell to the validate_rsync.sh
script:
1
|
|
When prompted to choose the login shell, enter /home/rbackup/validate_rsync.sh
.
If you have the AllowUsers
directive set for sshd in /etc/ssh/sshd_config
,
make sure to add the user rbackup
to the list, and restart sshd.
As root or with sudo, create a simple rsync wrapper, named rsync_wrapper.sh
at /usr/local/bin,
containing:
1 2 |
|
Testing
Test 1
Become user rbackup on uno, attempt ssh to zero:
1
|
|
Expected response:
1 2 |
|
The “Connection closed.” with the period at the end tells us the
validate_rsync.sh
worked as expected (echoing the last Connection closed).
Test 2
Become root on uno, attempt to ssh to zero-rsync (the alias set in root’s .ssh/config):
1
|
|
Expected response:
1 2 |
|
Test 3
Become root on uno, attempt to rsync something on zero that is restricted:
1 2 |
|
Expected response: you should have a copy of zero’s /etc directory in regular users tmp directory (or wherever). Important things to note in the above command - the –rsync-path switch and using zero-rsync as the host instead of just zero.
Using rsnapshot
If the above tests all work, setting up rsnapshot is easy. Check any other guide for general setup info, the relevant stuff for us to use in our rsnapshot.conf is:
1 2 |
|
Remember the rsnapshot.conf file needs tabs. The rsync_long_args
setting is
rsnapshot’s default rsync arguments, with our --rsync-path=rsync_wrapper.sh
added. The backup
command has our backup user (rbackup) and the host alias
set in root@uno’s .ssh/config.
References
This was pieced together from various notes, blogs, and articles. Some script source and extra-helpful information was found at: