2014-09-29 61 views
0

我已經做了我的劇本很容易在bash - 這腳本應該將該文件與特定的新名稱從服務器1傳送到遠程服務器2bash腳本轉移到Python - 從服務器日誌複製到另一個遠程服務器

燦有人幫助完成並將所有腳本傳輸到Python。

#!/bin/bash 

path=/opt/log #Im in dir /opt/log 
fdate=$(date +%Y%m%d -d "-1 day") # yesterday date 
file=maillog-$fdate  # log from yesterday which will be transfer to remote server 
cp $path/$file /tmp/$HOSTNAME-$file # copy $file to /tmp with the specific name of $HOSTNAME + $File name 
gzip /tmp/$HOSTNAME-$file  # ZIP the file 

[email protected] # remote server 
rpath=/opt/log/maillog # remote path 

scp /tmp/$HOSTNAME-$file.gz $rserver:$rpath # copy the file to remote server to remote path 

rm /tmp/$HOSTNAME-$file.gz # clean the /tmp dir 
#Done 
+0

這樣的回答可以幫助你: http://stackoverflow.com/questions/250283/how-to-scp-in-python – jgritty 2014-09-29 17:59:54

+0

你好這是有幫助但最後一件事我需要寫而不是fdate = $(日期+%Y%m%d -d「-1天」)#昨天日期 - 這是fdate =昨天日期「格式的日期命令+%Y%m %d - 1天,怎麼可能在Python中創建這個? – hansus 2014-09-30 09:36:25

+0

試試這個答案http://stackoverflow.com/questions/441147/how-can-i-subtract-a-day-from-a-python-日期 – jgritty 2014-09-30 16:59:51

回答

0

有幾種解決方案。

一種選擇是異口同聲:

http://xmodulo.com/synchronize-files-between-two-servers.html

rsync的解決方案:

您需要ssh到服務器第一個(這應該是微不足道)。一旦進入你可以運行這個。

rsync -rtlzv --ignore-errors -e ssh . [email protected]:/path/to/directory 

來自:http://darcynorman.net/2009/01/07/how-i-move-stuff-between-servers-with-rsync/

的SCP解決方案:

其實,我覺得這是最優雅的方式,因爲你沒有在第一個SSH協議。 Somethibng這樣的:

nohup scp [email protected]:/the/answer/of/all [email protected]:/var/tmp/42 & 

來自:https://unix.stackexchange.com/questions/65116/does-a-scp-transfer-close-when-i-close-the-shell

相關問題