简体   繁体   English

如何使用FIND递归备份多个Subversion存储库

[英]How can I use FIND to recursively backup multiple subversion repositories

At the moment our backup script explicitly runs svnadmin hotcopy on each of our repositories every night. 目前,我们的备份脚本每天晚上在每个存储库上明确运行svnadmin hotcopy。 Our repos are all stored under a parent directory (/usr/local/svn/repos) Our backup script has a line for each of the repos under that directory along the lines of: svnadmin hotcopy /usr/local/svn/repos/myrepo1 /usr/local/backup/myrepo1 我们的存储库都存储在父目录(/ usr / local / svn / repos)下。备份脚本在该目录下的每个存储库都有一行,分别是:svnadmin hotcopy / usr / local / svn / repos / myrepo1 / usr /本地/备份/ myrepo1

Instead of having to manually add a new line for each every new repo we bring online, I was hoping to using the find command to run svnadmin hotcopy for every directory it finds under /usr/local/svn/repos. 我希望不必为每个联机的新仓库手动添加新行,而是希望使用find命令对在/ usr / local / svn / repos下找到的每个目录运行svnadmin hotcopy。

So far I've got: 到目前为止,我已经:

find /usr/local/svn/repos/ -maxdepth 1 -mindepth 1 -type d -exec echo /usr/local/backup{} \;

,where I'm substituting "svnadmin hotcopy" with "echo" for simplicity's sake. ,为简单起见,在这里用“ echo”替换“ svnadmin hotcopy”。 The output of which is: 输出为:

/usr/local/backup/usr/local/svn/repos/ure
/usr/local/backup/usr/local/svn/repos/cheetah
/usr/local/backup/usr/local/svn/repos/casemgt
/usr/local/backup/usr/local/svn/repos/royalliver
/usr/local/backup/usr/local/svn/repos/ure_andras
/usr/local/backup/usr/local/svn/repos/profserv
/usr/local/backup/usr/local/svn/repos/frontoffice
/usr/local/backup/usr/local/svn/repos/ure.orig
/usr/local/backup/usr/local/svn/repos/projectcommon
/usr/local/backup/usr/local/svn/repos/playground
/usr/local/backup/usr/local/svn/repos/casegen

The problem being the full path is included in {}. 问题是完整路径包含在{}中。 I need only the last element of the directory name passed to -exec 我只需要传递给-exec的目录名的最后一个元素

The output I want being: 我想要的输出是:

/usr/local/backup/ure
/usr/local/backup/cheetah
/usr/local/backup/casemgt
/usr/local/backup/royalliver
/usr/local/backup/ure_andras
/usr/local/backup/profserv
/usr/local/backup/frontoffice
/usr/local/backup/ure.orig
/usr/local/backup/projectcommon
/usr/local/backup/playground
/usr/local/backup/casegen

I'm pretty much stuck at this point. 在这一点上,我几乎陷入了困境。 Can anyone help me out here? 有人可以帮我从这里出去吗? Thanks in advance, Dave 预先感谢,戴夫

You were on the right track. 您在正确的轨道上。 Try this: 尝试这个:

find /usr/local/svn/repos/ -maxdepth 1 -mindepth 1 -type d -printf "%f\0" | xargs -0 -I{} echo svnadmin hotcopy /usr/local/svn/repos/\{\} /usr/local/backup/\{\}

The %f is like basename and the null plus the -0 on xargs ensures that names with spaces, etc., get passed through successfully. %f类似于basename并且xargs上的null加上-0可确保成功传递带有空格等的名称。

Just remove the echo and make any adjustments you might need and it should do the trick. 只需删除echo并进行您可能需要的任何调整,它就可以解决问题。

在最后放置剪切命令

find /usr/local/svn/repos/ -maxdepth 1 -mindepth 1 -type d -exec echo /usr/local/backup{} \| cut -f1,2,3,9 -d"/"

How about adding a sed filter cuting out the middle part? 如何添加一个sed过滤器来切除中间部分?

sed 's/usr.local.svn.repos.//g'

Added like this 像这样添加

find /usr/local/svn/repos/ -maxdepth 1 -mindepth 1 -type d -exec echo /usr/local/backup{} ";" | sed 's/usr.local.svn.repos.//g'
ls -al /usr/local/svn/repos/ |grep '^d' |sed s/^...............................................................//" |xargs -L 1 -I zzyggy echo /usr/local/svn/repos/zzyggy

It's a bit long but it does the trick. 它有点长,但是可以解决问题。 You don't have to do everything with find when there are lots of other shell commands, although if I had to write this kind of script, I would do it in Python and leave the shell for interactive work. 当还有许多其他Shell命令时,您不必对find进行任何操作,尽管如果我必须编写此类脚本,则可以使用Python进行编写,而将Shell留作交互式工作。

ls -al lists all the files in the named directory with attributes ls -al列出具有属性的命名目录中的所有文件

grep '^d' selects the lines beginning with d which are directories grep '^d'选择以d开头的目录行

sed strips off all the characters to the left of the actual directory name. sed删除实际目录名称左侧的所有字符。 You may need to add or delete some dots 您可能需要添加或删除一些点

xargs takes the list of directory names and issues it one at a time. xargs获取目录名称列表,并一次发布一次。 I specified zzyggy as the name to substitute in the executed command, but you can choose what you like. 我指定了zzyggy作为要在执行的命令中替换的名称,但是您可以选择自己喜欢的名称。 Of course, you would replace echo with your svnadmin command. 当然,您可以使用svnadmin命令替换echo。

If it was in a shell script you should really do this 如果是在shell脚本中,则应该这样做

SVNDIRNAME="/usr/local/svn/repos"
ls -al $SVNDIRNAME |grep '^d' |sed s/^...............................................................//" |xargs -L 1 -I zzyggy echo $SVNDIRNAME/zzyggy

but I decided to show the wrong and right way just to explain this point. 但是我决定以错误和正确的方式来解释这一点。 I'm going to tag this with some shell tag, but I still think that a Python script is a superior way to solve this kind of problem in the 21st century. 我将使用一些shell标记对其进行标记,但是我仍然认为Python脚本是解决21世纪这种问题的一种更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM