How To Dump Postgres Database Running in a Docker Container?

All you need to do is to run the following comand:

docker exec -t database pg_dumpall -c -U postgres > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql

After installation, Postgres has a standard user postgres with no password. To set a password for the user, just connect to the database

sudo -u postgres psql postgres

and alter the user

ALTER USER postgres WITH PASSWORD 'new_password';

Connect to Postgres database in Docker

docker exec -tiu postgres database psql

 

 

How to automatically renew LetsEncrypt Certificate for Jetty on Ubuntu?

If you are using an embedded Jetty server for your web application, you will not find any information on how to automatically renew your certificate (unlike Apache or Ngnix server).

This is a short tutorial on how to set up automatic certificate renewal on Ubuntu Linux using a bash script (works the same or similar for all Linux distros) .

Go to folder /etc/cron.weekly, or cron.daily, or cron.monthly, if you want to have checked renewal daily or monthly. I chose weekly.

cd /etc/cron.weekly

Create a new file named certrenew with nano or any other editor.

nano certrenew

Add the following lines to new file.

#!/bin/bash
certbot renew --webroot -w /var/www/
kill $(ps aux | grep '[m]y_app_name' | awk '{print $2}')
sleep 3
java -jar /var/www/myapp.jar &

Let’s briefly explain the five lines.

Line 1: Standard text to indicate it is a bash script.

Line 2: This is the certbot command to renew your certificate. The path behind -w is where your application is located. Change if your app is located somewhere else.

Line 3: This command finds and kills your running application. This is needed to hook up the renewed certificate. Change the following to your app name: [m]y_app_name
If your app name is myGreatApp.jar for example, then change to [m]yGreatApp.

Line 4: Lets the bash script sleep three seconds before continuing to make sure the app was killed properly.

Line  5: Command to restart your app. Change the path to where your app is located.

Finally make the file executable.

sudo chmod a+x certrenew

That’s it! The script will now be automatically executed depending on the selected time frame (daily, weekly, monthly). To test it just execute it by

./certrenew

How To Install BOINC on Linux VPS?

Crunching with your computer is a nice way to help scientists find cures and drugs for diseases, explore the mysteries of the universe or to find methods to better understand the climate. Just as you can use your laptop or desktop PC, you can also use a virtual private server (VPS) that runs automatically around the clock.
This blog post shows step-by-step instruction on how to install BOINC on a VPS with Ubuntu.

  1. You need to sign up for a VPS plan. If you have already a VPS then skip this step. The VPS needs to have at least one core and 512 Mb Ram. Better to have 1024 Mb Ram. I recommend to use a VPS from Vultr.com or Digitalocean.com where VPS including 1024 Mb starts at $5 a month.
  2. Connect to the server using terminal. Submit command
    ssh root@<IP ADDRESS>

    and enter server password.

    console1
    Server connection (sensitive data is blackened)
  3.  When you are connected to your VPS then firstly update the list of packages by entering
    sudo apt update

    Wait until all package lists are updated. Now install BOINC by entering

    sudo apt-get install boinc-client

    After installation is done, start the BOINC client by entering

    /etc/init.d/boinc-client start

    It should show the following “ok” output:

    console2
    Output when BOINC client started successfully
  4. Start the BOINC service and navigate to BOINC installation folder by entering
    sudo /sbin/service boinc-client start

    and

    cd /var/lib/boinc
  5. Lastly, we need to configure the project you want to crunch for. This tutorial shows how to connect to WorldCommunityGrid, but the procedure is same for other projects that are supported by BOINC.Get your personal account key from your WorldCommunityGrid profile. Your account key can be found on My Profile page.
  6. Finally, enter the following command to attach the project.
    sudo -u boinc boinccmd --project_attach http://www.worldcommunitygrid.org <Account Key>

    That’s it. BOINC is now crunching on your VPS. There is no graphical interface, so you need to use commands to monitor status and progress.
    For instance, to monitor which work units are downloaded and currently being processed, enter the following  command

    boinccmd --get_tasks

    A full list of commands can be found on BOINC wiki. If you want to stop or restart BOINC client, see on this wiki page for the respective commands.

How to backup MySQL databases with Unix console?

The backup of a MySQL database over a surface like PhPMyAdmin is very simple. If you manage your database through a Unix console, you can easily create a backup using the following command:

mysqldump -u root -p --all-databases > dump.sql

If you changed the default database user when setting up the database, then root must be replaced by it. Otherwise, you could use ‘root’ standardly.

After backup file was created in the folder where you executed the mysqldump, you have the option to copy it to a local machine. Before you need to quit server connection to execute the following command locally:

scp remote_user@:remote_host/home/dump.sql /Users/local_user

Update: Since I was asked how to backup the file data of a website, the best way is to create a compressed file of your website folder executing command

tar zcvf backup.tar.gz my_website_folder/

and then download the compressed folder backup as described above.

Free high-quality sources for self-improvement courses

Lifelong learning is essential in today’s world. In order to be able to keep up with the speed of changes and innovations, one’s own knowledge must be constantly refreshed. But good education is expensive. However, there are many little-known sources
of well-known universities and companies which are freely accessible.

This article is a collection of free courses (and it is to serve as a reminder for me, if I need further education on a topiclater).

