1. 程式人生 > 其它 >伺服器升級替換檔案快速備份與回滾

伺服器升級替換檔案快速備份與回滾

伺服器升級替換檔案快速備份與回滾

由於升級回退問題把我坑了一下,ansible的copy backup固然好用,可是回退卻特別坑人,因為backup備份是有準確時間的,所以想到了以下方法,當然你可以寫playbook,希望讀者提出更好的建議

思路:
利用ansible

利用簡單指令碼

#指令碼內容展示
ybt root /etc/ansible/A #cat bak.sh 
#!/bin/bash
for i in `cat 3.txt`;do cp $i $i.bak2021_07_29; done
#for i in `cat 3.txt`; do  mv  $i{.bak2021_07_29,}; done

示例:

#展示升級檔案
ybt root /etc/ansible/A #cat 3.txt 
pro 
scf
scf1
備份:
#將升級文字COPY到遠端伺服器
ybt root /etc/ansible/A #ansible test -m copy -a 'src=3.txt dest=/root/cin/etc/'
#檢視3.txt是否存在
ybt root /etc/ansible/A #ansible test -m shell -a 'ls /root/cin/etc/'
192.168.23.137 | CHANGED | rc=0 >>
3.txt
pro
scf
scf1
ybt
192.168.23.138 | CHANGED | rc=0 >>
3.txt
pro
scf
scf1
ybt

#備份
#執行指令碼第一句,第二句註釋
#!/bin/bash
for i in `cat 3.txt`;do cp $i $i.bak2021_07_29; done
#for i in `cat 3.txt`; do  mv  $i{.bak2021_07_29,}; done

#示例:
ybt root /etc/ansible/A #ansible test -m script -a 'chdir=/root/cin/etc/ bak.sh'
#檢視是否備份成功
ybt root /etc/ansible/A #ansible test -m shell -a 'ls /root/cin/etc/'
192.168.23.137 | CHANGED | rc=0 >>
3.txt
pro
pro.bak2021_07_29
scf
scf1
scf1.bak2021_07_29
scf.bak2021_07_29
ybt
192.168.23.138 | CHANGED | rc=0 >>
3.txt
pro
pro.bak2021_07_29
scf
scf1
scf1.bak2021_07_29
scf.bak2021_07_29
ybt
回滾:
#執行指令碼第二句,第一句註釋
#!/bin/bash
#for i in `cat 3.txt`;do cp $i $i.bak2021_07_29; done
for i in `cat 3.txt`; do  mv  $i{.bak2021_07_29,}; done

#示例:
#執行指令碼
ybt root /etc/ansible/A #ansible test -m script -a 'chdir=/root/cin/etc/ bak.sh'
192.168.23.137 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.23.137 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.23.137 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}
192.168.23.138 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.23.138 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.23.138 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}
#檢視結果
ybt root /etc/ansible/A #ansible test -m shell -a 'ls /root/cin/etc/'
192.168.23.137 | CHANGED | rc=0 >>
3.txt
pro
scf
scf1
ybt
192.168.23.138 | CHANGED | rc=0 >>
3.txt
pro
scf
scf1
ybt

替換:

不在示例:直接用COPY模組即可