【KAWAKO】从mac上定时将腾讯云的数据备份到本地

前言

不信任一切云端平台,把数据牢牢握在自己手中才是最安全的。

需求

使用腾讯云服务器上的宝塔面板定时备份网站和数据库,然后定时将备份后的数据存到本地。

宝塔面板

备份网站

【KAWAKO】从mac上定时将腾讯云的数据备份到本地

备份数据库

【KAWAKO】从mac上定时将腾讯云的数据备份到本地

mac端

创建工程文件夹

【KAWAKO】从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

发表评论

相关文章