Showing posts with label web development. Show all posts
Showing posts with label web development. Show all posts

May 31, 2019

mysql rsnapshot backup separate database table files

Backing up MySQL databases and tables as separate files with rsnapshot. Simply copying /var/lib/mysql won't always work, so the databases need to be exported.

First, create a separate MySQL user backup with these limited to read-only permissions: LOCK TABLES, SELECT, SHOW VIEW.

Install rsync - otherwise you'd get bash: rsync: command not found; rsync: connection unexpectedly closed.

Then set up this script:

#! /bin/bash

BACKUP_DIR="/var/backups/mysql"
MYSQL_USER="backup"
MYSQL_PASSWORD="hackme"
MYSQL=$(which mysql)
MYSQLDUMP=$(which mysqldump)

databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`

for db in $databases; do
  for tbl in `$MYSQL -u $MYSQL_USER -p$MYSQL_PASSWORD -N -B -e "SHOW TABLES FROM $db"`; do
    VAR_LOCATION="${BACKUP_DIR}/${db}/${tbl}.sql"
    mkdir -p "$(dirname ${VAR_LOCATION})"
    $MYSQLDUMP --skip-comments --compact -u $MYSQL_USER -p$MYSQL_PASSWORD $db $tbl > "${VAR_LOCATION}"
  done
done

The above is a combination of these scripts from mensfeld.pl and jamescoyle.net.

I previously tried using the following line from this answer:

mysqldump --user=dbuser --password --tab=~/output/dir dbname
But that doesn't work, really: it throws Access denied (using password: YES) when executing 'SELECT INTO OUTFILE' because it requires a special kind of GRANT you can't easily set via the MySQL Workbench UI. After all, you end up with a CSV backup which you have to treat differently.

February 1, 2019

dompdf setPaper size orientation not working

According to the Usage page, in order to set the paper size and orientation you need to call setPaper(). However, this won't work if in your actual HTML you're rendering you have page size definition such as this:
    @page {
        margin: 0;
        padding: 0;
        size: 210mm 297mm;
    }
    html {
        margin: 0;
        padding: 0;
    }
    body {
        margin: 0;
        padding: 0;
    }
This will override whatever you set with setPaper(). Either remove it, or edit it.

August 4, 2017

opencart attach all categories and products to a new store

Added a new store to opencart and now realised that none of the existing products and categories are linked to it. Will have to either manually edit 700 items, or come up with a MySQL query to bulk add multiple products and categories to that store for me.

Products

Store ID=123. Deleting all existing links to this store (if any) to avoid duplicate index error. Then attaching all products to it.
DELETE FROM feetunique.product_to_store where store_id=123;
INSERT INTO product_to_store (product_id, store_id) SELECT product_id, '123' from `product`;

Categories

Again, first deleting all existing links then adding new ones.
DELETE FROM feetunique.category_to_store where store_id=123;
INSERT INTO category_to_store (category_id, store_id) SELECT category_id, '123' from `category`;

You can add conditions to retrieve only active products (status=1), or any other filters if you want.

July 19, 2017

debug opencart 2.3 events triggers not working

Opencart's Event system is a better alternative to vQmod, although how to use it is not obvious, and the documentation is poorly written. As usual, working with Opencart is a major pain in the ass. I've spent quite a lot of hours trying to figure out how Events are supposed to work. I decided to write something down because surprisingly there doesn't seem to be a lot of information online. If you found this and still can't figure it out, please comment and I'll try to elaborate.

Here's how I was debugging them:

  • To debug what events are present on a given page, go to the Event::trigger() function and var_dump the $event variable. Those are the hooks you can listen on
  • Use either catalog or admin (depending on where's your callback) controller/startup/event.php file to check if your event was registered. This file's register function adds the events to the queue using the similarly named register() function from system/engine/event.php above. Once it's done, you can dump it's $this->data variable and check if a. your event is there and b. if it has a valid action. Here's what a valid action looks like:
  • If your action is null, the system may be looking for a wrong callback path. The action for your event is built upon the callback route you provide when you register an event. That route is then converted into a full path of a file the system tries to load and execute. 
  • If you're trying to hook before admin/controller/catalog/product::index(), your event trigger will be admin/controller/catalog/product/before, note that "index" gets omitted.
  • Another gotcha. When Opencart tries to execute an action, it checks if the number of parameter your callback action expects matches the number of arguments it's about to pass to it. If it doesn't match, the action will fail silently, with the Error: Could not call message only visible in the debug trace. The solution is to make your callback arguments optional.

June 17, 2017

yandex metrika currencyCode missing

Я пытался настроить электронную коммерцию в Яндекс Метрике, и в настройках счетчика указывал параметр currencyCode для отслеживания валюты заказа, но он терялся из запроса к mc.yandex.ru и, соответственно, не отображался в отчетах.

Действовал согласно документации "Представление и передача данных", но в этот раз она меня, похоже, подвела.

Экспериментальным путем выяснилось что этот параметр надо передвинуть вниз под <actiontype>, то есть чтобы он соседствовал с products. Вот как это выглядит:

February 15, 2017

Using a .glyphicon as a list bullet point icon

Let's say you need to use a bootstrap's glyphicon as an <ul> bullet-point icon. You can try going with li:before:

li:before {
    content: "\e013";
}

But I think there's a problem with this: you'll have to re-declare (copy-paste) the icon's style into your own CSS file. You'll basically hardcode the Glyphicons Halflings font name and it's internal icon code (\e013 in the example above). If Boostrap, for whatever reason, changes that in the future, your HTML and CSS would have to be updated accordingly.

Here's what I did to avoid that:
<ul class="list-unstyled">
    <li>
        <i class="glyphicon glyphicon-ok"></i>
        Hello
    </li>
    <li>
        <i class="glyphicon glyphicon-ok"></i>
        World!
    </li>
</ul>
ul {
    margin-left: 17px;
}
ul li i.glyphicon {
    margin-left: -17px;
}

February 13, 2017

CSS diagonal gradient line is pixelated, not smooth - workaround

The problem is described in this stackoverflow question. Basically, when you create a divided gradient, the now visible line has 1px "steps" on it. Here's what it looks like:


To fix that, decrease each previous stripe's width by 0.01%:

background:linear-gradient(135deg, red 49.99%, blue 50.00%)