{"id":306,"date":"2011-09-06T08:36:03","date_gmt":"2011-09-06T14:36:03","guid":{"rendered":"http:\/\/www.goodjobsucking.com\/?p=306"},"modified":"2011-09-06T14:27:49","modified_gmt":"2011-09-06T20:27:49","slug":"green-backups-with-backuppc-and-wol","status":"publish","type":"post","link":"https:\/\/www.goodjobsucking.com\/?p=306","title":{"rendered":"Green backups with BackupPC and WOL"},"content":{"rendered":"<p>In an effort to reduce power consumption, I&#8217;ve moved most systems I use regularly onto a pair of virtual servers, enabling me to keep many machines off most of the time, except for occasional use.\u00a0 There are still a few physical machines, and I do prefer to have current backups of everything, so I decided to use the wake-on-LAN protocol, which seems to work for most modern computers hard-wired to ethernet (on the same segment.)<\/p>\n<p>I started with <a title=\"this post\" href=\"http:\/\/blog.chameeya.com\/2011\/08\/backuppc-and-waking-machines.html\">this post<\/a> by Salik Rafiq, but my first problem was that I didn&#8217;t want to turn every machine on, back it up, and then just leave it on.\u00a0 Nor did I particularly want to turn every machine off after being backed up, since it&#8217;s not unusual for a machine to be backed up while I&#8217;m actually using it for something.\u00a0 The obvious solution is to store whether or not a machine is actually on, then return it to that state afterward.<\/p>\n<p>I made a few other changes, which I&#8217;ll briefly outline:<\/p>\n<ul>\n<li>My BackupPC sends 5 arguments to ping<del>, so I lazily hacked in arguments 3 and 4.\u00a0 (There are much more elegant ways to do this.)<\/del> so this script assumes that the last argument is the machine name.<\/li>\n<li>I replaced &#8220;ethwake&#8221; with &#8220;wakeonlan,&#8221; for the sake of simplicity (not that etherwake doesn&#8217;t work, but since WOL packets require a hardware address anyway, why not just use the hardware address?)<\/li>\n<li>Related to the above, I retrieve and store the hardware addresses for each machine using the simple command &#8220;arp -a machine &gt; machine.wol&#8221; which gives me a file with the hardware address for each machine.<\/li>\n<li><del>Note that I use the hardcoded directory &#8220;\/usr\/tools\/wol&#8221; for both hardware and state information.<\/del>\u00a0 The $WOLDIR directory is used for hardware and state information.<\/li>\n<\/ul>\n<p>As with Salik&#8217;s original script, this replaces the &#8220;ping&#8221; command for BackupPC, and is reasonable to use with every machine, whether or not it supports wake-on-lan.<\/p>\n<pre>#!\/bin\/bash\r\n\r\n#this script is totally designed for the backuppc ping command\r\n#which is the first thing it does before it starts a backup\r\n#this is a substitute which pings the machine, if it is not\r\n#awake then it wakes it using a magic packet - using the wol.bsh script\r\n#then pings again to make sure\r\n\r\nPING=\/bin\/ping\r\nargnum=$(($#-1))\r\nARGS=${@:1:$argnum}\r\nWAKEHOST=\"${!#}\"\r\nETHWAKE=\/usr\/bin\/wakeonlan\r\nWOLDIR=\/usr\/tools\/wol\r\nSLEEPTIME=3m\r\n\r\nlogger \"Backuppc pinging $ARGS $WAKEHOST\"\r\n\r\nfunction getwol {\r\n\tif [ -f $WOLDIR\/$1.wol ]; then\r\n\t   hwaddr=$(cat $WOLDIR\/$1.wol | cut -d\" \" -f4)\r\n\telse\r\n\t   logger \"No MAC address for $1\"\r\n\t   exit -1\r\n\tfi\r\n}\r\n\r\nfunction fwol {\r\n        TO_WAKEUP=$1\r\n\tgetwol $1\r\n        sudo $ETHWAKE $hwaddr\r\n}\r\n\r\n$PING $ARGS $WAKEHOST &gt;&gt;\/dev\/null 2&gt;&amp;1\r\n\r\nif [ $? -ne 0 ]; then\r\n        fwol $WAKEHOST\r\n        if [ \"$WOL_RES\" = \"FAIL\" ]; then\r\n                exit 1\r\n        fi\r\n\techo \"OFF\" &gt; $WOLDIR\/$WAKEHOST.state\r\n        sleep $SLEEPTIME\r\n        $PING $ARGS $WAKEHOST\r\n        if [ $? -eq 0 ]\r\n        then\r\n           logger \"success waking $WAKEHOST.\"\r\n        else\r\n           logger \"unable to wake $WAKEHOST.\"\r\n           exit 1\r\n        fi\r\nelse\r\n   $PING $ARGS $WAKEHOST\r\n   echo \"ON\" &gt; $WOLDIR\/$WAKEHOST.state\r\nfi\r\n\r\nexit 0<\/pre>\n<p>To turn the machines back on, I updated my postcmd.sh script (see <a title=\"this post\" href=\"http:\/\/www.goodjobsucking.com\/?p=62\">this post<\/a> for details) to look up the machine state, and if it was off before, turn it off now:<\/p>\n<pre>#!\/bin\/bash\r\nWINEXE=\/usr\/bin\/winexe\r\nUNAME=\"Username\"\r\nPWD=\"Password\"\r\nWRKGRP=\"WORKGROUP\"\r\nBOX=$1\r\nPID=$($WINEXE -U $UNAME -W $WRKGRP --password=$PWD \/\/$BOX 'cmd \/c echo '1'\r\n<span style=\"color: #800000;\">&gt; c:\\backuppc\\wake.up')<\/span>\r\necho \"Rsync and shadow copy unloaded\"\r\nif [ -f \/usr\/tools\/wol\/$BOX.state ]; then\r\n   read wasoff &lt; \/usr\/tools\/wol\/$BOX.state\r\n   if [ \"$wasoff\" = \"OFF\" ]; then\r\n      $WINEXE -U $UNAME --password=$PWD \/\/$BOX 'shutdown -f -s -c \"Backup\r\nComplete\"'\r\n   fi\r\nfi<\/pre>\n<p>Although it only recognizes &#8220;on&#8221; and &#8220;off&#8221; as valid machine states (it doesn&#8217;t detect nor return a system to hibernated, sleeping, etc. states) it at least takes care of the simple function of turning machines on, backing them up, then turning them back off.<\/p>\n<p>For systems that don&#8217;t support wake-on-lan, like those connected through wireless, I&#8217;m considering using X10.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In an effort to reduce power consumption, I&#8217;ve moved most systems I use regularly onto a pair of virtual servers, enabling me to keep many machines off most of the time, except for occasional use.\u00a0 There are still a few physical machines, and I do prefer to have current backups \u2026 <a class=\"continue-reading-link\" href=\"https:\/\/www.goodjobsucking.com\/?p=306\"> Continue reading <span class=\"meta-nav\">&rarr; <\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[36,158,157,156],"_links":{"self":[{"href":"https:\/\/www.goodjobsucking.com\/index.php?rest_route=\/wp\/v2\/posts\/306"}],"collection":[{"href":"https:\/\/www.goodjobsucking.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.goodjobsucking.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.goodjobsucking.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.goodjobsucking.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=306"}],"version-history":[{"count":10,"href":"https:\/\/www.goodjobsucking.com\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":317,"href":"https:\/\/www.goodjobsucking.com\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions\/317"}],"wp:attachment":[{"href":"https:\/\/www.goodjobsucking.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.goodjobsucking.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.goodjobsucking.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}