使用RedHat派生发行版(CentOS),我想保留常规用户(UID超过500),组(和影子文件)的列表推送到备份服务器。
从主服务器到备份服务器的同步只是单向的。
我真的不想处理LDAP或NIS。
我所需要的只是一个简单的脚本,可以每晚运行以保持备份服务器的更新。
主服务器可以通过SSH连接到备份系统。
任何build议?
编辑:
感谢迄今为止的build议,但我觉得我没有把自己弄清楚。
我只是在同步UID在500以上的普通用户。
系统/服务用户(UID低于500)在两个系统上可能不同。
所以你不能只同步整个文件。
您可以使用awk来提取ID为500或更大的用户/组。 我也冒着排除用户ID 65534的自由,这个用户ID通常是为“nobody”用户保留的(取决于发行版;如果CentOS是这样的话,不会提供线索):
awk -F: '($3>=500) && ($3!=65534)' /etc/passwd > passwd.new awk -F: '($3>=500) && ($3!=65534)' /etc/group > group.new awk -F: '($3>=500) && ($3!=65534) {print $1}' /etc/passwd | grep -f - /etc/shadow > shadow.new
然后使用rsync,scp或您select的文件传输方法将文件复制到您的备份系统。 这些文件然后可以被添加到一个'干净'passwd,组或阴影文件的末尾,当你需要恢复它们(即:仅默认系统用户/组,以防止无意识的ID /用户名重复)。
cat passwd.new >> /etc/passwd cat group.new >> /etc/group cat shadow.new >> /etc/shadow
NIS / NIS +是由于这个确切原因而发明的。
但是,如果你能做到这一点,那么它们就是丑陋而集中(LDAP / Kerberos / SMB /等等)authentication是一个好得多的想法。 要设置NIS / NIS +,您将需要:
包:
yp-tools ypbind ypserv portmap
和一个类似于/etc/yp.conf的东西:
domain example.org server nis.example.org ypserver nis.example.org
然后在/ etc / sysconfig / network中:
NISDOMAIN=example.org
而且我很懒,这里有一个很好的方法: http ://www.wains.be/index.php/2007/02/28/setting-up-nis-under-centos-4/会引导你。
个人来说,备份我只是备份整个/ etc /目录,并完成它。 最多只有几个megs。
使用cppw和cpgr:
CPPW(8)
名称
cppw,cpgr – 将给定文件locking到密码或组文件
概要
cppw [-h] [-s]密码文件cpgr [-h] [-s] group_file
描述
cppw和cpgr会将locking的指定文件分别复制到/ etc / passwd和/ etc / group中。 使用-s标志,他们将分别复制这些文件的影子版本/ etc / shadow和/ etc / gshadow。 使用-h标志,这些命令将显示一条简短的帮助消息并以无提示的方式退出。
也可以看看
vipw(8),vigr(8),group(5),passwd(5),shadow(5),gshadow(5)
作者
cppw和cpgr是由Stephen Frost根据Guy Maor写的vipw和vigr写的。
这里有很多方法和解决方法,但回答原来的问题有三个步骤:
在服务器上创build一个无密码的SSH密钥:
ssh-keygen -b 4096
将.ssh / id_rsa.pub复制到客户端上的.ssh / authorized__keys2:
scp ~/.ssh/id_rsa.pub client:.ssh/authorized_keys2
添加这样的东西到你的/ etc / crontab(或用crontab -e编辑):
0 0 * * * scp /etc/{passwd,shadow,group} root@backupbox:/var/mybackupdir
那么,我认为现在有一些东西可以使用,而不需要自己的解决scheme,但是我必须做一些快速的事情。
下面是一个脚本,将做我所需要的。
为了使其工作,只需将最小和最大UID的几个configurationvariables更改为普通用户和远程主机名或IP地址即可。
您必须设置远程服务器才能接受来自本地服务器root用户的传入SSH会话,而不必input密码。
Keen指挥官暗示了他在这个页面上的回答是如何完成的,但是你也可以参考无密码的SSHlogin了解详细的指导。
脚本的function是将每个远程密码 , 组 , 影子 , gshadow文件从远程服务器复制到lcoal服务器上的临时位置。
然后从所有“普通”用户中去除这些临时文件,只保留对系统用户的引用。
下一步是通过passwd , group , shadow , gshadow的每个本地版本,并将“正常”用户附加到相应的临时文件,然后将其上传到远程服务器以replace旧的。
在您尝试任何操作之前,请确保您在本地和远程服务器上都复制了passwd , group , shadow , gshadow 。
文件所有权和属性被保留。
临时文件保存在/tmp并被删除,无论同步是否成功。
本地服务器必须对备份具有无密码的root访问权限(但不能以其他方式)。 这是必要的,所以我们可以得到用户帐户configuration文件(否则受到限制)。
这是第一次尝试,它有点杂乱(不漂亮的代码),但它做得很好,其他人可能会觉得它有用。
这是一个Perl脚本,它只依赖Net::SCP模块在服务器之间安全地复制文件。
#!/usr/bin/perl -w use Net::SCP qw(scp); use strict; use constant TRUE => (1==1); use constant FALSE => (1==0); #-------------------------------------------------------- # Configuration # Modify as needed #-------------------------------------------------------- my $remoteHost = '10.13.113.2'; # email backup server my $minUID = 500; my $maxUID = 30000; my $minGID = 500; my $maxGID = 30000; #-------------------------------------------------------- # Internal variables, normally not to be modified. #-------------------------------------------------------- my $systemConfigDir = '/etc'; my $tmpDir = $ENV{TMPDIR} || $ENV{TMP} || $ENV{TEMP} || '/tmp'; #-------------------------------------------------------- # Main #-------------------------------------------------------- # STEP 1 # Get the remote files to /tmp and # clean them of their normal users ProcessFiles('remote'); # STEP 2 # Append the local normal users to the temp files # and then send them back to the remote ProcessFiles('local'); #-------------------------------------------------------- # ProcessFiles sub does one of two things: # - if the passed argument is 'remote', then fetch each # user account file from the remote server, then remove # all normal users from each file, only keeping the # system users. # - if the passed argument is 'local', then appends all # normal local users to the previously fetched and # cleaned-up files, then copies them back to the remote. #-------------------------------------------------------- sub ProcessFiles { my $which = shift; my $tmpfile; my %username = (); my %usergroup = (); my %userUID = (); my %userGID = (); my @info; foreach my $f ('passwd','group','shadow','gshadow') { my $tmpfile = "$tmpDir/$f.REMOTE"; if ($which eq 'remote') { # Fetch the remote file unlink $tmpfile if -e $tmpfile; scp("$remoteHost:$systemConfigDir/$f", $tmpfile) or die ("Could not get '$f' from '$remoteHost'"); } # Glob the file content open CONFIGFILE, (($which eq 'remote') ? $tmpfile : "$systemConfigDir/$f"); my @lines = <CONFIGFILE>; close CONFIGFILE; # Open the temp file, either truncating it or in append mode open TMPFILE, (($which eq 'remote') ? ">$tmpfile" : ">>$tmpfile" ) or die "Could not open '$tmpfile' for processing"; foreach my $line (@lines) { # Skip comments, although they should be illegal in these files next if $f =~ /^\s*#/; @info = (split ':', $line); if ($f eq 'passwd') { my $uid = $info[2]; my $isnormaluser = ($uid > $minUID) && ($uid < $maxUID); next if (($which eq 'remote') ? $isnormaluser : !$isnormaluser); $username{$info[0]} = TRUE; $userUID{$uid} = TRUE; $userGID{$info[3]} = TRUE; } elsif ($f eq 'group') { my $gid = $info[2]; my $isnormalgroup = ($gid > $minGID) && ($gid < $maxGID); next if (($which eq 'remote') ? $isnormalgroup : !$isnormalgroup); $usergroup{$info[0]} = TRUE; } elsif ($f eq 'shadow') { next if !exists $username{$info[0]}; } else { next if !exists $usergroup{$info[0]}; } # Any line that reaches this point is valid print TMPFILE $line; } close TMPFILE; if ($which eq 'local') { # send the file back scp($tmpfile, "$remoteHost:$systemConfigDir/$f") or die ("Could not send '$f' to '$remoteHost'"); unlink $tmpfile; } } } #-------------------------------------------------------- # Make sure we cleanup the temp files when we exit #-------------------------------------------------------- END { my $tmpfile; foreach my $f ('passwd','group','shadow','gshadow') { $tmpfile = "$tmpDir/$f.REMOTE"; unlink $tmpfile if -e $tmpfile; } }
更新21MAY2010:更新代码以改进组ID的同步
我使用crontab条目中的rsync来做一个简单的备份来完成同样的事情。 不幸的是,我不通过SSH。 我的crontab条目如下所示:
0 4 * * 0 rsync -av –delete / etc / / backup / etc /
其实我的crontab启动我的NAS服务器,即上面列出的第二个path,通过唤醒LAN,然后做了几个备份,这只是其中之一。 然后Cron给我发一封电子邮件,让我知道备份的内容,即哪些文件是同步的。
我还没有考虑通过ssh做这个,但我希望这有助于。
您只需要部分同步就意味着脚本必须更加复杂,因此更有可能出现某种types的错误。 就个人而言,我会花一些时间,并调查多less努力,这将是简单修复这些其他帐户。 我不知道我们正在谈论多less服务,但我猜想,在更改调整服务帐户ID后,您所要做的就是更新某些文件的所有者。
如果你想做一些非常简单的事情,你可以像rdist ( 简介 )那样设置,只需将文件推送到其他服务器即可。 为了保证安全,您需要为rdist进程设置基于密钥的ssh访问权限。