Matteo Mattei

Hello, my name is Matteo Mattei and this is my personal website. I am computer engineer with a long experience in Linux system administration and web software development. My preferred computer languages and technologies are PHP, Python, Bash, HTML, HTML5, CSS3, jQuery, MySQL, Node.js, MongoDB, etc...

© 2006-2015 Matteo Mattei - all rights reserved. About my blog code

linkedin rss twitter google+ github facebook

HTML5 Ajax file upload

Very often I have to write HTML forms for uploading files and since I would like to have something responsive without a full page reload I decided to write a little script as example for the next time I will need something similar. The following example should be compatible with every browser but the actual dynamic part is only available on new browsers that support FormData object from XMLHttpRequest2. Old browsers should be able to upload the files using the old (standard) way with just few changes in the server side part.

The following example creates a form for uploading PDF files (checks at line 34) and show the list of uploaded files inside the

element with class result.

The main components of the script are the following:

  • FormData object from XMLHttpRequest2.
  • multiple attribute of file input HTML field.
  • FileReader object from the new File API.

Remember to apply all needed checks in the PHP server side script.

Please note the TransferCompleteCallback() at line 10 that can be used to get the content of the files once they are transferred. This might be useful for example to directly render uploaded images to the screen without an additional request to the server to get them back.

Mount NFS share using Virtualbox under NAT

I am using Ubuntu 14.04 (but it is valid also with other Linux distributions) that runs under Virtualbox. My network interface is set as NAT and I am running nfs version 4 (NFSv4).

Every time I try to mount a remote share I always obtain the following error:

matteo@vm:~$ sudo mount -t nfs /mnt/remote
mount.nfs: access denied by server while mounting

And in the kernel log (dmesg) I see this:

[ 1351.443078] RPC: server requires stronger authentication.

The issue here is that the NFS client is trying to use UDP protocol to access the remote host. You can verify it by capturing the network traffic with wireshark (or tcpdump). In this case is sufficient to force the usage of the TCP protocol when you mount the share:

matteo@vm:~$ sudo mount -t nfs /mnt/remote -o proto=tcp

Another option is to change your network adapter in Virtualbox from NAT to BRIDGE.

That's all for the moment.

Use Python to print labels with Dymo LabelWriter 450 Turbo

Dymo is a leading company for label printing. Altought several things can be done using their DYMO Label v.8 Software, I found that there was no way to set a specific date in a label layout so that every time I need to print a label I have the current date plus 30 days. I read the manual and I found that to do it we have to use a variable field but this is something that has to be handled using an external program.

So I wrote a little python tool that runs on Windows 7 and that does exactly what I need. Make sure to install pywin32 since we are going to use COM apis.

This is my example label (my.label) and the related Python code:

As you can see I have two fields in the XML with IsVariable set to True and I have assigned to them the name TEXT1 and TEXT2. Now, all the logic happens between line 30 and line 41 of the Python script.

  • I initialize the Dispatch for the printer, configure the label I want to use (mylabel) and then I select the printer [lines 30-34].
  • Then I apply my rules to the variables TEXT1 and TEXT2 [lines 36-37].
  • And finally I launch the print job [lines 39-41].

That's all! With only few lines of Python code we are able to print a label using our Dymo LabelWriter 450 Turbo printer.

Note: the full code is available on GitHub including the scripts to build a self-contained executable.

Backup your server on using megatools

Mega is a wonderful and secure online cloud service that offers 50GB of free storage. So I thought that it would be great using it as additional backup service for my Linux servers. Fortunately there is a good application library and tools to access Mega storage from Linux command line. This tools are called megatools and it is released under GPLv2 license.

In this post I will show you how to compile, install and configure a full backup system for your server using Mega and MegaTools.

First of all register an account with Mega at then follow these instructions to compile and install megatools.



apt-get install -y pkg-config libglib2.0-dev libssl-dev libcurl4-openssl-dev libfuse-dev glib-networking

tar xzf megatools-${VERSION}.tar.gz
cd megatools-${VERSION}
./configure && make && make install && ldconfig

Note: the above instructions are valid for Debian 7 Wheezy. In case you have a different Linux distribution please install the required dependencies.

Now that you have MegaTools installed in /usr/local/bin create a configuration file with your credentials in /root/.megarc:

Username = Your_Mega_Username
Password = Your_Mega_Password

Since the password is in clear, it is important to protect the file:

chmod 640 /root/.megarc

Test now your mega installation and login credentials:

root@debian:~# megals 

If all goes well you are ready to prepare your backup script. Create a new file called and place it in /root:

