Skip to content

Instantly share code, notes, and snippets.

@baniol
baniol / filter_logs.py
Created October 13, 2014 11:57
Filter logs files for regexp & uniq
import re
# open('not_found.log','w').writelines([ line for line in open('allLogs.log') if 'not found' in line])
fname = 'allLogs.log'
with open(fname) as f:
content = f.readlines()
new = [line for line in content if 'not found' in line]
filtered = []
for line in new:
@baniol
baniol / mongodb_backup.md
Last active August 15, 2022 18:58
MongoDB automatic backup

Maintaining even a small mongodb application in production requires regular backups of remotely stored data. MongoDB gives you three ways to acomplish it. In this post I'm using monogodump command for creating a backup and mongorestore for recreating the data. The purpose of this writing is to provide a simple way of periodic database dumps from a remote server to a Dropbox cloud storage.

Remember that for using mongodump you have to have a mongod process running.

Dumping a database

Suppose that you want make a backup of your books database.

To create a dump use mongodump -d books -o which will result in a book folder containing bson files with all collections.

@baniol
baniol / centos_vb.md
Last active July 19, 2021 22:20
Centos on Mac with Virtual Box

If you want to learn how to administer a Linux server or have a need to test your application on a specific Linux distribution, a good point of start is to virtualize it locally using VirtualBox. This post explains how get CentOS up and running on OS X. Our goal here is to install minimal Linux server and access it for administration via ssh from your Host operating system. We will also enable the Guest system to act like a web server.

If you don't have VirtualBox already go to download page and install it.

To get an image of CentOS go here and choose the version you want (I went for 6.5) by clicking on x86_64 then pick one of the mirror servers for downloading the desired image (my choice was CentOS-6.5-x86_64-minimal.iso).

Instalation steps.

Run VirtualBox

  • Clik New.
@baniol
baniol / node_production.md
Created October 5, 2014 07:39
Node on Production

Running a node application on a local machine is easy - you just execute node app.js or, if you want to ease the development process, you use nodemon or forever. Things get a little bit more complicated when you decide to go live with the app and want it stable on the production server. This post describes how to get your application running as a service on CentOS Linux using init scripts and monit for managing and monitoring it.

Register your app as service - init.d

Let's say you have a nodejs application located on your server in /var/www/myapp and the main script is index.js.

First thing to do is to add your app to init scripts. To do so, create a new file in /etc/init.d and name it after your application.

<!doctype html>
<html>
<head>
<script src='//code.jquery.com/jquery-1.7.2.min.js'></script>
<!-- <script src='//localhost:3000/socket.io/socket.io.js'></script> -->
<script src='//baniowski.net:3000/socket.io/socket.io.js'></script>
<script>
// var socket = io.connect('//localhost:3000');
var socket = io.connect('//baniowski.net:3000');
@baniol
baniol / gcd.js
Created September 15, 2014 06:36
Greatest Common Divisor, recursive & iterative
// recursive
var gcd = function(a, b) {
if ( ! b) {
return a;
}
return gcd(b, a % b);
};
// iterative
@baniol
baniol / git_del_tag
Created September 2, 2014 07:16
GIt - delete a tag
git tag -d 12345
git push origin :refs/tags/12345
@baniol
baniol / ssl.js
Created June 18, 2014 07:36
nodejs ssl certificate
// from: http://www.hacksparrow.com/node-js-https-ssl-certificate.html
$ openssl genrsa -out hacksparrow-key.pem 1024
$ openssl req -new -key hacksparrow-key.pem -out certrequest.csr
... bunch of prompts
$ openssl x509 -req -in certrequest.csr -signkey hacksparrow-key.pem -out hacksparrow-cert.pem
var https = require('https');
var fs = require('fs');
@baniol
baniol / mongo_dump.sh
Created June 10, 2014 18:21
mongodb dump & dropbox upload, backup
#!/usr/bin/env bash
#Get current date
NOW="$(date +'%d-%m-%Y_%H-%M')"
# Settings:
# Path to a temporary directory
DIR=/root/mongodb_dump/