![다중 서버 rsync 백업 스크립트](https://linux55.com/image/73475/%EB%8B%A4%EC%A4%91%20%EC%84%9C%EB%B2%84%20rsync%20%EB%B0%B1%EC%97%85%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8.png)
rsync를 사용하여 SSH를 통해 원격 서버의 데이터를 백업하는 간단한 스크립트가 있습니다.
이에 대한 외부 구성 파일이 있습니다. 이 구성 파일에는 OPTIONS, REMOTE_IP, SOURCE 및 DESTINATION 변수가 있습니다.
이제 더 많은 원격 서버를 추가하고 여러 서버에 대해 단일 스크립트를 사용해야 합니다. 구성의 섹션(예: [SERVER_01], [SERVER_02]...)을 사용하여 정의하고 싶습니다.
스크립트:
# You can provide external configuration file if you specify it with -c option
# Then if you haven't specified it, use one from ~/rsync_script/config.cfg
if [[ $1 == -c ]]; then
CONFIG_FILE=$2
else
CONFIG_FILE=~/rsync_script/config.cfg
fi
# Add constants from config file to script's environment
if [[ -f $CONFIG_FILE ]]; then
. $CONFIG_FILE
fi
# Create full path before running rsync, because rsync cannot mkdir with -p option
# Run rsync with parameters from config.cfg and put files to $DESTINATION/$REMOTE_IP/YYYY-MM-DD
if [[ -d $DESTINATION ]]; then
mkdir -p $DESTINATION$REMOTE_IP/$(date +"%A")
rsync -avx \
--timeout=30 \
$OPTIONS \
rsync@$REMOTE_IP:$SOURCE $DESTINATION$REMOTE_IP/$(date +"%F")
else
echo "failure"
fi
구성:
# Set extra options for rsync command
OPTIONS="--itemize-changes --log-file=changes.log"
# Set IP address of server the you want to backup
REMOTE_IP="192.168.11.123"
# Set the folder on remote server to backup
SOURCE="/home/rsync/somedata"
# Set the destination folder on local machine
DESTINATION="/backup/"
이 문제를 해결하는 가장 좋은 방법을 제안해주세요.
모든 코드 의견과 제안을 환영합니다 :)
감사해요
답변1
이는 가능한 시나리오입니다. 기존 rsync 코드( if [[ -d $DESTINATION ...
)를 셸 함수(예: )에 넣고 runbackup
실행된 부분을 파일을 읽고 섹션 구분 기호를 . $CONFIG_FILE
찾는 루프로 바꿉니다. [SERVER_...]
하나를 찾으면 runbackup 함수를 호출합니다(첫 번째 함수 제외). 다른 행의 경우 eval
각 행에서 동일하게 작동합니다 .
. 마지막 섹션에서 runbackup이 호출되도록 하기 위해 [END]
더미 섹션이 입력에 추가됩니다.
(cat $CONFIG_FILE; echo '[END]') |
while read line
do if [[ "$line" =~ ^\[([A-Z_0-9]+)\] ]]
then if [ -n "$OPTIONS" -a -n "$REMOTE_IP" ]
then echo "section $section"
runbackup
fi
section=${BASH_REMATCH[1]} # captured from =~ regex above
unset OPTIONS REMOTE_IP SOURCE DESTINATION
else eval $line
fi
done