Make it executable and accessible only to root:

chmod 750 /root/

You only need to set a cron-job now to execute the backup every day:

04 04 * * * root /root/

Update 2015-02-14: MegaFuse

I recently found a better tool to accomplish my needings, it is called MegaFuse and it works like a Linux mountpoint. First of all download all needed dependencies:

apt-get install libcrypto++-dev libcurl4-openssl-dev libdb5.1++-dev libfreeimage-dev libreadline-dev libfuse-dev make

Then download MegaFuse and compile it:

git clone
cd MegaFuse

Now create a configuration file /root/.megafuse.conf with your Mega credentials:

USERNAME = your_mega_email
PASSWORD = your_mega_password

The above configuration assume you will use /mnt as your mountpoint (change it if you want).

Protect it in the same way:

chmod 640 /root/.megafuse.conf

And this is the revisited script to do the backup:

You can now configure the cronjob:

04 04 * * * root /root/

Create a self-contained installer in Bash

In this post I will show you how to develop a self contained Linux command line installer in Bash that will decompress an archive and perform some tasks.

Installer content

Our installer that is basically a self-extracting archive with some logic around, consists in three parts:

  • A bash script that performs the extraction of the archive and applies some logic.
  • A marker to separate the bash script and the archive.
  • An archive containing the actual data to install.

Start now!

Create a new bash script called with the following content:


echo ""
echo "My Command Line Installer"
echo ""

# Create destination folder
mkdir -p ${DESTINATION}

# Find __ARCHIVE__ maker, read archive content and decompress it
ARCHIVE=$(awk '/^__ARCHIVE__/ {print NR + 1; exit 0; }' "${0}")
tail -n+${ARCHIVE} "${0}" | tar xpJv -C ${DESTINATION}

# Put your logic here (if you need)

echo ""
echo "Installation complete."
echo ""

# Exit from the script with success (0)
exit 0


This script is self-explain but I will try to describe the steps:

  1. Create a destination folder ${DESTINATION}.
  2. Find ARCHIVE marker and put the tarball content into ${ARCHIVE} variable.
  3. Decompress the tarball into the destination folder.
  4. Eventually apply your installation logic (copy some files, change some others, etc...).
  5. Exit from the script (this step is mandatory otherwise bash will try to interpret the tarball and will exit with error).
  6. Add ARCHIVE marker at the bottom of the script. This marker will be used to separate the actual bash script with the tarball content.

Now generate a compressed tarball of your application (I used .tar.xz in the above example):

tar cJf myarchive.tar.xz /folder/to/archive

OK, now append it to the installer bash script and make it executable:

cat myarchive.tar.xz >>
chmod +x

That's all! You can now distribute your installer.

Execute your installer

The users will execute your installer simply running:


Install CouchDB 1.6.x on Debian 7 (Wheezy)

Setup repository and install all dependencies

echo "deb wheezy contrib" >> /etc/apt/sources.list
wget -qO - | apt-key add -
apt-get update
apt-get install -y build-essential curl erlang-nox erlang-dev libmozjs185-1.0 libmozjs185-dev libcurl4-openssl-dev libicu-dev

Create CouchDB account

useradd -d /var/lib/couchdb couchdb
mkdir -p /usr/local/{lib,etc}/couchdb /usr/local/var/{lib,log,run}/couchdb /var/lib/couchdb
chown -R couchdb:couchdb /usr/local/{lib,etc}/couchdb /usr/local/var/{lib,log,run}/couchdb
chmod -R g+rw /usr/local/{lib,etc}/couchdb /usr/local/var/{lib,log,run}/couchdb

Install CouchDB

tar xzf apache-couchdb-1.6.1.tar.gz
cd apache-couchdb-1.6.1
./configure --prefix=/usr/local --with-js-lib=/usr/lib --with-js-include=/usr/include/js --enable-init
make && make install

Create symlinks and start the database

chown couchdb:couchdb /usr/local/etc/couchdb/local.ini
ln -s /usr/local/etc/init.d/couchdb /etc/init.d/couchdb
/etc/init.d/couchdb start
update-rc.d couchdb defaults

Verify that all is working fine


The output should be like this:

{"couchdb":"Welcome","uuid":"5da242ff50cecec904d6caf36be34194","version":"1.6.1","vendor":{"name":"The Apache Software Foundation","version":"1.6.1"}}

Finalize setup

In order to connect from remote edit /usr/local/etc/couchdb/local.ini and change



bind_address to

Restart the database:

service couchdb restart

And from a web browser visit the CouchDB Futon: