2022-12-31

[Python] Nesting LIST inside DICT / Nesting DICT inside LIST / Nesting DICT inside DICT

#!/usr/bin/env python3

# ---------------------------------------
# Project Name: PythonCheatSheet
# File Name: Dict15-01-Nested-Dict.py
# Created Date : 2022-12-31 08:56:51 UTC+7
# Last Modified: 2022-12-31 09:09:29 UTC+7
# ---------------------------------------
import json
from pytictoc import TicToc
t = TicToc()  # create TicToc instance
t.tic()       # Start timer
print(f"================================")
print(f'---------------------')
print(f'# Nesting LIST inside DICT:')
programming_language = {
    'Elshad': ['Python', 'Java', 'C#'],
    'Renad': 'Scratch',
    'Edy': 'Java',
}
print(f'programming_language:')
print(json.dumps(programming_language,
                 sort_keys=False,
                 indent=4,
                 separators=(',', ': ')
                 ))


print(f'\n---------------------')
print(f'# Nesting DICT inside LIST:')
programming_language = [
    {
        'user_name': 'Elshad',
        'favorite_language': ['Python', 'Java', 'C#'],
        'experience': 10,
    },
    {
        'user_name': 'Renad',
        'favorite_language': ['Scratch', 'Python'],
        'experience': 2
    },
]
print(json.dumps(programming_language,
                 sort_keys=False,
                 indent=4,
                 separators=(',', ': ')
                 ))


print(f'\n---------------------')
print(f'# Nesting DICT inside DICT:')
programming_language = {
    'Elshad': {
        'favorite_language': ['Python', 'Java', 'C#'],
        'experience': 10,
    },
    'Renad': {
        'favorite_language': ['Scratch', 'Python'],
        'experience': 2,
    }
}
print(f'programming_language:')
print(json.dumps(programming_language,
                 sort_keys=False,
                 indent=4,
                 separators=(',', ': ')
                 ))
print(f"================================")
t.toc() # Print elapsed time: "Elapsed time is <xxx> seconds."

# RESULT:
"""
# ================================
# ---------------------
# # Nesting LIST inside DICT:
# programming_language:
# {
#     "Elshad": [
#         "Python",
#         "Java",
#         "C#"
#     ],
#     "Renad": "Scratch",
#     "Edy": "Java"
# }

# ---------------------
# # Nesting DICT inside LIST:
# [
#     {
#         "user_name": "Elshad",
#         "favorite_language": [
#             "Python",
#             "Java",
#             "C#"
#         ],
#         "experience": 10
#     },
#     {
#         "user_name": "Renad",
#         "favorite_language": [
#             "Scratch",
#             "Python"
#         ],
#         "experience": 2
#     }
# ]

# ---------------------
# # Nesting DICT inside DICT:
# programming_language:
# {
#     "Elshad": {
#         "favorite_language": [
#             "Python",
#             "Java",
#             "C#"
#         ],
#         "experience": 10
#     },
#     "Renad": {
#         "favorite_language": [
#             "Scratch",
#             "Python"
#         ],
#         "experience": 2
#     }
# }
# ================================
# Elapsed time is 0.003771 seconds.
"""

2022-02-18

OpenSSL Generate SSL CERT FOR IIS (PFX) from PEM

#!/bin/bash
#/etc/haproxy/certs/ssl-convert-PEM-2-PFX.sh

###################################################
#LastUpdate: #7:59 2022.02.18
###################################################
#__________[GLOBAL_VAR]:BEGIN
Domain_Name="
example.com"

SSL_CRT_From_Provider="star_
example.com.crt"
SSL_CA_From_Provider="DigiCertCA.crt"
SSL_CA_Bundle_Provider="My_CA_Bundle.crt"
SSL_CERT_PRIVATE="003-1-STAR_.
example.com
-PRIVATE.key"

SSL_CERT_PEM_FORMAT="$Domain_Name-wildcard-2021.10.03.pem"

