Monday, May 30, 2016

Sublime Text 3 - Show whitespace and tabs on editor

I still learning how to use VIM but my preferable editor stilll Sublime Text FTW! I wrote some bash script today and whitespace are too sensitive for unix. Let's handle it properly, okey?

1) Open Sublime Text editor
2) Click on "Preference"
3) Choose "Setting - user"
4) Now, add "draw_white_space": "all" after the curly braces (put on tops of other setting)
5) Press Ctrl+S
6) Done! Now tabs show up as dashes, while spaces are dots.

That all for now..Wasssalam ~

Software Wars!

Thursday, May 26, 2016

Installing NodeJS 4.4.* (LTS) on Fedora 23

I was switch to use Ubuntu 16.04 LTS about 3 week ago, seem APT still suck to manage repos and pulling to much data bandwidth with deb compressing format. Now I switch back to Fedora 23 and I missed DNF so much..LOL, enough with the unnecessary chit-chat.

When I using the official Fedora repo, seem version of nodejs a to far behind and I prefer to use LTS version. Thank god, I found someone make a COPR repo for nodejs
$ sudo dnf copr enable voor/nodejs-lts
$ sudo dnf install nodejs
Yeah, Awesome...

Update : Hmm.. now switching to NVM (Node Version Manager)

Tuesday, May 24, 2016

Unix - View JSON format in terminal

I learning NodeJS and I play a lot with JSON formated data. It realy mess-up when you want to curl any JSON ouput into terminal. My solution is to use json package via npm :
$ sudo npm install -g json

Let's check some example :
$ curl -X GET "https://jakim-esolat.herokuapp.com/prayer-times?zone_id=JHR04&start_date=2016-05-24&end_date=2016-05-25" | json
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   407    0   407    0     0    352      0 --:--:--  0:00:01 --:--:--   352
{
  "metadata": {
    "generated_at": "2016-05-24 15:58:06",
    "total_result": 2,
    "zone": {
      "id": "JHR04",
      "name": "Batu Pahat, Muar, Segamat, Gemas"
    }
  },
  "data": [
    {
      "date": "2016-05-24",
      "imsak": "5:28",
      "subuh": "5:38",
      "syuruk": "6:59",
      "zohor": "13:08",
      "asar": "16:32",
      "maghrib": "19:14",
      "isyak": "20:28"
    },
    {
      "date": "2016-05-25",
      "imsak": "5:28",
      "subuh": "5:38",
      "syuruk": "6:59",
      "zohor": "13:08",
      "asar": "16:32",
      "maghrib": "19:14",
      "isyak": "20:28"
    }
  ]
}

NodeJS - npm via proxy

Add proxy :
$ npm config set proxy http://proxy.uthm.edu.my:8080
$ npm config set https-proxy http://proxy.uthm.edu.my:8080

Remove proxy (unix) :
$ npm config rm proxy
$ npm config rm https-proxy
$ unset HTTP_PROXY
$ unset HTTPS_PROXY
$ unset http_proxy
$ unset https_proxy

Remove proxy (Windows) :
npm config delete proxy
npm config delete https-proxy

Saturday, May 21, 2016

Solved - Server unable to read htaccess file, denying access to be safe

I create a custom virtual host on my apache for my current web project which is I called it as "manja.dev" and directed/located at my dropbox folder /home/krip-hensem/Dropbox/Coding/manja-dev, let have a look at our site config file :
$ cat /etc/apache2/sites-available/manja.dev.conf 

        <VirtualHost *:80>
            ServerAdmin [email protected]
            ServerName eSPTRBA.dev
            ServerAlias eSPTRBA.dev
            DocumentRoot /home/krip-hensem/Dropbox/Coding/manja-dev
            <Directory />
                AllowOverride All
            </Directory>
            <Directory /home/krip-hensem/Dropbox/Coding/manja-dev>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride all
                Require all granted
            </Directory>
            ErrorLog /var/log/apache2/manja.dev-error.log
            LogLevel error
            CustomLog /var/log/apache2/manja.dev-access.log combined
        </VirtualHost>

