Soemarko Ridwan

iOS & Web Developer ⟡ Coffee Addict ⟡ Scuba Diver


Bludit’s Automated Regular Backup

So I just had a real scare, this blog suddenly just show this error on screen: Theme 'quark' does not exist, unable to display page. I was like, wtf?! And ready to scream in Bludit’s support chat room. But luckily before I did that, I looked for the backup I made when I updated bludit, then I saw it. The file structures all fucked up, seems like the shared hosting company did some kind of roll back and merged this /blog/ folder with old files, htmly files to be specific. And among other things, the index.php file was overwritten too.

I’m glad that the files under ./bl-content are intact, so I can create a clean backup from there. Nuke the whole thing, and start a new bludit instance, and restore the content. As usual, I need to learn things the hard way. Now I need to look into automated regular backup, from the situation mentioned above, you’d already know how simple it is to back-up a Bludit’s site. Just create a script to zip-up a directory, and throw that script into crontab. And here’s the script:

<!--?php

$rootPath = realpath('./blog/');

$today = date("Y-m-d-H-i-s");
$zipFile = "./blog-".$today.".zip";

$zip = new ZipArchive();
$zip--->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);

$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file) {
    // Skip directories (they would be added automatically)
    if (!$file->isDir()) {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);

        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}

// Zip archive will be created only after closing object
$zip->close();

echo $today . " backup done";

“Hold up, shared hosting?! Really?!” Yes really 😁 I always have shared hosting account because it’s dirt cheap. This particular host costs me like $15 / year. A single cheapest instance on DigitalOcean is like $5 / month. Unless it’s needed, I’d rather deploy any projects in a shared hosts rather than in a dedicated or cloud hosts.

If it’s a project that I need to scale, or if I opt for something like Grav, then maybe a cloud hosts made sense. While I do like the idea of doing something like git push to publish a post, premature optimizations are bad. And for now Bludit is perfectly fine.