#__________[GLOBAL_VAR]:END

openssl \
pkcs12 \
-inkey $SSL_CERT_PRIVATE \
-in $SSL_CRT_From_Provider \
-certfile $SSL_CA_Bundle_Provider \
-export -out $Domain_Name-wildcard-2021.10.03.pfx


echo "VERIFY SSL CERT:"
openssl pkcs12 -info -in $Domain_Name-wildcard-2021.10.03.pfx

#THE_END
#RESULT: example.com-wildcard-2021.10.03.pfx





REF:
1:
#https://www.sslshopper.com/ssl-converter.html
#Convert PEM to PFX:
#openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in 
certificate.crt -certfile CACert.crt

2:
STEP BY STEP USING LETSENCRYPT TO MAKE SSL CERT FOR IIS - MANUAL

2022-01-06

TableAU Backup and Restore

#!/bin/bash
#############################################
# @CreatedDate: #13:39 2022.01.04
# @LastUpdate : #12:46 2022.01.06
# @Last Modified by: iadmin
#############################################
#cat /opt/script/tableau-backup.sh | grep LastUpdate
#############################################
#__________TIME_START:BEGIN
time_start=`date +%s.%3N`
#__________TIME_START:END
#############################################
#############################################
#__________TIME_CALCULATE:BEGIN
#now1: 2022.01.06-11.55.40.168
now1="$(date +'%Y.%m.%d-%H.%M.%S.%3N')"

CURRENT_YEAR=$(date +'%Y'); CURRENT_MONTH=$(date +'%m'); CURRENT_DATE=$(date +'%d')
CURRENT_HOUR=$(date +'%H'); CURRENT_MINUTE=$(date +'%M'); CURRENT_SECOND=$(date +'%S'); CURRENT_MILISECOND=$(date +'%3N');
#YYYY_MM: 2022-01
YYYY_MM="$CURRENT_YEAR-$CURRENT_MONTH"

#YYYY_MM_DD: 2022-01-06
#YYYY_MM_DD="$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE"

#YYYY_MM_DD: 20220106
YYYY_MM_DD="$CURRENT_YEAR""$CURRENT_MONTH""$CURRENT_DATE"

#HH_MM_SS: 11:55:40.206
#HH_MM_SS="$CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND.$CURRENT_MILISECOND"

#HH_MM_SS: 11.55.40.206
HH_MM_SS="$CURRENT_HOUR.""$CURRENT_MINUTE.""$CURRENT_SECOND.""$CURRENT_MILISECOND"

# echo "YYYY_MM: $YYYY_MM"
# echo "YYYY_MM_DD: $YYYY_MM_DD"
# echo "HH_MM_SS: $HH_MM_SS"
# echo "now1: $now1"
#__________TIME_CALCULATE:END

(
#__________BACKUP_PROCESSING:BEGIN
TableAU_Backup_LOC="/opt/bk/TableAU";
mkdir -p $TableAU_Backup_LOC; cd $TableAU_Backup_LOC

echo ""
echo "Go to backup date folder: [$TableAU_Backup_LOC/$YYYY_MM_DD]"
mkdir -p $YYYY_MM_DD; cd $YYYY_MM_DD

Config_File="config-$YYYY_MM_DD-$HH_MM_SS.json"
Data_File="data-$YYYY_MM_DD-$HH_MM_SS.tsbak"

echo ""
echo "Backup Config: [tsm settings export -f config $Config_File]"
#tsm settings export --output-config-file <path/to/output_file.json> [global options]
#tsm settings export -f config "$Config_File"
tsm settings export --output-config-file "$Config_File"

echo ""
echo "Backup Data: [tsm maintenance backup -f $Data_File]"
#tsm maintenance backup --file <backup_file> [options] [global options]
tsm maintenance backup --file "$Data_File"

echo ""
echo "Moving backup data to Backup Folder [$TableAU_Backup_LOC/$YYYY_MM_DD]"
mv /var/opt/tableau/tableau_server/data/tabsvc/files/backups/* $TableAU_Backup_LOC/$YYYY_MM_DD

md5sum "$Data_File" > "$Data_File".md5
#__________BACKUP_PROCESSING:END


#______________________DELETE_OLD_DATA:BEGIN
cd $PATH_DST
NumberOfFileWantToKeep=14
#Giu lai [14] file moi nhat trong thu muc [$FOLDER_DST]:
#XOA FILE:
#/bin/rm -rf `ls -t "20"* | awk 'NR>7'`

#XOA FOLDER:
cd $TableAU_Backup_LOC
/bin/rm -rf `ls -td */ | awk 'NR>14'`
#______________________DELETE_OLD_DATA:END

#______________________FREE_RAM:BEGIN
free && sync && echo 3 > /proc/sys/vm/drop_caches && free
free
# printf "RAM FREE: \n $(free -h)\n" >> $DB_RESULT.log
#______________________FREE_RAM:END

#############################################
#############################################
#__________TIME_END:BEGIN
time_end=`date +%s.%3N`
execution_time=$( echo "$time_end - $time_start" | bc -l -l );
echo "time_start: [$time_start = $(date -d@$time_start +'%Y.%m.%d-%H.%M.%S.%3N')]"
echo "time_end : [$time_end = $(date -d@$time_end +'%Y.%m.%d-%H.%M.%S.%3N')]"
echo "runtime : [$execution_time] (second.milliseconds)"
#__________TIME_END:END
) 2>&1 | tee backup-job-$YYYY_MM_DD-$HH_MM_SS.log


#THE_END:
# #Restore Config:
# #https://help.tableau.com/current/server/en-us/cli_settings_tsm.htm#TSMSet
# tsm settings import --import-config-file config-2022.01.04-17h40.json

# #Restore Data:
# #https://help.tableau.com/current/server/en-us/upgrade_migrate.htm
# chown -R tableau.tableau /opt/temp/bi-2021.01.04-17h40-2022-01-04.tsbak
# tsm maintenance restore -f /opt/temp/bi-2021.01.04-17h40-2022-01-04.tsbak


#####################
# root@srv032:/opt/bk/TableAU/20220106# /opt/script/tableau-backup.sh

# Go to backup date folder: [/opt/bk/TableAU/20220106]

# Backup Config: [tsm settings export -f config config-20220106-12.33.43.768.json]
# Configuration file written to /opt/bk/TableAU/20220106/config-20220106-12.33.43.768.json.

# Backup Data: [tsm maintenance backup -f data-20220106-12.33.43.768.tsbak]
# Job id is '25', timeout is 1440 minutes.
# 6% - Starting the Active Repository instance, File Store, and Cluster Controller.
# 13% - Waiting for the Active Repository, File Store, and Cluster Controller to start.
# 20% - Installing backup services.
# 26% - Estimating required disk space.
# 33% - Gathering disk space information from all nodes.
# 40% - Analyzing disk space information.
# 46% - Checking if sufficient disk space is available on all nodes.
# 53% - Backing up configuration.
# 60% - Backing up object storage data.
# 66% - Backing up database.
# 73% - Backing up asset keys.
# 80% - Assembling the tsbak archive.
# 86% - Stopping the Active Repository if necessary.
# 93% - Waiting for the Active Repository to stop if necessary.
# 100% - Uninstalling backup services.
# Backup written to '/var/opt/tableau/tableau_server/data/tabsvc/files/backups/data-20220106-12.33.43.768.tsbak' on the controller node.

# Moving backup data to Backup Folder [/opt/bk/TableAU/20220106]
# time_start: [1641447223.762 = 2022.01.06-12.33.43.762]
# time_end : [1641447421.623 = 2022.01.06-12.37.01.623]
# runtime : [197.861] (second.milliseconds)
# root@srv032:/opt/bk/TableAU/20220106#
#####################