But then, I got an error message that telling :
"Forbidden
You don't have permission to access / on this server.
Server unable to read htaccess file, denying access to be safe

Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request."

The heck! I tried to chmod .htaccess file with 644 permission. But nothing happen. It getting my nerve, so I check the error log :
$ cat /var/log/apache2/manja.dev-error.log
[Sat May 21 03:03:06.126741 2016] [core:crit] [pid 13917] 
(13)Permission denied: [client 127.0.0.1:45206] 
AH00529: /home/krip-hensem/Dropbox/.htaccess pcfg_openfile: 
unable to check htaccess file, ensure it is readable and that '/home/krip-hensem/Dropbox/' is executable

Auww.. This is kacang! Just need to let the *bold* folder executable, just ran an chmod command :
$ sudo chmod 755 /home/krip-hensem/Dropbox/
Tango! The problem just solved! Alhamdulillah..

Wednesday, May 18, 2016

How to use - Google doc to embed and view on your website

<iframe style="width: 100%; height: 500px;"
src="http://docs.google.com/gview?url=< YOUR PDF FILE LOCATION HERE>&amp;embedded=true" 
width="300" height="150" frameborder="0"></iframe>

Saturday, May 14, 2016

Vagrant - The guest additions on this VM do not match the installed version of VirtualBox!

Each time when I ran vagrant up, I will get this annoying message:
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.3.28
default: VirtualBox Version: 5.0
==> default: Setting hostname...

My VM image using the older VirtualBox Guest Additions tool than my Oracle VirtualBox installed on my machine. This cause my shared folder unable to mount and sync correctly. Now, let upgrade our tool:


1. Mount VBoxGuestAdditions.iso as optical disk drive (located at Oracle Virtualbox installed folder)

2. Run VM via Oracle Virtualbox or SSH inside our VM and install the linux kernel headers and the basic developer tools
$ sudo apt-get install linux-headers-$(uname -r) build-essential dkms

3. Run the shell script on your VM to install the guest additions.
$ sudo sh /media/cdrom/VBoxLinuxAdditions.run

Now restart / halt you VM. Next time when booting, the vagrant doesnt give the annoying message anymore and Oracle VirtualBox should able to mount the shared folder correctly.

Wednesday, May 11, 2016

Powershell - View wifi password using psget module

> (new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex
> Install-Module WiFi-Password
> Show-WiFiPassword
SSID : BAU_KAMBING_WIFI 
Password : 1122XX3344YY556677ZZ8899 
Auth type : WPA2-Personal

Sunday, May 8, 2016

Friday, May 6, 2016

MSSQL - Search string in all table on a database via query declaration

USE YOUR_DATABASE_NAME

DECLARE @SearchStr nvarchar(100) = 'STRING_YOU_WANT_TO_FIND'
DECLARE @Results TABLE (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SET  @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL

BEGIN
    SET @ColumnName = ''
    SET @TableName = 
    (
        SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
        FROM     INFORMATION_SCHEMA.TABLES
        WHERE         TABLE_TYPE = 'BASE TABLE'
            AND    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
            AND    OBJECTPROPERTY(
                    OBJECT_ID(
                        QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                         ), 'IsMSShipped'
                           ) = 0
    )

    WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)

    BEGIN
        SET @ColumnName =
        (
            SELECT MIN(QUOTENAME(COLUMN_NAME))
            FROM     INFORMATION_SCHEMA.COLUMNS
            WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                AND    TABLE_NAME    = PARSENAME(@TableName, 1)
                AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
                AND    QUOTENAME(COLUMN_NAME) > @ColumnName
        )

        IF @ColumnName IS NOT NULL

        BEGIN
            INSERT INTO @Results
            EXEC
            (
                'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
                FROM ' + @TableName + ' (NOLOCK) ' +
                ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
            )
        END
    END    
END

SELECT ColumnName, ColumnValue FROM @Results

MSSQL - Search string in all tables on a database via stored procedure

Query to create programmability stored procedure :
USE [YOUR_DB_NAME]
GO

CREATE PROC SearchAllTables
(
    @SearchStr nvarchar(100)
)
AS
BEGIN

    CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

    SET NOCOUNT ON

    DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
    SET  @TableName = ''
    SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

    WHILE @TableName IS NOT NULL

    BEGIN
        SET @ColumnName = ''
        SET @TableName =
        (
            SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
            FROM     INFORMATION_SCHEMA.TABLES
            WHERE         TABLE_TYPE = 'BASE TABLE'
                AND    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
                AND    OBJECTPROPERTY(
                        OBJECT_ID(
                            QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                             ), 'IsMSShipped'
                               ) = 0
        )

        WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)

        BEGIN
            SET @ColumnName =
            (
                SELECT MIN(QUOTENAME(COLUMN_NAME))
                FROM     INFORMATION_SCHEMA.COLUMNS
                WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                    AND    TABLE_NAME    = PARSENAME(@TableName, 1)
                    AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
                    AND    QUOTENAME(COLUMN_NAME) > @ColumnName
            )

            IF @ColumnName IS NOT NULL

            BEGIN
                INSERT INTO #Results
                EXEC
                (
                    'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)
                    FROM ' + @TableName + ' (NOLOCK) ' +
                    ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
                )
            END
        END   
    END

    SELECT ColumnName, ColumnValue FROM #Results

