我在 AIX 中运行一个 shell 脚本,该脚本将每晚为每个 SVN 存储库执行备份。执行如下:
下载每个存储库并保存为转储文件:
svnrdump on repo_A
svnrdump on repo_B
svnrdump on repo_X ## X is the subsequent number of repository
下载过程完成后,内容将被压缩:
bzip2 --compress repo_A_yyyymmdd.dump
bzip2 --compress repo_B_yyyymmdd.dump
bzip2 --compress repo_X_yyyymmdd.dump ## X is the subsequent number of backup copy
然后我将对备份副本进行整理,删除那些距今超过 5 天的副本(我只需要保留最近的 5 个备份副本):
rm `ls -t repo_A_????????.dump.bz2 | tail -n +6`
rm `ls -t repo_B_????????.dump.bz2 | tail -n +6`
rm `ls -t repo_X_????????.dump.bz2 | tail -n +6` ## X is the subsequent number of backup copy
我的问题
有没有办法通过将它们repo name
放入数组然后循环执行每个存储库的每个命令来优化此代码?
答案1
或者:
repodir=/some/path
backupdir=/some/otherpath
maxagedays=6
for repo in repo_A repo_B repo_X; do
svnadmin dump $repodir/$repo | bzip2 -9 > $backupdir/$repo-`date +%F`.dump.bz2
done
find /$backupdir -type f -iname *dump.bz2 -mtime +$maxagedays -exec rm -f {} \;
答案2
尝试这样的操作:
第一种形式,后缀数组:
#!/bin/bash
array=(A B C D E F)
for i in "${array[@]}"
do
echo svnrdump on repo_$i
echo bzip2 --compress "repo_$i.dump"
done
第二种形式,如果 repo 名称更加多样化:
#!/bin/bash
while read REPO; do
echo svnrdump on "${REPO}"
echo bzip2 --compress "${REPO}".dump
done << REPOLIST
repo0
repo1
repo2
repo3
REPOLIST
您必须将所有存储库添加到 REPOLIST。