كيف يمكنني استخدام Find للنسخ الاحتياطي بشكل متكرر مستودعات تخريب متعددة
سؤال
في الوقت الحالي ، يدير البرنامج النصي النسخ الاحتياطي بشكل صريح svnadmin hotcopy على كل من مستودعاتنا كل ليلة. يتم تخزين جميع عمليات إعادة التجهيز لدينا تحت دليل الوالدين (/usr/local/svn/repos) البرنامج النصي الاحتياطي لدينا خط لكل من repos تحت هذا الدليل على غرار: svnadmin hotcopy/usr/local/svn/repos/myrepo1 /usr/local/backup/myrepo1
بدلاً من الاضطرار إلى إضافة سطر جديد يدويًا لكل ريبو جديد نحضره عبر الإنترنت ، كنت آمل في استخدام أمر Find لتشغيل svnadmin hotcopy لكل دليل يجده تحت/usr/local/svn/repos.
حتى الآن لدي:
find /usr/local/svn/repos/ -maxdepth 1 -mindepth 1 -type d -exec echo /usr/local/backup{} \;
، حيث أقوم باستبدال "svnadmin hotcopy" مع "Echo" من أجل البساطة. إخراجها هو:
/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
يتم تضمين المشكلة كونها المسار الكامل في {}. أحتاج فقط العنصر الأخير من اسم الدليل إلى -exec
الإخراج الذي أريد أن أكون:
/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
أنا عالق إلى حد كبير في هذه المرحلة. هل يمكن لأي شخص مساعدتي هنا؟ شكرا مقدما ، ديف
المحلول
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.
Just remove the echo
and make any adjustments you might need and it should do the trick.
نصائح أخرى
put a cut command at the end
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 '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.
ls -al
lists all the files in the named directory with attributes
grep '^d'
selects the lines beginning with d which are directories
sed
strips off all the characters to the left of the actual directory name. You may need to add or delete some dots
xargs
takes the list of directory names and issues it one at a time. I specified zzyggy as the name to substitute in the executed command, but you can choose what you like. Of course, you would replace echo with your svnadmin command.
If it was in a shell script you should really do this
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.