{"id":4369,"date":"2022-01-10T12:05:14","date_gmt":"2022-01-10T03:05:14","guid":{"rendered":"https:\/\/www.wsmeguro.jp\/wp\/?p=4369"},"modified":"2022-01-10T12:13:01","modified_gmt":"2022-01-10T03:13:01","slug":"coreserver%e3%81%ae%e3%83%90%e3%83%83%e3%82%af%e3%82%a2%e3%83%83%e3%83%97%e6%96%b9%e6%b3%95%e5%a4%89%e6%9b%b4%ef%bc%88%e3%81%9d%e3%81%ae%ef%bc%92%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsmeguro.jp\/wp\/?p=4369","title":{"rendered":"coreserver\u306e\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u65b9\u6cd5\u5909\u66f4\uff08\u305d\u306e\uff12\uff09"},"content":{"rendered":"<p>\u305d\u306e\uff11\u3067\u3001coreserver\u306e\/backup\/\u5185\u306b\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u5bfe\u8c61\u30d5\u30a1\u30a4\u30eb\u304c\u4fdd\u5b58\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u306e\u3067\u3001Raspbery\u3000Pi\u3092\u4f7f\u3063\u3066\u3001\u5bb6\u306eHDD\u306b\u79fb\u3059\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002<br \/>\n\u307e\u305a\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u30b2\u30c3\u30c8\u3059\u308bphp\uff08get_backup.php\uff09<br \/>\n<code><br \/>\n$dir_hdd1 = \"\/mnt\/hdd1\/backupfolder\/\";<br \/>\n$org_serv = \"xxx.coreserver.jp\";<br \/>\n$org_user = \"loginnname\";<br \/>\n$org_pass = \"yourpassword\";<br \/>\n$date_today = date(\"Y-m-d\") . \"\/\";<br \/>\n$blogs = array(\"\",\"blog1\/\",\"blog2\/\");<\/p>\n<p>\/\/\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u5148\u306b\u30d5\u30a9\u30eb\u30c0\uff08\u30d5\u30a9\u30eb\u30c0\u540d\u306f\u65e5\u4ed8\uff09\u3092\u4f5c\u6210<br \/>\nforeach($blogs as $blog){<br \/>\n\t$target = $dir_hdd1 . $date_today . $blog;<br \/>\n\tif (file_exists($target)){<br \/>\n\t\techo $target . \" already exists.\\n\";<br \/>\n\t} else {<br \/>\n\t\tif (mkdir($target, 0777, true)){<br \/>\n\t\t\techo \"Made \" . $target . \".\\n\";<br \/>\n\t\t} else {<br \/>\n\t\t\techo \"Failed to make \" . $target . \".\\n\";<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<\/p>\n<p>\/\/ \u63a5\u7d9a\u3092\u78ba\u7acb\u3057\u307e\u3059\u3002\u3067\u304d\u306a\u3051\u308c\u3070\u7d42\u4e86\u3057\u307e\u3059\u3002<br \/>\n$conn_org = ftp_connect($org_serv) or die(\"Couldn't connect to $ftp_server\");<\/p>\n<p>\/\/ \u30ed\u30b0\u30a4\u30f3\u3092\u8a66\u307f\u308b<br \/>\nif (@ftp_login($conn_org, $org_user, $org_pass)) {<br \/>\n    echo \"Connected as $org_user@$org_serv\\n\";<br \/>\n} else {<br \/>\n    echo \"Couldn't connect as $ftp_user\\n\";<br \/>\n}<\/p>\n<p>\/\/\u30e1\u30a4\u30f3\u3068\u30d6\u30ed\u30b0\u7528\u306e\u30b5\u30d6\u30d5\u30a9\u30eb\u30c0\u3092\u9806\u756a\u306b\u5de1\u56de\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u3092\u53d6\u5f97<br \/>\nforeach($blogs as $blog){<br \/>\n\tif (ftp_chdir($conn_org, \"\/backup\/\" . $blog)) {<br \/>\n    echo \"Current directory is now: \" . ftp_pwd($conn_org) . \"\\n\";<br \/>\n\t} else {<br \/>\n\t\t\techo \"Couldn't change directory\\n\";<br \/>\n\t}<br \/>\n\t\/\/ \u30ab\u30ec\u30f3\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u5185\u5bb9\u3092\u5f97\u308b<br \/>\n\t$contents = ftp_nlist($conn_org, \".\");<\/p>\n<p>\tforeach($contents as $file) {<br \/>\n\t\tif (mb_strlen($file)>3){<br \/>\n\t\t\techo $file . \":\";<br \/>\n\t\t\t$ret = ftp_nb_get($conn_org, $dir_hdd1 . $date_today . '\/' . $blog . \"\/\" . $file, $file, FTP_BINARY);<br \/>\n\t\t\twhile ($ret == FTP_MOREDATA) {<br \/>\n\t\t\t\t\/\/ \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3092\u7d99\u7d9a\u3059\u308b\u2026<br \/>\n\t\t\t\t$ret = ftp_nb_continue($conn_org);<br \/>\n\t\t\t}<br \/>\n\t\t\tif ($ret != FTP_FINISHED) {<br \/>\n\t\t\t\techo \"There was an error downloading the file...\";<br \/>\n\t\t\t\texit(1);<br \/>\n\t\t\t}else{<br \/>\n\t\t\t\t\/\/ \u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u304c\u6210\u529f\u3057\u305f\u3089\u30d5\u30a1\u30a4\u30eb\u306f\u524a\u9664<br \/>\n\t\t\t\tftp_delete($conn_org, $file);<br \/>\n\t\t\t\techo $file . \" is done!\\n\";<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>}<\/p>\n<p>\/\/ \u63a5\u7d9a\u3092\u9589\u3058\u308b<br \/>\nftp_close($conn_org);<br \/>\n<\/code><br \/>\n\u3053\u308c\u3067\u3001raspberry\u3000pi\u306b\u63a5\u7d9a\u3055\u308c\u3066\u3044\u308b\u5916\u4ed8\u3051HDD\u306b\u6bce\u65e5\u30d5\u30a9\u30eb\u30c0\u304c\u4f5c\u3089\u308c\u30d5\u30a1\u30a4\u30eb\u304c\u84c4\u7a4d\u3055\u308c\u3066\u3044\u304d\u307e\u3059\u3002\u305f\u3060\u3001\u7121\u7528\u306b\u8caf\u3081\u3066\u3082\u4ed5\u65b9\u306a\u3044\u306e\u3067\u3001\u904e\u53bb1\u5e74\u4ee5\u5185\u306e\u6bce\u6708\u672b\u3068\u76f4\u8fd1\u306e\u6570\u65e5\uff08\u6307\u5b9a\uff09\u5206\u3092\u6b8b\u3057\u3066\u3001\u4ed6\u306f\u524a\u9664\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002\uff08set_backup.php\uff09<br \/>\n<code><br \/>\n\/\/\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u30d5\u30a9\u30eb\u30c0<br \/>\n$dir_hdd1 = \"\/mnt\/hdd1\/backupfolder\/\";<\/p>\n<p>\/\/ \u4fdd\u5b58\u65e5\u6570<br \/>\n$days = 3;<\/p>\n<p>\/\/ \u65e5\u4ed8\u306e\u6b63\u898f\u8868\u73fe<br \/>\n$pattern = \"#^\\d{4}([\/-]?)\\d{2}\\\\1\\d{2}$#\";<\/p>\n<p>\/\/\u4eca\u65e5\u306e\u78ba\u8a8d<br \/>\n$date0 = strtotime(date(\"Y-m-d\"));<br \/>\n$month0 = date(\"m\");<br \/>\necho $date0 . \":\" . date(\"Y-m-d\", $date0) . \" \" . $month0 . \"\\n\";<\/p>\n<p>\/\/\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u30d5\u30a9\u30eb\u30c0\u306e\u30d5\u30a1\u30a4\u30eb\u4e00\u89a7\u306e\u53d6\u5f97<br \/>\n$files = scandir($dir_hdd1, SCANDIR_SORT_DESCENDING);<br \/>\nforeach ($files as $file) {<br \/>\n  \/\/\u6b63\u898f\u8868\u73fe\u306b\u30de\u30c3\u30c1\u3059\u308c\u3070\u51e6\u7406\u5bfe\u8c61\u306b\u3059\u308b\uff08\u672c\u5f53\u306f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u30c1\u30a7\u30c3\u30af\u3082\u5fc5\u8981\uff09<br \/>\n  if (preg_match($pattern, $file, $match)) {<br \/>\n    $date1 = strtotime($file);<br \/>\n    $month1 = date(\"m\", $date1);<br \/>\n    if ((($date0 - $date1) > ($days - 1) * 86400) && $month0 == $month1) {<br \/>\n      $target = $dir_hdd1 . $file;<br \/>\n      system(\"rm -rf {$target}\");<br \/>\n      echo \"Deleted \" . $file . \"!\\n\";<br \/>\n    } else {<br \/>\n      echo $date1 . \":\" . date(\"Y-m-d\", $date1) . \" \" . $month1 . \":\" . ($date0 - $date1) . \"\\n\";<br \/>\n    }<br \/>\n  }<br \/>\n}<br \/>\n<\/code><br \/>\n\u3053\u308c\u3089\u3092\u6bce\u65e5\u52d5\u304b\u3059\u305f\u3081\u306e\uff53\uff48<br \/>\n<code><br \/>\n#!\/bin\/sh<br \/>\n\/usr\/bin\/php \/root\/script\/get_backup.php<br \/>\n\/usr\/bin\/php \/root\/script\/set_backup.php<br \/>\n<\/code><br \/>\n\u3053\u308c\u3092cronta\uff42\u306b\u767b\u9332\u3057\u3066\u6bce\u65e5\u52d5\u304b\u305b\u3070\u3001\u76f4\u8fd1\uff13\u65e5\u5206\u30681\u5e74\u4ee5\u5185\u306e\u5404\u6708\u672b\u306e\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u304c\u4fdd\u5b58\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u305d\u306e\uff11\u3067\u3001coreserver\u306e\/backup\/\u5185\u306b\u30d0\u30c3\u30af\u30a2\u30c3\u30d7\u5bfe\u8c61\u30d5\u30a1\u30a4\u30eb\u304c\u4fdd\u5b58\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u306e\u3067\u3001Raspbery\u3000Pi\u3092\u4f7f\u3063\u3066\u3001\u5bb6\u306eHDD\u306b\u79fb\u3059\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002 \u307e\u305a\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u30b2\u30c3\u30c8\u3059\u308bphp\uff08get_ba&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[593,454,481,430],"tags":[],"class_list":["post-4369","post","type-post","status-publish","format-standard","hentry","category-iot","category-php","category-raspberry-pitinker-board","category-430"],"_links":{"self":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4369"}],"version-history":[{"count":9,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4369\/revisions"}],"predecessor-version":[{"id":4378,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4369\/revisions\/4378"}],"wp:attachment":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}