<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Admin on Madprof&#39;s workshop</title>
    <link>http://www.madprof.net/tags/admin/</link>
    <description>Recent content in Admin on Madprof&#39;s workshop</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Fri, 21 Mar 2014 11:16:00 +0000</lastBuildDate>
    <atom:link href="http://www.madprof.net/tags/admin/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>LVM snapshots for a resetable machine</title>
      <link>http://www.madprof.net/2014/03/21/lvm-snapshots-for-a-resetable-machine/</link>
      <pubDate>Fri, 21 Mar 2014 11:16:00 +0000</pubDate>
       <guid isPermaLink="false">https://blog.madprof.net/?p=313</guid> 
      <description>&lt;p&gt;I have ended up maintaining a few websites which we are hosting on a machine off in Germany somewhere.&lt;/p&gt;
&lt;p&gt;I want to get everything automated, so I have less work to do if something goes wrong.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m using ansible, which is wonderful, and have a nice set of playbooks I&amp;rsquo;ve written which take a raw CentOS install, and install everything, (php-fpm, nginx, etc&amp;hellip;) set up the virtualhosts, install wordpress &amp;amp; joomla and all that for the sites that need it, etc.&lt;/p&gt;
&lt;p&gt;Until today, I&amp;rsquo;ve been using a virtualbox on my local computer to test on, and it works great.  I haven&amp;rsquo;t bothered with vagrant, as I tried it for a couple of days, and it crashed my whole computer twice, so I gave up.  With virtualbox, it&amp;rsquo;s almost as simple.  I have a virtualmachine which I can spin up, install stuff on, and then when I want to go back to a fresh machine, it&amp;rsquo;s a matter of turning it off, and clicking &amp;lsquo;restore snapshot&amp;rsquo; to the snapshot I made when it was clean installed.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s practically instant, and just works.&lt;/p&gt;
&lt;p&gt;However, running a virtualmachine on my primary work computer all the time does make everything else somewhat sluggish.  So I&amp;rsquo;ve scrouged an old computer that wasn&amp;rsquo;t doing anything, and am now using that instead.&lt;/p&gt;
&lt;p&gt;In order to get snapshots and restore points going well, here&amp;rsquo;s how I did it:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Install CentOS, leaving a bunch of free space on the LVM primary group.&lt;/li&gt;
&lt;li&gt;Make a snapshot when it&amp;rsquo;s first installed&lt;/li&gt;
&lt;li&gt;Restore (merge to) that snapshot whenever I want it back to original settings.&lt;/li&gt;
&lt;li&gt;Reboot&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To make &amp;amp; restore the snapshots, I&amp;rsquo;ve written the commands as scripts so I don&amp;rsquo;t have to remember the lg-whatever stuff. (vg_localtest is the name of the volume group I set up for the HD when I installed):&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;/usr/sbin/snapshot_make&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;#!/bin/sh&lt;br&gt;
lvcreate &amp;ndash;size 100G -s -n original_snapshot /dev/vg_localtest/root&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;/usr/sbin/snapshot_restore&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;#/bin/sh&lt;br&gt;
lvconvert &amp;ndash;merge /dev/vg_localtest/original_snapshot &amp;amp;&amp;amp; reboot&lt;/p&gt;
&lt;p&gt;It works great so far.  One improvement I&amp;rsquo;m making, since I one time forgot to make a snapshot, and so couldn&amp;rsquo;t restore to a blank slate without re-installing the whole thing (which, admittedly, only takes half an hour or so):&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m adding &amp;lsquo;snapshot_make&amp;rsquo; into a boot script, and then modifying it to remove itself from the bootscript once it&amp;rsquo;s made the snapshot.  That way as soon as the machine reboots into it&amp;rsquo;s original snapshot, it will automatically re-create the snapshot.&lt;/p&gt;
&lt;p&gt;This looks like:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;/usr/sbin/snapshot_make&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;#!/bin/sh&lt;br&gt;
lvcreate &amp;ndash;size 100G -s -n original_snapshot /dev/vg_localtest/root&lt;br&gt;
sed -ine &amp;lsquo;/snapshot/d&amp;rsquo; /etc/rc.local&lt;/p&gt;
&lt;p&gt;and then &lt;strong&gt;/etc/rc.local&lt;/strong&gt; will look like:&lt;/p&gt;
&lt;p&gt;#&amp;hellip; whatever it has&lt;br&gt;
/usr/sbin/snapshot_make&lt;/p&gt;</description>
    </item>
    <item>
      <title>Merging directories with the magic of Python.</title>
      <link>http://www.madprof.net/2013/10/31/merging-directories-with-the-magic-of-python/</link>
      <pubDate>Thu, 31 Oct 2013 10:59:00 +0000</pubDate>
       <guid isPermaLink="false">https://blog.madprof.net/?p=314</guid> 
      <description>&lt;p&gt;We finally got the last projects out of that monstrosity &amp;lsquo;Final Cut Server&amp;rsquo;, but one project at the end was a nightmare to export, and we weren&amp;rsquo;t sure which files from the end actually were in a different version of the project that we already had.&lt;/p&gt;
