Using a RedHat-derivative distro (CentOS), I'd like to keep the list of regular users (UID over 500), and group (and shadow files) pushed to a backup server.
The sync is only one-way, from the main server to the backup server.
I don't really want to have to deal with LDAP or NIS.
All I need is a simple script that can be run nightly to keep the backup server updated.
The main server can SSH into the backup system.
Any suggestion?
Edit:
Thanks for the suggestions so far but I think I didn't make myself clear enough.
I'm only looking at synchronising normal users whose UID is on or above 500.
System/service users (with UID below 500) may be different on both system.
So you can't just sync the whole files I'm afraid.
You can use awk to extract users/groups with IDs of 500 or greater. I have also taken the liberty of excluding user id 65534, which is often reserved for the "nobody" user (depending on distro; no clue if CentOS does so):
Then use rsync, scp, or your file transmission method of choice to copy the files to your backup system. These files can then be appended to the end of a 'clean' passwd, group or shadow file when you need to restore them (ie: default system users/groups only, to prevent unintentional duplications of ID/username).
NIS/NIS+ were invented for this exact reason.
But they're kind of ugly and centralized (LDAP/Kerberos/SMB/etc.) authentication is a much much better idea if you can do it. To setup NIS/NIS+ you will need:
Packages:
and an /etc/yp.conf with something like:
and then in /etc/sysconfig/network:
And I got lazy, here's a good howto: http://www.wains.be/index.php/2007/02/28/setting-up-nis-under-centos-4/ that will walk you through it.
Personally for backup I'd just backup the entire /etc/ directory and be done with it. It's only a few megs at most.
use cppw and cpgr:
There are many ways and solutions here, but to answer the original question there are three steps:
Create a password-less SSH-key on the server:
ssh-keygen -b 4096
Copy .ssh/id_rsa.pub to .ssh/authorized__keys2 on the client:
scp ~/.ssh/id_rsa.pub client:.ssh/authorized_keys2
Add something like this to your /etc/crontab (or edit with crontab -e):
0 0 * * * scp /etc/{passwd,shadow,group} root@backupbox:/var/mybackupdir
Well, I thought there was something existing I could use without having to roll my own solution, but I had to do something quick.
Below is a script that will do just what I needed.
Instructions
For it to work, just change the few config variables for the minimum and maximum UID to be considered as a normal user and the remote host name or IP address.
You must have setup the remote server to accept incoming SSH sessions from the local server's
root
user without having to enter a password.Commander Keen hinted on how it's done in his answer on this page but you can also refer to password-less SSH login for detailed instructions.
How it works
What the script does is copy each of the remote passwd, group, shadow, gshadow files from the remote server to a temporary location on the lcoal server.
Then it strips these temp files from all "normal" users, keeping only the references to the system users.
The next step is going through each of the local versions of passwd, group, shadow, gshadow and appending just the "normal" users to their corresponding temp files, then uploading each of them back to the remote server to replace the old one.
Warning
Before you attempt anything, make sure you make a copy of your passwd, group, shadow, gshadow on both the local and remote servers.
Security
File ownership and attributes are preserved.
Temporary files are saved in
/tmp
and deleted, whether the sync was successful or not.The local server must have password-less
root
access to the backup (but not the other way around). This is necessary so we can get the user accounts config files (which are otherwise restricted).The Code
This is a first-attempt and it's a bit messy (not beautiful code) but it does the job pretty well and someone else may find it useful.
It's a Perl script that only has a dependency on the
Net::SCP
module to copy files securely between servers.Update 21MAY2010: updated code to improve sync of group ID
You requirement for just a partial sync means the script must be a lot more complex, and thus more likely to have some kind of bugs. Personally, I would take take some time and investigate how much of an effort it would be to simply fix those other accounts. I don't know how many services we are talking about, but I would guess, that all you would have to do after changing adjusting the IDs of the service accounts would be to update the owner of some files.
If you wanted to do something really simple you could setup like rdist (intro) to simply push the files to you want to the other server. To keep this secure, you would need to setup key-based ssh access for the rdist process to use.
I use rsync in a crontab entry to do a simple backup that accomplishes the same thing. Unfortunately I don't do it over ssh. My crontab entry looks like this:
0 4 * * 0 rsync -av --delete /etc/ /backup/etc/
Actually my crontab starts my NAS server, i.e. the second path you see listed above, via wake-on-lan and then does several backups with this just being one of them. Cron then sends me an email letting me know what was backed up, i.e. which files were synched, etc.
I've not looked into doing this over ssh but I hope this helps.