目錄
- 前言
- 需求
- 寶塔面板
- 備份網站
- 備份資料庫
- mac端
- 創建工程檔案夾
- rua.py
- rua
- stdout
- plist
- Reference
前言
不信任一切云端平臺,把資料牢牢握在自己手中才是最安全的,
需求
使用騰訊云服務器上的寶塔面板定時備份網站和資料庫,然后定時將備份后的資料存到本地,
寶塔面板
備份網站
備份資料庫
mac端
創建工程檔案夾
rua.py
在python中使用scp將備份的網站檔案和資料庫檔案傳到本地,會將log資訊放進rua.log中,若失敗,則會出現持續幾秒的彈窗提示,
import os
import paramiko
import unicodedata
from scp import SCPClient
import logging
import time
logging.basicConfig(filename='/path/to/bk/rua.log',level=logging.INFO)
week = ["一", "二", "三", "四", "五", "六", "日"]
t = time.localtime()
tm = "%s年%s月%s日(周%s), %s:%s:%s" % (t[0], t[1], t[2], week[t[6]-1], t[3], t[4], t[5])
logging.info(tm)
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('騰訊云ip', 埠數字, '用戶名', '密碼')
scp = SCPClient(client.get_transport())
scp.get('/path/to/database', '/path/to/bk/database', recursive=True)
scp.get('/path/to/site', '/path/to/bk/site', recursive=True)
scp.close()
client.close()
except Exception as e:
os.system('osascript -e \'display notification "%s" with title "wordpress備份失敗!!!" subtitle "請前往【/path/to/bk】檢查" \'' % e)
logging.error(e)
else:
logging.info("success !!!")
rua
每次從這里呼叫rua.py,注意python指令不能直接用python,需要用其絕對路徑(可以用whereis python查看),
time=$(date "+%Y-%m-%d %H:%M:%S")
echo "$time" >> /path/to/bk/log.txt
/path/to/python /path/to/bk/rua.py
echo "finished" >> /path/to/bk/log.txt
stdout
創建一個標準輸出檔案,
touch stdout
plist
在/Library/LaunchDaemons/中創建com.backupwordpress.plist,指定每天13點14分開始運行rua程式,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Label唯一的標識 -->
<key>Label</key>
<string>com.backupwordpress</string>
<!-- 指定要運行的腳本 -->
<key>ProgramArguments</key>
<array>
<string>/path/to/bk/rua</string>
</array>
<!-- 指定運行的時間 -->
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>14</integer>
<key>Hour</key>
<integer>13</integer>
</dict>
<!-- 時間間隔(秒) -->
<!-- <key>StartInterval</key>
<integer>3</integer> -->
<key>StandardOutPath</key>
<!-- 標準輸出檔案 -->
<string>/path/to/bk/stdout</string>
<!-- 標準錯誤輸出檔案,錯誤日志 -->
<key>StandardErrorPath</key>
<string>/path/to/bk/error.txt</string>
</dict>
</plist>
運行:launchctl load -w /Library/LaunchDaemons/com.backupwordpress.plist
停止:launchctl unload -w /Library/LaunchDaemons/com.backupwordpress.plist
錯誤資訊會保存在error.txt中
Reference
https://blog.csdn.net/linwwwei/article/details/84682981
http://events.jianshu.io/p/4fbad2909a21
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/544816.html
標籤:Python