&lt;p&gt;We essentially needed to merge two different versions of projects directories, making sure not to lose any files, and we didn&amp;rsquo;t want to lose the organization of the files.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a quick python script I wipped up to make it quicker.&lt;/p&gt;
&lt;p&gt;With the 4000 odd files in the project, it took under a second to run, and it turned out we only had about 20 files which hadn&amp;rsquo;t already been merged.  Much simpler to sort out.&lt;/p&gt;
&lt;p&gt;The script took about 10 minutes to write and test. This is why you should learn to program.  Hacking stuff like this up is easy, and saves *so* much time.&lt;/p&gt;
&lt;p&gt;(Yes, you probably could do this with a couple of lines of perl or BASH, but what the heck.)&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f3f3;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#09f;font-style:italic&#34;&gt;#!/usr/bin/env python&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;from&lt;/span&gt; &lt;span style=&#34;color:#0cf;font-weight:bold&#34;&gt;subprocess&lt;/span&gt; &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;import&lt;/span&gt; Popen, PIPE
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;from&lt;/span&gt; &lt;span style=&#34;color:#0cf;font-weight:bold&#34;&gt;os&lt;/span&gt; &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;import&lt;/span&gt; stat
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;from&lt;/span&gt; &lt;span style=&#34;color:#0cf;font-weight:bold&#34;&gt;os.path&lt;/span&gt; &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;import&lt;/span&gt; basename, abspath
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#c0f&#34;&gt;run&lt;/span&gt;(&lt;span style=&#34;color:#555&#34;&gt;*&lt;/span&gt;command):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    found &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; Popen(command, stdout&lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt;PIPE)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;return&lt;/span&gt; found&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;communicate()[&lt;span style=&#34;color:#f60&#34;&gt;0&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#c0f&#34;&gt;files_in&lt;/span&gt;(dirname):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;return&lt;/span&gt; [x &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;for&lt;/span&gt; x &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;in&lt;/span&gt; run(&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;find&amp;#39;&lt;/span&gt;, abspath(dirname), &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;-type&amp;#39;&lt;/span&gt;,&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;f&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;-print0&amp;#39;&lt;/span&gt;)&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;split(&lt;span style=&#34;color:#366&#34;&gt;chr&lt;/span&gt;(&lt;span style=&#34;color:#f60&#34;&gt;0&lt;/span&gt;)) &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;if&lt;/span&gt; x]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;if&lt;/span&gt; &lt;span style=&#34;color:#033&#34;&gt;__name__&lt;/span&gt; &lt;span style=&#34;color:#555&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;from&lt;/span&gt; &lt;span style=&#34;color:#0cf;font-weight:bold&#34;&gt;sys&lt;/span&gt; &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;import&lt;/span&gt; argv
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;try&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        sourcedir &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; files_in(argv[&lt;span style=&#34;color:#f60&#34;&gt;1&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        destdir &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; files_in(argv[&lt;span style=&#34;color:#f60&#34;&gt;2&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;except&lt;/span&gt; &lt;span style=&#34;color:#c00;font-weight:bold&#34;&gt;IndexError&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;Usage:&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; argv[&lt;span style=&#34;color:#f60&#34;&gt;0&lt;/span&gt;], &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;  &amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;Where you want to check if files in  are also in &amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;(but perhaps with a different relative path)&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        exit(&lt;span style=&#34;color:#f60&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;---------------------------------------------------&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{0}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt; files in &lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{1}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;format(&lt;span style=&#34;color:#366&#34;&gt;len&lt;/span&gt;(sourcedir), abspath(argv[&lt;span style=&#34;color:#f60&#34;&gt;1&lt;/span&gt;]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{0}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt; files in &lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{1}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;format(&lt;span style=&#34;color:#366&#34;&gt;len&lt;/span&gt;(destdir), abspath(argv[&lt;span style=&#34;color:#f60&#34;&gt;2&lt;/span&gt;]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;---------------------------------------------------&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    destnames &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; {}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;for&lt;/span&gt; destfile &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;in&lt;/span&gt; destdir:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        destnames[basename(destfile)] &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; {&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;size&amp;#39;&lt;/span&gt;: stat(destfile)&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;st_size,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;path&amp;#39;&lt;/span&gt;: destfile }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;for&lt;/span&gt; newfile &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;in&lt;/span&gt; sourcedir:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        base &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; basename(newfile)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;if&lt;/span&gt; base &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;not&lt;/span&gt; &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;in&lt;/span&gt; destnames:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; newfile, &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;is NOT in the new dir&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;else&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            destfile &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; destnames[base]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;if&lt;/span&gt; stat(newfile)&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;st_size &lt;span style=&#34;color:#555&#34;&gt;!=&lt;/span&gt; destfile[&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;size&amp;#39;&lt;/span&gt;]:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{0}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{1}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;) differs from &lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{2}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{3}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;)&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;format(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                      newfile, stat(newfile)&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;st_size,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                      destfile[&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;path&amp;#39;&lt;/span&gt;], destfile[&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;size&amp;#39;&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
    </item>
  </channel>
</rss>