END


To start searching, please create new query tab and put this code
USE [YOUR_DB_NAME]
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[SearchAllTables]
  @SearchStr = N'STRING_YOU_WANT_TO_FIND'

SELECT 'Return Value' = @return_value

GO

Thursday, May 5, 2016

Fixed - Unable to open the physical file (attach) on MSSQL server

Problem :
Msg 5120, Level 16, State 101, Line 1 Unable to open the physical file "C:\beruk\mung\Databases.mdf". Operating system error 5: "5(Access is denied.)".

Solution :
  1. Press Win+R (Run dialog)
  2. Type services.msc on open dialog and press OK
  3. Search service name of SQL Server (MSSQLSERVER) or SQL Server (SQLEXPRESS) depending on you server instance name.
  4. Right click > Properties
  5. Click on Log On tab menu
  6. Select Local Account and press OK
  7. Right click on service name again and choose Stop
  8. Right click on service name again and choose Start
Now, open SQL Server 20** management studio and login as usual, try to attach you database. It should worked as expected.  

Tuesday, May 3, 2016

MSSQL - Check Microsoft SQL server version information



Peace be upon you, I tried to copy database from network server to localhost server and the operation are failed and it give me notice that said:
The destination server cannot be a SQL Server 2005 or later Express instance.
From the error message, we know that my MSSQL server on my network using old MSSQL version which is 2005. But how to check it by yourself? It is very simple, just open new query and type SELECT @@VERSION, the you will get the MYSSQL server version information..wo0t! Just a simple knowledge sharing here, I hope it useful for us. Wassalam~

Note : If you get message just like I quote above, it mean you can't copy database but you can export to *.mdf and *.ldf file via right click on database > "task" > "export" as you working solution :)

Monday, May 2, 2016

Linux - Check which network card are used to connect internet network

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.8.223.254    0.0.0.0         UG    600    0        0 wlp2s0
10.8.216.0      0.0.0.0         255.255.248.0   U     600    0        0 wlp2s0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 virbr0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.240.99  10.8.223.254    255.255.255.255 UGH   600    0        0 wlp2s0

MSSQL - SELECT DATE BETWEEN

SELECT [Code_no]
      ,[User_RefNo]
      ,[Loan_No]
      ,[Created_Date]
      ,[Created_By]
      ,[Tax]
FROM [DB_name].[dbo].[Table_name]
WHERE [Created_Date] BETWEEN '2016-04-18' AND '2016-04-20 23:59:59.999'
ORDER BY [Created_Date] asc

Sunday, May 1, 2016

Database disaster prevention by Olek Nilson (Microsoft SQL)

Disclamer: This article was taken from DineSQL blog and this post is written by Olek Nilson. All credit goes to them.


Database disaster prevention is one of the greatest pains in life of every DBA. Data from database are changing daily, gradually expanding the size of the latter. Of course, there is no perfect way to prevent database corruption, but there are some practical steps that can help you to bring the risk to minimum.