MIT Open Course Ware
The source makes the materials used in the teaching of MIT’s subjects available on the web. It provides course materials of various fields.
https://ocw.mit.edu/courses/find-by-topic/#cat=business&subcat=innovation

Standford University offers free course as well on http://online.stanford.edu/courses

Havard University cooperates with MOOC (massive open online course) provider EduX.org to provide free courses from a wide range of topics including Chinese history, archeology and master pieces of world literature. The courses can be audited free or students can choose to receive a verified certificate for a small fee. Courses can be found on https://www.edx.org/school/harvardx

Many other universities around the world also offer such free course. Just browse to their websites and look them up.

Udacity.com
Udacity is another MOOC. Most of the courses are chargeable. But there is also a good number of courses which are free. Since Udacity was found by a former Google employee, it is cooperating with Google and other companies from tech area. Therefore many free courses cover tech topics like mobile and software programming or systems architecture. To find free courses, go to the course catalog on the start site and filter for type “free courses”
https://www.udacity.com/courses/all

The list will be updated with new sources from time to time.

Python: Sorting Algorithm For Multidimentional Key/Value Dictionary

I had to sort a multidimentional dictionary holding several thousands of values on the first level each consisting of one key/value entry on second level, respectively. There are plenty of libraries out there which do the job but I wanted to write one on my own. Here is the result that works very well.

Ascending Order Sorting

a = [{'item3':33}, {'item1':11}, {'item5':55}, {'item4':44}, {'item2':22}]

for i1, e1 in enumerate(a):
	for i2, e2 in enumerate(a):
		if e2 > e1:
			e1 = a[i2]
			a[i2] = a[i1]
			a[i1] = e1
print a
#result: [{'item1': 11}, {'item2': 22}, {'item3': 33}, {'item4': 44}, {'item5': 55}]

Descending Order Sorting

a = [{'item3':33}, {'item1':11}, {'item5':55}, {'item4':44}, {'item2':22}]

for i1, e1 in enumerate(a):
	for i2, e2 in enumerate(a):
		if e2 < e1:
			e1 = a[i2]
			a[i2] = a[i1]
			a[i1] = e1
print a
#result: [{'item5': 55}, {'item4': 44}, {'item3': 33}, {'item2': 22}, {'item1': 11}]

Renaming A Bunch Of Files Using Python

Python is a great progamming language for writing powerful methods with less code. We can use this for useful operations which facilitate our life.

Assuming that we have a folder with hundreds of pictures of our last trip to California. Usually, digital camera pictures have names like DSC100001.png, DSC100002.png, … In our example we want to give them more clearly names like California_Trip_2013_1.png, California_Trip_2013_2.png, … In order to achieve that we write a simple script in python.

import os

i = 0
for filename in os.listdir('.'):
    new_filename = "California_Trip_2013_%s.png" % (i)
    os.rename(filename, new_filename)
    i = i+1
 

As line 5 as a placeholder, we can write some more change operations on file names. Here is a small collection:

new_filename = filename.lower() # make all file names having only lower cases
new_filename = filename.upper() # make all file names having only upper cases
new_filename = filename.capitalize() # make all file names having the first character capitalized
new_filename = filename.swapcase() # make upper cases to lower cases and vice versa

How To Figure Out Location Of Django Packages?

Are you tired off having this standard django admin page style? To figure out where all your django file are located you simply need to type the following command liner into your console/terminal. It outputs the location of your django installation, and all what you need to do is to find the right file and adapt it. Have fun!

python -c "import sys sys.path = sys.path[1:] import django print(django.__path__)"

Java: How to Figure Out the Operating System a User Runs

Java developers often need to adapt some parts of their application (mostly GUI) to the particular operation system (OS) a user runs. Java offers a simple procedure to detect the OS on which the application runs.

Here is a short code snippet to detect the OS in JAVA:

public String getOS() {
    String os = System.getProperty("os.name").toLowerCase();

    if(os.indexOf("mac") >= 0){
       return "MAC";
    }
    else if(os.indexOf("win") >= 0){
       return "WIN";
    }
    else if(os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0){
       return "LINUX/UNIX";
    }
    else if(os.indexOf("sunos") >= 0){
       return "SOLARIS";
    }
    else {
       return "OPERATING SYSTEM IS UNDETECTABLE";
    }
}

MySQL: Fulltext Search – An Example

A fulltext index accelerates the search of tokens noticeble. On a simple LIKE search implementation (i.e. token = %mytoken%) all database rows have to be touched in order to find the token(s). This is not a big deal on a small database. But on big database holding several millions of rows this can cause really big performance problems. Fulltext search is the solution for such cases. If fulltext search is enabled, all fulltext-enabled columns of an inserted entry are indexed. If you search on a indexed colum, only the index is touched, which runs much more faster in search.

This short example shows how to implement a fulltext for MySQL database:

CREATE TABLE blogentry(
entryID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) ,
message text,
fulltext (title, message)
);

In the above example we define that the row title and body are indexed on a new entry insertion. Of course, you can only index one colum or even more than two. But the more colums you index, the more time it needs to search fulltext. So, try to find an appropriate middle course.
A fulltext search is then conducted with the MATCH() AGAINST() command.

SELECT title
FROM blogentry
WHERE MATCH (title, body) AGAINST ('Java Apache Lucene');