<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Autarchy of the Private Cave &#187; convert</title> <atom:link href="https://bogdan.org.ua/tags/convert/feed" rel="self" type="application/rss+xml" /><link>https://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Wed, 28 Dec 2022 16:09:04 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>https://wordpress.org/?v=3.8.27</generator> <item><title>How to: convert your VPS root filesystem to btrfs (using rescue boot)</title><link>https://bogdan.org.ua/2016/02/15/how-to-convert-your-vps-root-filesystem-to-btrfs-using-rescue-boot.html</link> <comments>https://bogdan.org.ua/2016/02/15/how-to-convert-your-vps-root-filesystem-to-btrfs-using-rescue-boot.html#comments</comments> <pubDate>Mon, 15 Feb 2016 17:04:25 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[*nix]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[btrfs]]></category> <category><![CDATA[convert]]></category> <category><![CDATA[Debian]]></category> <category><![CDATA[filesystem]]></category> <category><![CDATA[VPS]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=2349</guid> <description><![CDATA[I&#8217;m moving from (a kind of&#8230;) a dedicated server to a VPS, to decrease my frightful anticipation of hardware failures. Honestly though, that server had been freezing up and restarting spontaneously for several months now, causing sometimes really long down-times&#8230; That server is now about 6-7 years old, built with off-the-shelf components, some of which [&#8230;]]]></description> <content:encoded><![CDATA[<p>I&#8217;m moving from (a kind of&#8230;) a dedicated server to a VPS, to decrease my frightful anticipation of hardware failures.<br
/> <em>Honestly though, that server had been freezing up and restarting spontaneously for several months now, causing sometimes really long down-times&#8230;</em><br
/> That server is now about 6-7 years old, built with off-the-shelf components, some of which (the HDD <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /> ) had weird noises from the very start.<br
/> Definitely time to move!</p><p>I&#8217;ve purchased a fairly cheap VPS with an easy, one-click upgrade option for after I&#8217;m done configuring it.<br
/> It comes with a wide selection of OSes to pre-install; I&#8217;ve chosen <strong>Debian Jessie</strong>, version 8.3 as of this writing.</p><p>I wanted to use <strong>btrfs</strong> from the beginning, so could have installed Debian myself, but&#8230; VPS provider does some initial configuration (like their Debian mirror and some other things), so I&#8217;ve felt that converting to btrfs <em>after the fact</em> would be easier. Now that I&#8217;ve done this &#8211; I guess it was fairly easy, although preparation did take some time.</p><p>Below, I&#8217;m providing step-by-step instructions on how to convert your root filesystem from (most likely) <strong>ext4 to btrfs</strong>.<br
/> <span
id="more-2349"></span><br
/> I&#8217;ll be using my provider&#8217;s <em>rescue boot</em> mode: this is a live Debian system which is network-booted on my own VPS, and thus has access to the SSD/HDD of my VPS. Hopefully, your provider has a similar feature.</p><p>Preparing for this conversion, I had mostly used two sources:</p><ul><li>an older <a
href="https://www.howtoforge.com/how-to-convert-an-ext3-ext4-root-file-system-to-btrfs-on-ubuntu-12.10-p2">howtoforge conversion tutorial</a>, and</li><li><a
href="https://btrfs.wiki.kernel.org/index.php/SysadminGuide">Sysadmin Guide</a></li></ul><p>Before we begin with actual conversion, here is a</p><h3>List of hints and useful practices/commands for a future btrfs user</h3><ul><li>Changing the default subvolume with <code>btrfs subvolume set-default</code> will make the top level of the filesystem inaccessible, except by use of the <code>subvol=/</code> or <code>subvolid=5</code> mount options.</li><li>If top level is no longer your default subvolume, it is useful to have an <strong>fstab</strong> entry for the top level (note the <strong>noauto</strong> option!):<br
/> <code>LABEL=toplevel   /root/btrfs-top-lvl   btrfs  subvol=/,defaults,noauto,noatime   0  0</code></li><li><a
href="http://marc.merlins.org/perso/btrfs/post_2014-03-19_Btrfs-Tips_-Btrfs-Scrub-and-Btrfs-Filesystem-Repair.html">scurbbing and repairing btrfs</a>; most of the hints below originate from Marc&#8217;s <a
href="http://events.linuxfoundation.org/sites/events/files/slides/Btrfs_1.pdf">2014 btrfs presentation</a></li><li>scrub script: <a
href="http://marc.merlins.org/linux/scripts/btrfs-scrub">http://marc.merlins.org/linux/scripts/btrfs-scrub</a></li><li><code>mount -o compress=lzo</code> is fast and best for SSDs; <code>mount -o compress=zlib</code> compresses better but is slower</li><li>you can turn off COW (copy-on-write) for specific files and directories with <code>chattr Â­-C /path</code> (new files will inherit this)</li><li>for a highly fragmented VM, <code>btrfs filesystem defragment vbox.vdi</code> could take hours; <code>cp -reflink=never vbox.vdi vbox.vdi.new; rm vbox.vdi</code> is much faster</li><li><code>cp -reflink=always</code> copies within and between subvolumes without duplicating data</li><li>run scrub nightly or weekly on all btrfs filesystems</li><li><strong>noatime</strong> is the best fstab mount option for btrfs <em>IF USING SNAPSHOTS</em>, because <strong>relatime</strong> still causes at least 1 write every 24h</li><li>if you have <strong>errors=remount-ro</strong> in your <strong>fstab</strong> for extX-filesystem, you should remove it after converting to <strong>btrfs</strong> as it does not understand the option.</li></ul><h3>We are almost ready to start! Just one last thing: partitioning!</h3><p>On my previous server, I did have a certain partition scheme, with separate <strong>/boot</strong>, <strong>/home</strong>, <strong>/var</strong>, <strong>/usr</strong>, and everything else under <strong>/</strong> partition.<br
/> It worked very well for me, never ran into problems (although this is more like a Debian&#8217;s feature, not that of my partition scheme <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /> ).<br
/> However&#8230; With the current VPS, it started initially with just everything under <strong>/</strong>, in a single partition.<br
/> I could have re-partitioned everything from the rescue system while converting to <strong>btrfs</strong>, but what for? What would be the benefits?</p><p>Let&#8217;s have a look and evaluate</p><h3>Advantages/disadvantages of keeping specific partitions separate.</h3><ul><li><strong>/boot</strong> comes the first. Normally I format it with ext2, and set aside just 100-200 megabytes for this small partition.<br
/> This improves boot-reliability of the system. With <strong>btrfs</strong>, it was also the case that grub could not use <strong>/boot</strong> on a btrfs volume.<br
/> However,</p><blockquote><p> Newer GRUBs can handle a /boot partition which is btrfs, so you need not have a separate /boot partition formatted as ext3/4.</p></blockquote><p>So this time I&#8217;m going without a separate /boot.<br
/> I will not even create a separate subvolume for it: for snapshotting purposes, <strong>/boot</strong> fits perfectly under the <strong>/</strong> snapshots.</li><li>Next comes <strong>/home</strong>.<br
/> The reason to separate it is to safeguard user data when upgrading/reinstalling the OS.<br
/> This, however, can be achieved with <strong>btrfs subvolumes</strong> &#8211; no need for partitions!<br
/> I&#8217;m going with a &#8220;subvolume for home&#8221; here, then.</li><li>Next comes <strong>/var</strong>, or more precisely <strong>/var/log</strong>.<br
/> One of the reasons to keep <strong>/var/log</strong> separate is to prevent filesystem overfill if logs suddenly start growing too fast.<br
/> This, however, can be achieved with <strong>subvolume quotas</strong>. No need for a partition!</li><li>There is also <strong>/var/lib/<databasename></strong> (and also <strong>/var/lib/lxc/&#8230;</strong>), which hold fairly big data files, often with random write access patterns.<br
/> There are two reasons to make them subvolumes:</p><ol><li>for data-only snapshots</li><li>to enable <code>nocow</code>/<code>nodatacow</code> at mount time, to avoid high fragmentation with random writes</li></ol><p>However, this can be done later, when these databases/LXC get installed &#8211; in just a few commands, without rebooting.</li><li>Finally, <strong>/</strong> itself should be a separate subvolume &#8211; mostly for snapshot and reinstall/upgrade reasons.</li></ul><p>Ok, now we are <strong>really</strong> going to</p><h3>Convert the filesystem</h3><ol><li>Enable rescue system, write down access credentials. You may need to restart your VPS to boot into rescue system, but check with your VPS provider first.</li><li>Connect to and log in to the rescue system.</li><li>Install <strong>btrfs-tools</strong> in the rescue system, if not there yet. We&#8217;ll need <strong>btrfs-convert</strong>, which should be included in <strong>btrfs-tools</strong>.</li><li>I&#8217;m assuming that your SSD/HDD is device <strong>/dev/sda</strong>, and that the only (root) partition there is <strong>/dev/sda1</strong>; adjust the next steps if your setup is different. <em>Only proceed with commands if you understand what they are doing!</em></li><li>It never hurts to <code>fsck /dev/sda1</code> before doing filesystem conversion <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /> You should see something like <em>/dev/sda1: clean, 27282/1564672 files, 351853/6249472 blocks</em>.</li><li><code>btrfs-convert -l toplevel /dev/sda1</code>; note that I&#8217;m adding a &#8220;toplevel&#8221; <em>LABEL</em> to the top level of btrfs, for easier mounting it later. The output should look like this:<br
/><blockquote><p> creating btrfs metadata.<br
/> creating ext2fs image file.<br
/> cleaning up system chunk.<br
/> conversion complete.</p></blockquote><p> At this point the system is already btrfs! <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></li><li>If you forgot to set btrfs label, or decide to do it later: list the filesystems with <code>btrfs filesystem show</code>, or just look for the label with <code>btrfs filesystem label /dev/sda1</code> (which will produce an empty line if there is no label), then simply <code>btrfs filesystem label /dev/sda1 toplevel</code>; it will produce no output on success.</li><li>Let&#8217;s change into the new system, and start configuring it:<br
/> <code>mount /dev/sda1 /mnt<br
/> for fs in proc sys dev dev/pts; do mount --bind /$fs /mnt/$fs; done<br
/> chroot /mnt</code></li><li><code>ls</code> now should show your root filesystem files and directories, plus the <strong>ext2_saved</strong> &#8220;directory&#8221; (which is a subvolume, actually):<br
/><blockquote><p> bin   dev  ext2_saved  initrd.img         installimage.debug  lib64       media  opt   root  sbin  sys  usr  vmlinuz<br
/> boot  etc  home        installimage.conf  lib                 lost+found  mnt    proc  run   srv   tmp  var</p></blockquote></li><li>Run <code>blkid /dev/sda1</code> to find out the UUID of the new btrfs top-level volume. You can also see it with <code>btrfs filesystem show</code>:<br
/><blockquote><p> /dev/sda1: LABEL=&#8221;toplevel&#8221; UUID=&#8221;3124c827-c3bd-4a62-843f-1d5a552f1858&#8243; UUID_SUB=&#8221;ffad5668-2ac7-4ea4-83ad-e6ccba7ccf96&#8243; TYPE=&#8221;btrfs&#8221; PARTUUID=&#8221;750b31a7-01&#8243;</p></blockquote></li><li>Edit <strong>/etc/fstab</strong> (with e.g. <code>nano /etc/fstab</code>):<ul><li>change <strong>UUID</strong> of <strong>/</strong> to the new one</li><li>change the filesystem type from ext3/ext4 to <strong>btrfs</strong></li><li>change the options to <strong>subvol=root,defaults,noatime,compress=lzo,ssd,discard</strong> (yes, <strong>subvol=root</strong> does not exist yet&#8230; also, <strong>ssd,discard</strong> are for SSDs only, and instead of adding <strong>discard</strong> here it might be better to setup an <strong>fstrim</strong> cronjob. Finally, <strong>noatime</strong> can be left out if you are not going to make snapshots.)</li><li>change the last number in the line from 1 to 0 (the fsck passes &#8211; btrfs does not want boot-time filesystem checking)</li></ul></li><li>Some guides mention editing /etc/grub.d/00_header in a specific way, running <em>grub-mkconfig | grep &#8221; ro &#8220;</em> to see if it adds the proper <em>rootflags=subvol=root</em>, then even run <em>update-grub</em> and <em>grub-install /dev/sda</em> here&#8230; But all of this seems redundant or plain unnecessary. First of all, the <em>root</em> subvolume simply does not exist yet, so <em>grub-mkconfig</em> will <strong>not</strong> produce correct <em>rootflags</em>. Secondly, we are not yet done with setting up subvolumes, and we are not going to reboot right now, so running <em>update-grub</em>/<em>grub-install</em> is a bit premature at this stage <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></li><li>Now we are going to create some subvolumes. As I want to name the <strong>root</strong> subvolume simply <em>root</em>, I should first move the root user&#8217;s home files somewhere else (otherwise <code>btrfs snapshot</code> complains &#8220;ERROR: incorrect snapshot name &#8216;/&#8217;&#8221;):<br
/> <code>mv /root /root_orig<br
/> btrfs subvolume snapshot / /root</code></li><li>The same <em>problem</em> with <strong>/home</strong>, need to move it as well: <code>mv /home /home_orig</code></li><li>I can NOT snapshot <strong>/home</strong> like I did with <strong>/</strong>, because it is just a directory, not a volume like <strong>/</strong>, so:<br
/> <code>btrfs subvolume create /home</code><br
/> Note: my <strong>/home</strong> (/home_orig) is still empty, as this is a brand-new installation with only the root user, so double-check steps below, as I haven&#8217;t really run them.<br
/> <code>rsync --progress -aHAX /home_orig/* /home<br
/> ls -la /home/<br
/> ls -la /home_orig/<br
/> rm -fr /home_orig/*<br
/> ls -la /root/home/<br
/> rm -fr /root/home/*</code> # be sure to leave the empty /root/home/ in place: it will be the mount point.</li><li><code>nano /etc/fstab</code> to add the <strong>/home</strong> mount line, using <strong>the same UUID as earlier for the / partition</strong>, but a different <strong>subvol</strong> option:<br
/> <strong>UUID=3124c827-c3bd-4a62-843f-1d5a552f1858 /home               btrfs   subvol=home,defaults 0       0</strong><br
/> Feel free to add more mount options, similar to those for root subvolume. While we are editing /etc/fstab, it also makes sense to add a <strong>non-automount /toplevel</strong> entry (<strong>UUID=3124c827-c3bd-4a62-843f-1d5a552f1858 /toplevel               btrfs   subvol=/,defaults,noauto 0       0</strong>) and create mountpoint for it: <code>mkdir /root/toplevel</code></li><li>Follow the same procedure to create the <strong>/var/log</strong> subvolume. I&#8217;m using <em>flat subvolumes</em> layout here:<br
/> <code>btrfs subvolume create /log<br
/> rsync --progress -aHAX /var/log/* /log<br
/> ls -la /log/<br
/> ls -la /var/log/<br
/> rm -fr /var/log/*<br
/> ls -la /root/var/log/<br
/> rm -fr /root/var/log/*<br
/> </code></li><li>Add <strong>/var/log</strong> entry to <strong>fstab</strong>:<br
/> <strong>UUID=&#8221;3124c827-c3bd-4a62-843f-1d5a552f1858&#8243; /var/log   btrfs   subvol=log,defaults,ssd,discard,compress=lzo,noatime   0   0</strong></li><li>/var/lib/<name> &#8211; databases, LXC, nodatacow/nocow &#8211; can be created later, not doing it here and now.</li><li><code>cp /etc/fstab /root/etc/fstab</code></li><li><code>exit</code> # chroot</li><li> <code>for fs in proc sys dev/pts dev; do umount /mnt/$fs; done<br
/> umount /mnt</code></li><li>Let&#8217;s enter the new root subvolume!<br
/> <code>mount -o subvol=root /dev/sda1 /mnt<br
/> for fs in proc sys dev dev/pts; do mount --bind /$fs /mnt/$fs; done<br
/> chroot /mnt</code></li><li>Oh, we forgot to move back root_orig into /root! That&#8217;s easy: <code>mv root_orig root</code>. If you want your root&#8217;s aliases/profile to load, then just <code>exit</code> and <code>chroot /mnt</code> again (I had to do that for the nice pre-configured prompt colours <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_biggrin.gif" alt=":D" class="wp-smiley" /> )</li><li><code>btrfs subvolume list /</code> to see our new and shiny subvolumes:<br
/><blockquote><p> ID 256 gen 8 top level 5 path ext2_saved<br
/> ID 260 gen 49 top level 5 path root<br
/> ID 261 gen 35 top level 5 path home<br
/> ID 262 gen 42 top level 5 path log</p></blockquote></li><li>Let&#8217;s make <strong>root</strong> the default subvolume! If suddenly <em>rootflags</em> get lost during grub configuration, then the system should still be able to boot. Subvol ID comes from the output of the command above (260 in my case):<br
/> <code>btrfs subvolume set-default 260 /</code> # no output<br
/> <code>btrfs subvolume get-default /</code> # to verify</p><blockquote><p>ID 260 gen 49 top level 5 path root</p></blockquote></li><li>Now let&#8217;s check grub-mkconfig output: <code>grub-mkconfig | grep " ro "</code>. Now that the subvolume exists, it does show all the desired <em>rootflags=subvol=root</em>.</li><li>Let&#8217;s make sure our system will be bootable:<br
/> <code>update-grub<br
/> grub-install /dev/sda</code></li><li>Leave the chroot:<br
/> <code>exit<br
/> for fs in proc sys dev/pts dev; do umount /mnt/$fs; done<br
/> umount /mnt</code></li><li>Now let&#8217;s mount top-level again, for cleanup. If you remember, we had all the <strong>/</strong> files there &#8211; which are now in the <strong>root</strong> subvolume:<br
/> <code>mount -o subvol=/ /dev/sda1 /mnt</code></li><li>Navigate to /mnt (<code>cd /mnt</code>, yes <img
src="https://bogdan.org.ua/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /> ) and delete from here everything that is <strong>NOT</strong> a subvolume.<br
/> The command which supposedly does this is <code>find /mnt -xdev -delete</code> (<strong>-xdev</strong> is supposed to not descend into mounts of other filesystems), but I haven&#8217;t used it &#8211; was a bit tired and did not want to screw up the so-far-successful conversion, so I just did <code>rm -rf bin boot dev etc initrd.img lib lib64 lost+found media mnt opt proc run sbin srv sys tmp usr vmlinuz var root_orig</code> &#8211; basically, deleted everything except for ext2_saved, home, root, and log &#8211; which are all subvolumes.</li><li>Confirm that only subvolumes are left now:<br
/> <code>ls -la</code>:</p><blockquote><p> drwxr-xr-x 1 root root  10 Feb 13 21:30 ext2_saved<br
/> drwxr-xr-x 1 root root   0 Feb 13 22:04 home<br
/> drwxr-xr-x 1 root root 510 Feb 13 22:13 log<br
/> drwxr-xr-x 1 root root 318 Feb 13 22:18 root</p></blockquote><p><code>btrfs subvolume list /mnt</code>:</p><blockquote><p> ID 256 gen 8 top level 5 path ext2_saved<br
/> ID 260 gen 52 top level 5 path root<br
/> ID 261 gen 35 top level 5 path home<br
/> ID 262 gen 42 top level 5 path log</p></blockquote><p><code>btrfs subvolume get-default /mnt</code>:</p><blockquote><p>ID 260 gen 52 top level 5 path root</p></blockquote></li><li>We can leave now: <code>cd / &#038;&#038; umount /mnt</code></li><li>Reboot into the new system. If this fails: can boot rescue again, chroot and check what went wrong&#8230;</li><li>In my case, conversion seemed to have succeeded, BUT: a) for some reason <strong>/toplevel</strong> was mounted at boot and full of root-like files which should not have been there anymore; b) I could not delete <strong>ext2_saved</strong>, at all, neither from <strong>/</strong> nor from <strong>/toplevel</strong>. The problem was with the <strong>toplevel</strong> mount line in <strong>/etc/fstab</strong> (initially, I forgot to specify the filesystem type). After fixing this and rebooting once more, everything was perfect: <strong>/toplevel</strong> not mounted, and when mounted it only had subvolumes in it. And of course the system booted without any problems. Now I could delete the previous filesystem snapshot: <code>mount /toplevel; btrfs subvolume delete /toplevel/ext2_saved; umount /toplevel</code>. Note that there was also another, different <strong>ext2_saved</strong> in the <strong>root</strong> subvolume: it is NOT a subvolume, which was not carried over when we created the <strong>/</strong> snapshot &#8211; only the empty directory was left in place. (Yes, snapshots do not descend into subvolumes.)</li></ol><p>The last 2 commands to issue (after <code>mount /toplevel</code>) are <code>btrfs filesystem defragment -r /toplevel</code> and <code>btrfs balance start /toplevel</code>.<br
/> I&#8217;m not including them in the instructions above: they are not strictly required, and if your filesystem had some heavy use before conversion, you may want to run these 2 commands more intelligently, in phases (metadata first, then big files, then smaller, etc). Another reason is that it may produce significant system load. On my fresh system both finished very quickly.</p><p>&#8230;And we are done!</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2016%2F02%2F15%2Fhow-to-convert-your-vps-root-filesystem-to-btrfs-using-rescue-boot.html&amp;linkname=How%20to%3A%20convert%20your%20VPS%20root%20filesystem%20to%20btrfs%20%28using%20rescue%20boot%29" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2016%2F02%2F15%2Fhow-to-convert-your-vps-root-filesystem-to-btrfs-using-rescue-boot.html&amp;linkname=How%20to%3A%20convert%20your%20VPS%20root%20filesystem%20to%20btrfs%20%28using%20rescue%20boot%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2016%2F02%2F15%2Fhow-to-convert-your-vps-root-filesystem-to-btrfs-using-rescue-boot.html&amp;linkname=How%20to%3A%20convert%20your%20VPS%20root%20filesystem%20to%20btrfs%20%28using%20rescue%20boot%29" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2016%2F02%2F15%2Fhow-to-convert-your-vps-root-filesystem-to-btrfs-using-rescue-boot.html&amp;linkname=How%20to%3A%20convert%20your%20VPS%20root%20filesystem%20to%20btrfs%20%28using%20rescue%20boot%29" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2016%2F02%2F15%2Fhow-to-convert-your-vps-root-filesystem-to-btrfs-using-rescue-boot.html&amp;linkname=How%20to%3A%20convert%20your%20VPS%20root%20filesystem%20to%20btrfs%20%28using%20rescue%20boot%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2016%2F02%2F15%2Fhow-to-convert-your-vps-root-filesystem-to-btrfs-using-rescue-boot.html&#038;title=How%20to%3A%20convert%20your%20VPS%20root%20filesystem%20to%20btrfs%20%28using%20rescue%20boot%29" data-a2a-url="https://bogdan.org.ua/2016/02/15/how-to-convert-your-vps-root-filesystem-to-btrfs-using-rescue-boot.html" data-a2a-title="How to: convert your VPS root filesystem to btrfs (using rescue boot)"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2016/02/15/how-to-convert-your-vps-root-filesystem-to-btrfs-using-rescue-boot.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Email address to image converter</title><link>https://bogdan.org.ua/2009/04/21/email-address-to-image-converter.html</link> <comments>https://bogdan.org.ua/2009/04/21/email-address-to-image-converter.html#comments</comments> <pubDate>Tue, 21 Apr 2009 13:49:09 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Software]]></category> <category><![CDATA[Web]]></category> <category><![CDATA[address]]></category> <category><![CDATA[convert]]></category> <category><![CDATA[email]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=674</guid> <description><![CDATA[After trying several of the kind, I found CHXO email address to image converter a reliable and working one. It supports transparent PNGs, and just works. Also, it comes with a complete PHP source, so it looks like you can embed it into your own web-applications. (As it is GPLed, I&#8217;ve saved a copy for [&#8230;]]]></description> <content:encoded><![CDATA[<p>After trying several of the kind, I found <a
href="http://chxo.com/labelgen/" class="broken_link" rel="nofollow">CHXO email address to image converter</a> a reliable and working one.<br
/> It supports transparent PNGs, and just works. Also, it comes with a complete <a
href="http://chxo.com/labelgen/labelgen.php?showsource=1" class="broken_link" rel="nofollow">PHP source</a>, so it looks like you can embed it into your own web-applications. (As it is GPLed, I&#8217;ve saved a <a
href="http://bogdan.org.ua/wp-content/uploads/2009/04/labelgen.txt">copy</a> for myself.)</p><p>For Gmailers, there&#8217;s a <a
href="http://www.signaturegenerator.net/gmail.php">nicer generator</a>.</p><p>If you&#8217;d like to convert larger amounts of text to images, then use <a
href="http://www.hidetext.net/" class="broken_link" rel="nofollow">hidetext.net</a>:<br
/> <img
src="http://bogdan.org.ua/wp-content/uploads/2009/04/kpvkg2yo2g.gif" alt="hidetext.net" title="hidetext.net" width="653" height="48" class="alignleft size-full wp-image-676" /></p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F04%2F21%2Femail-address-to-image-converter.html&amp;linkname=Email%20address%20to%20image%20converter" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F04%2F21%2Femail-address-to-image-converter.html&amp;linkname=Email%20address%20to%20image%20converter" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F04%2F21%2Femail-address-to-image-converter.html&amp;linkname=Email%20address%20to%20image%20converter" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F04%2F21%2Femail-address-to-image-converter.html&amp;linkname=Email%20address%20to%20image%20converter" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F04%2F21%2Femail-address-to-image-converter.html&amp;linkname=Email%20address%20to%20image%20converter" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2009%2F04%2F21%2Femail-address-to-image-converter.html&#038;title=Email%20address%20to%20image%20converter" data-a2a-url="https://bogdan.org.ua/2009/04/21/email-address-to-image-converter.html" data-a2a-title="Email address to image converter"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2009/04/21/email-address-to-image-converter.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Convert MySQL database from one encoding/collation into another</title><link>https://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html</link> <comments>https://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html#comments</comments> <pubDate>Fri, 08 Feb 2008 19:17:45 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Notepad]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Web]]></category> <category><![CDATA[collation]]></category> <category><![CDATA[convert]]></category> <category><![CDATA[database]]></category> <category><![CDATA[encoding]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[latin1]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[script]]></category> <category><![CDATA[utf8]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html</guid> <description><![CDATA[Most frequent use: convert database from latin1_swedish to utf8_general_ci. Original script found at: MySQL and UTF-8. Update: the original script had an error, it would generate queries likes this one (note the bold part): ALTER TABLE `links` CHANGE `link_rel` `link_rel` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT NULL; This is clearly wrong syntax [&#8230;]]]></description> <content:encoded><![CDATA[<p>Most frequent use: <a
title="self-link" href="http://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html">convert database from latin1_swedish to utf8_general_ci</a>.<br
/> Original script found at: <a
href="http://www.phpwact.org/php/i18n/utf-8/mysql" class="broken_link" rel="nofollow">MySQL and UTF-8</a>.</p><p><ins
datetime="2008-02-22T21:50:28+00:00">Update:</ins> the original script <u>had an error</u>, it would generate queries likes this one (note the bold part):</p><blockquote><p>ALTER TABLE `links` CHANGE `link_rel` `link_rel` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci <strong>NOT NULL  DEFAULT NULL</strong>;</p></blockquote><p>This is clearly wrong syntax (and logic). I fixed this by making comparison to NULL strict (with three equal signs instead of two):</p><blockquote><p> // Does the field default to null, a string, or nothing?<br
/> if ($row['Default'] === NULL)</p></blockquote><p><ins
datetime="2008-06-27T07:23:50+00:00">Update 2:</ins> based on comment by <em>banesto</em>, I modified the script; now it does not require specifying the from_collation, it&#8217;s sufficient to specify to_collation (which will be used for all the fields and tables). The modified code is:</p><blockquote><p>if ($row['Collation'] == &#8221; || $row['Collation'] == $convert_to)<br
/> continue;</p></blockquote><p><ins
datetime="2010-06-28T11:58:11+00:00">Update 3:</ins> the long-lasting, re-appearing NOT NULL DEFAULT NULL problem is finally fixed.</p><p><ins
datetime="2010-06-28T12:11:38+00:00">Update 4:</ins> incorporated Russ&#8217;s fix to skip numeric fields (in order to leave autoincrement values intact).</p><p>Here&#8217;s the script itself: (to copy-paste: first click the &#8220;Plain text&#8221; header)<br
/> <span
id="more-255"></span></p><div
id="ig-sh-1" class="syntax_hilite"><div
class="code"><ol
class="php" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// Original script (v1.0) by/from: http://www.phpwact.org/php/i18n/utf-8/mysql</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// Improved/modified (v1.05) by Bogdan http://bogdan.org.ua/</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// Last updated: 2010-06-28</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// this script will output all queries needed to change all fields/tables to a different collation</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// it is HIGHLY suggested you make a MySQL backup prior to running any of the generated queries</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// this code is provided AS IS and without any warranty</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// add text/plain header when used not as a CLI script; PHP CLI SAPI shouldn't output headers</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #990000;">header</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot;Content-Type: text/plain&quot;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// precaution</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #990000;">die</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot;Make a backup of your MySQL database, then remove this line from the code!&quot;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #990000;">set_time_limit</span><span
style="color: #009900;">&#40;</span><span
style="color: #cc66cc;">0</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// collation you want to change to:</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$convert_to</span> &nbsp; <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'utf8_general_ci'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// character set of new collation:</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$character_set</span><span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'utf8'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// DB login information - *modify before use*</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$username</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'user'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$password</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'pass'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$database</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'database_name'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$host</span> &nbsp; &nbsp; <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'localhost'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">//-- usually, there is nothing to modify below this line --//</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// show TABLE alteration queries?</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$show_alter_table</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">true</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// show FIELD alteration queries?</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$show_alter_field</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">true</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #990000;">mysql_connect</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$host</span><span
style="color: #339933;">,</span> <span
style="color: #000088;">$username</span><span
style="color: #339933;">,</span> <span
style="color: #000088;">$password</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #990000;">mysql_select_db</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$database</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$rs_tables</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_query</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot; SHOW TABLES &quot;</span><span
style="color: #009900;">&#41;</span> or <span
style="color: #990000;">die</span><span
style="color: #009900;">&#40;</span><span
style="color: #990000;">mysql_error</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #b1b100;">while</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row_tables</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_fetch_row</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$rs_tables</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #000088;">$table</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row_tables</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;0&amp;#93;);</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Alter table collation</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// ALTER TABLE `account` DEFAULT CHARACTER SET utf8</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$show_alter_table</span><span
style="color: #009900;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">echo</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot;ALTER TABLE `<span
style="color: #006699; font-weight: bold;">$table</span>` DEFAULT CHARACTER SET <span
style="color: #006699; font-weight: bold;">$character_set</span>;<span
style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #000088;">$rs</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_query</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot; SHOW FULL FIELDS FROM `<span
style="color: #006699; font-weight: bold;">$table</span>` &quot;</span><span
style="color: #009900;">&#41;</span> or <span
style="color: #990000;">die</span><span
style="color: #009900;">&#40;</span><span
style="color: #990000;">mysql_error</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #b1b100;">while</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_fetch_assoc</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$rs</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Collation'&amp;#93; == '' || $row&amp;#91;'Collation'&amp;#93; == $convert_to )</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #b1b100;">continue</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Is the field allowed to be null?</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Null'&amp;#93; == 'YES' )</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$nullable</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">' NULL '</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">else</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$nullable</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">' NOT NULL '</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Does the field default to null, a string, or nothing?</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93; === NULL &amp;&amp; $row&amp;#91;'Null'&amp;#93; == 'YES' )</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">&quot; DEFAULT NULL &quot;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">elseif</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93; != '' )</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">&quot; DEFAULT '&quot;</span><span
style="color: #339933;">.</span><span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93;).&quot;'&quot;;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #b1b100;">else</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">''</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// sanity check and fix:</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$nullable</span> <span
style="color: #339933;">==</span> <span
style="color: #0000ff;">' NOT NULL '</span> <span
style="color: #339933;">&amp;&amp;</span> <span
style="color: #000088;">$default</span> <span
style="color: #339933;">==</span> <span
style="color: #0000ff;">' DEFAULT NULL '</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">''</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">echo</span> <span
style="color: #0000ff;">&quot;/* Warning: wrong combination of 'default value' and 'NULL-flag' detected - and fixed! */<span
style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">echo</span> <span
style="color: #0000ff;">&quot;/* Diagnostics: row&amp;#91;Null&amp;#93; = '<span
style="color: #006699; font-weight: bold;">$row</span>&amp;#91;Null&amp;#93;', row&amp;#91;Default&amp;#93; = &quot;</span> <span
style="color: #339933;">.</span> <span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93;) . &quot;, MySQL version: &quot; . mysql_get_server_info() . &quot; */\n&quot;;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Don't alter INT columns: no collations, and altering them drops autoincrement values</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #990000;">strpos</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Type'&amp;#93;, 'int') !== false) {</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$show_alter_field</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">False</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">else</span> <span
style="color: #009900;">&#123;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$show_alter_field</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">True</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Alter field collation:</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// ALTER TABLE `tab` CHANGE `field` `field` CHAR( 5 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$show_alter_field</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$field</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Field'&amp;#93;);</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #b1b100;">echo</span> <span
style="color: #0000ff;">&quot;ALTER TABLE `<span
style="color: #006699; font-weight: bold;">$table</span>` CHANGE `<span
style="color: #006699; font-weight: bold;">$field</span>` `<span
style="color: #006699; font-weight: bold;">$field</span>` <span
style="color: #006699; font-weight: bold;">$row</span>&amp;#91;Type&amp;#93; CHARACTER SET <span
style="color: #006699; font-weight: bold;">$character_set</span> COLLATE <span
style="color: #006699; font-weight: bold;">$convert_to</span> <span
style="color: #006699; font-weight: bold;">$nullable</span> <span
style="color: #006699; font-weight: bold;">$default</span>;<span
style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></div></div><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&#038;title=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" data-a2a-url="https://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html" data-a2a-title="Convert MySQL database from one encoding/collation into another"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html/feed</wfw:commentRss> <slash:comments>60</slash:comments> </item> </channel> </rss>