It’s important to say a few words about Microsoft Corporation. In spite of the fact that SQL Server is 100% Microsoft product, the company does not provide any means to protect your database, meaning that database protection completely up to the user.

Below we would like to discuss cases that can trigger database disaster.

One of the most popular cases that can potentially cause database corruption is hardware failure, for example, incorrect work of Disk, CPU or Memory Module.

Another thing that can corrupt your database is Antivirus software. Please note that after your antivirus software is installed, all SQL Server Database Files should be excluded from being scanned by the Antivirus software.

The next issue that can potentially damage your database is a desire to save free space on the local disk. Based on my personal experience, there were some users who stored all SQL Server database files in zip or 7zip files to save some space on their disks. Ultimately, these actions lead to a database failure. If you desperately need free space, you can use third party tool to make database backups with compression and store them online.

Another important thing that will help you to prevent database disaster is setting up the latest updates for SQL Server and Windows. Otherwise it will cause incorrect working of your shift.  

And do not forget about the case with a power failure that may lead to all databases being corrupted.

It is not an exhaustive list of failures that can destroy your database. So what shall we do to prevent database disasters and minimize the risk? You have to remember that there is no perfect way or method that will ensure 100% protection for your database. However, the best thing that can help you to minimize the risk of the database corruption is to make scheduled database backups. Database backups cannot prevent the corruption, but they can help you to restore the database with minimal losses.   

It is recommended that all experienced DBAs should make regular database backups. This leads to a question  – How often should I make backups or what backup strategies should I employ? Before answering this question, please consider yet another question - How much data can you afford to lose? Working as a DBA, I have had a great many databases to restore and I can say that the more quality backups you have the less data you will eventually lose. 

Presently there are three ways to make database backups:
  1. Using T-SQL commands:

    If you prefer using T-SQL command you have to keep in mind that you need to make database backups all the time. Even you have to create a schedule and carefully stick to it. Perhaps, make backups with the help of T-SQL commands are comfortable if the database is not large and grows slowly. But in the most cases changes in the database are taking place all day and night, so how do make backups at night? Of course, to solve this issue you can find in the Web scripts for making schedule backups.

    So, if you make the decision to make database backups with the help of T-SQL use the following commands:
    -- To make a full database backup use the following command:
    BACKUP DATABASE AdventureWorks TO DISK = 'Z:\SQLServerBackups\AdventureWorks.bak'
    
    -- Use the following command to make a differential backup:
    BACKUP DATABASE AdventureWorks TO DISK = 'Z:\SQLServerBackups\AdventureWorksDiff.bak' WITH DIFFERENTIAL
    
    -- The following command will help you to make a transaction log backup:
    BACKUP LOG AdventureWorks TO DISK = 'Z:\SQLServerBackups\AdventureWorksLog.bak'
    
  2. With the help of SSMS (SQL Server Management Studio):

    Using SSMS for making backups is a little bit easier then make backups with the help of T-SQL commands. You don't need to write commands every time when you are going to make a backup it is enough to make a couple simple steps, which we described below. It has to be admitted that you can also make scheduled backups with the help of SSMS, but it will take more efforts from you.

    To make a backup please:
    a. Right-click on the database you want to backup
    b. Select “Tasks”, then “Back up…”
    c. Choose “Backup type"
    d. Add backup destination
    e. Click “OK”
  3. Or use third party tools; You can choose such enterprise solutions as SQL Backup Pro from RedGate, something similar to SqlBackupAndFtp, or try out Sqlbak.com if you prefer SAAS model.
In my opinion, this is the best way for making scheduled database backups. Here everything you need is to create the job for the scheduled backups and run the application. The third party tool will make backups according to the schedule.

     So, if your databases are growing fast and there are many changes occurring every hour, it would be better to choose “maniacal” backup plan, for example, it is a Full database backup every 24 hours, a Differential backup every three hours and make transaction log backups every 15 minutes. Yes, perhaps it will take more time to restore your database should failure occur, but the percentage of data loss will be minimal.