![路径作为变量](https://linux22.com/image/526393/%E8%B7%AF%E5%BE%84%E4%BD%9C%E4%B8%BA%E5%8F%98%E9%87%8F.png)
BACKUP_SERVER_NAME="name"
BACKUP_SERVER_USERNAME="user"
COPY_FILE_TO_WINDOWS="/cygdrive/c/unix_test_copy"
scp -r /$1/ $PRIMARY_SERVER_USERNAME@$PRIMARY_SERVER_NAME:$COPY_FILE_TO_WINDOWS
我如何将路径传递给变量以及如何将其传递给命令并执行它?
这是我的 bash 文件,如果有人可以转换它:
PRIMARY_SERVER_NAME="error.com.jo"
PRIMARY_SERVER_USERNAME="Administrator"
BACKUP_SERVER_NAME="V1DEV1.com.jo"
BACKUP_SERVER_USERNAME="Administrator"
COPY_FILE_TO_WINDOWS="/cygdrive/c/unix_test_copy"
SCAN_BATCH_FILE_WINDOWS="c:\\scan.bat"
LOG_FILE="/var/log/scan.log"
if [ -f "$1" ]
then
scp -r $1 ${BACKUP_SERVER_USERNAME}@${BACKUP_SERVER_NAME}:${COPY_FILE_TO_WINDOWS}"
server_status=$?
server_status_on=0
if [$server_status -eq $server_status_on ];
then
ssh -l ${BACKUP_SERVER_USERNAME}@${PRIMARY_SERVER_NAME} 'cmd /c ${SCAN_BATCH_FILE_WINDOWS} {$COPY_FILE_TO_WINDOWS}\'$1'
else
scp -r $1 ${BACKUP_SERVER_USERNAME}@${BACKUP_SERVER_NAME}:${COPY_FILE_TO_WINDOWS}
ssh -l ${BACKUP_SERVER_USERNAME} ${BACKUP_SERVER_NAME} 'cmd /c ${SCAN_BATCH_FILE_WINDOWS}${COPY_FILE_TO_WINDOWS}\'$1''
fi
error_code=$?"
echo "[$(date +%c)] $1 error_code = $?" >> ${LOG_FILE}
exit $error_code"
else
echo "[$(date +%c)] $1 error_code = 5" >> ${LOG_FILE}
exit 5
fi
答案1
创建备份脚本.sh
#!/bin/bash
BACKUP_SERVER_NAME="name"
BACKUP_SERVER_USERNAME="user"
COPY_FILE_TO_WINDOWS="/cygdrive/c/unix_test_copy"
scp -r $1 ${BACKUP_SERVER_USERNAME}@${BACKUP_SERVER_NAME}:${COPY_FILE_TO_WINDOWS}
使可执行文件:
chmod +x backup_script.sh
跑步:
backup_script.sh /etc/
要调试,请更改
scp -r $1 ${BACKUP_SERVER_USERNAME}@${BACKUP_SERVER_NAME}:${COPY_FILE_TO_WINDOWS}
到
echo "scp -r $1 ${BACKUP_SERVER_USERNAME}@${BACKUP_SERVER_NAME}:${COPY_FILE_TO_WINDOWS}"
或者运行:
set -x