Last active
January 28, 2018 22:59
-
-
Save nrtkbb/801e0b0a3564bda957eee5da57b536e3 to your computer and use it in GitHub Desktop.
raspberry pi の本体SDカードをNASにクローンして7zでアーカイブする。結果はslackで通知。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# dependency: | |
# $ sudo apt-get install p7zip-full | |
# $ sudo apt-get install python-dev python-pip | |
# $ sudo pip instal requests | |
# $ sudo python pi_clone.py | |
import os | |
import subprocess | |
import time | |
import requests | |
import datetime | |
SLACK_URL = 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX' | |
ME = u'pi' | |
ICON = u':one:' | |
CLONE_PATH = u'/mnt/nas/{}_backup/'.format(ME) | |
# backup_delete start | |
MOST_DAYS = 3 | |
def before_datetime(days): | |
# type: (int) -> datetime | |
today = datetime.date.today() | |
most_days = datetime.timedelta(days=days) | |
judge_day = today - most_days | |
t = datetime.time(0, 0, 0) | |
judge_datetime = datetime.datetime.combine(judge_day, t) | |
return judge_datetime | |
def isMostRecent(img_datetime): | |
# type: (datetime.datetime) -> bool | |
# True with in 3 days | |
return img_datetime > before_datetime(MOST_DAYS) | |
def isWeeklyOr1st(img_datetime): | |
# type: (datetime.datetime) -> bool | |
# True with in 1 months | |
if img_datetime < before_datetime(31): | |
return False | |
# monday is True | |
if 0 == img_datetime.weekday(): | |
return True | |
# or 1st day is True | |
return 1 == img_datetime.day | |
def is1st(img_datetime): | |
# type: (datetime.datetime) -> bool | |
return 1 == img_datetime.day | |
def backup_delete(base_dir): | |
# type: (str) -> List[str] | |
imgs = os.listdir(base_dir) | |
result = [] | |
for img_path in imgs: | |
path = os.path.join(base_dir, img_path) | |
mtime = os.stat(path).st_mtime | |
img_datetime = datetime.datetime.fromtimestamp(mtime) | |
if isMostRecent(img_datetime): | |
result.append('most recent {}'.format(path)) | |
continue | |
if isWeeklyOr1st(img_datetime): | |
result.append('weekly or 1st {}'.format(path)) | |
continue | |
if is1st(img_datetime): | |
result.append('month 1st {}'.format(path)) | |
continue | |
result.append('delete {}'.format(path)) | |
# os.remove(path) | |
return result | |
# backup_delete end | |
def apache(arg): | |
# type: (str) -> None | |
cmd = ['sudo', | |
'/etc/init.d/apache2', | |
arg, | |
] | |
p = subprocess.Popen(cmd) | |
p.wait() | |
def clone(out_file): | |
# type: (str) -> None | |
cmd = ['sudo', | |
'dd', | |
'if=/dev/mmcblk0', | |
'bs=1M', | |
'of={}'.format(out_file), | |
] | |
p = subprocess.Popen(cmd) | |
p.wait() | |
def archive(img, path_7z): | |
# type: (str) -> str | |
cmd = ['7z', | |
'a', | |
path_7z, | |
img | |
] | |
p = subprocess.Popen(cmd) | |
p.wait() | |
return path_7z | |
def slack(message): | |
# type: (unicode) -> None | |
data = json.dumps({ | |
'text': message, | |
'username': ME, | |
'icon_emoji': ICON, | |
'link_names': 1}) | |
r = requests.post(SLACK_URL, data) | |
print(r.status_code) | |
print(r.text) | |
def ondo(): | |
# type: () -> str | |
cmd = ['cat', | |
'/sys/class/thermal/thermal_zone0/temp' | |
] | |
result = subprocess.check_output(cmd) | |
do = int(result) / 1000.0 | |
return str(do) + "'C" | |
def main(): | |
# type: () -> None | |
try: | |
today = time.strftime("%Y%m%d") | |
img = '{}{}.img'.format(CLONE_PATH, today) | |
if os.path.exists(img): | |
slack('img file is exists done.\n{}'.format(img)) | |
return | |
path_7z = '{}{}.7z'.format(CLONE_PATH, today) | |
if os.path.exists(path_7z): | |
slack('7z file is exists done.\n{}'.format(path_7z)) | |
return | |
before_ondo = ondo() | |
apache('stop') | |
clone(img) | |
apache('start') | |
if not os.path.exists(img): | |
return | |
archive(img, path_7z) | |
if not os.path.exists(path_7z): | |
return | |
os.remove(img) | |
deleted_paths = backup_delete(CLONE_PATH) | |
after_ondo = ondo() | |
slack(u'before ondo: {}\n'+ | |
'after ondo:{}\n'+ | |
'img path: {}\n'+ | |
'delete_path: \n{}'.format( | |
before_ondo, | |
after_ondo, | |
img, | |
'\n'.join(deleted_paths))) | |
except EnvironmentError as err: | |
slack(unicode(err)) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment