瀏覽代碼

public samples

kalxasus 6 年之前
當前提交
798003ddf0

+ 32 - 0
backup/backup-functions/clean.sh

@@ -0,0 +1,32 @@
+function clean_dir_by_file_num() {
+    local err=0; local target=$1; local count=$2
+    test $# -eq 2 || { show_error "Wrong usage of the function! Args=$@" "$FUNCNAME"; return 1; }
+    total=`ls -l $target | grep '^-' | wc -l`
+    to_delete=$[${total} - ${count}]
+    test $to_delete -gt 0 && {
+            for var in `ls -ltr $target | grep '^-' | head -n $to_delete | awk '{print $9}'`; do
+                    test -f $target/$var || { show_error "File $target/$var doesn't exist or it's not a file!" "$FUNCNAME";}
+                    show_notice "Deleting $target/$var ..." "$FUNCNAME"
+                    rm -rf $target/$var 2>&1 || { show_error "Error on del $target/$var" "$FUNCNAME"; }
+            done
+    }
+
+    return $err;
+}
+
+function clean_dir_by_dir_num() {
+    local err=0; local target=$1; local count=$2
+    test $# -eq 2 || { show_error "Wrong usage of the function! Args=$@" "$FUNCNAME"; return 1; }
+    total=`ls -l $target | grep '^d' | wc -l`
+    to_delete=$[${total} - ${count}]
+    test $to_delete -gt 0 && {
+            for var in `ls -ltr $target | grep '^d' | head -n $to_delete | awk '{print $9}'`; do
+                    test -d $target/$var || { show_error "File $target/$var doesn't exist or it's not a file!" "$FUNCNAME";}
+                    show_notice "Deleting $target/$var ..." "$FUNCNAME"
+                    rm -rf $target/$var 2>&1 || { show_error "Error on del $target/$var" "$FUNCNAME"; }
+            done
+    }
+
+    return $err;
+}
+

+ 26 - 0
backup/backup-functions/gdrive.sh

@@ -0,0 +1,26 @@
+gdfver=1.02
+
+function gdrive_upload() {
+        local err=0; local target=$1; local current_date=$2;
+        which drive > /dev/null 2>&1 || { show_error 'Install drive first! (ppa:twodopeshaggy/drive)' "$FUNCNAME"; return 1; }
+        test $# -eq 2 || { show_error "Wrong usage of the function! Args=$@" "$FUNCNAME"; return 1; }
+        show_notice "Upload $source to $target/$current_date ..." "$FUNCNAME"
+        test "x`drive stat -quiet $target 2>&1`" != "x" && drive push -quiet $target
+        drive push -quiet $target/$current_date 2>&1 || { show_error "Error on drive push $target/$current_date" "$FUNCNAME"; local err=1; }
+        return $err;
+}
+
+function gdrive_clean() {
+        local err=0; local target=$1; local count=$2
+        which drive > /dev/null 2>&1 || { show_error 'Install drive first! (ppa:twodopeshaggy/drive)' "$FUNCNAME"; return 1; }
+        test $# -eq 2 || { show_error "Wrong usage of the function! Args=$@" "$FUNCNAME"; return 1; }
+        total=`drive ls $target | wc -l`
+        to_delete=$[${total} - ${count}]
+        test $to_delete -gt 0 && {
+                for var in `drive ls $target | xargs basename -a | sort | head -n $to_delete`; do
+                        show_notice "Deleting $target/$var ..." "$FUNCNAME"
+                        drive del --quiet $target/$var 2>&1 || { show_error "Error on drive del $target/$var" "$FUNCNAME"; local err=1; }
+                done
+        }
+}
+

+ 17 - 0
backup/backup-functions/mysql.sh

@@ -0,0 +1,17 @@
+mysql_dump_name='all_databases'
+
+function mysql_xtra_backup() {
+        local err=0; local dir=$1;
+        which innobackupex > /dev/null 2>&1 || { show_error "No innobackupex installed!" "$FUNCNAME"; return 1; }
+        test -f "/root/.my.cnf" || { show_error "No /root/.my.cnf file with access credintials!" "$FUNCNAME"; return 1; }
+        test $# -eq 1 || { show_error "Wrong usage of the function! Args=$@" "$FUNCNAME"; return 1; }
+        test -d $dir/$mysql_dump_name || mkdir -p $dir/$mysql_dump_name
+        show_notice "Dumping database $db ..." "$FUNCNAME"
+        {
+                innobackupex --no-timestamp --rsync $dir/$mysql_dump_name && \
+                innobackupex --no-timestamp --rsync --apply-log $dir/$mysql_dump_name && \
+                nice -n 19 ionice -c 3 tar -cf $dir/$mysql_dump_name.tar.$compress_ext --use-compress-prog="$compress_prog" $tar_opts $dir/$mysql_dump_name && \
+                test -d $dir/$mysql_dump_name && rm -rf $dir/$mysql_dump_name
+        } || { show_error "Error on dump databuses with innobackupex" "$FUNCNAME"; local err=1; }
+        return $err;
+}

+ 49 - 0
backup/custom.backup-template

@@ -0,0 +1,49 @@
+#!/bin/bash
+
+. /usr/local/include/backup-functions/main.sh
+. /usr/local/include/backup-functions/gdrive.sh
+. /usr/local/include/backup-functions/config.sh
+
+type='Daily'
+
+backup_dir='/root/Gdrive/Backup/Notebook'
+num_of_backup=5
+local_days=5
+
+tar_opts+=' -h'
+tar_exclude='--exclude=*.iso --exclude=*.jpg --exclude=*.zip --exclude=*.rar --exclude=*.7z --exclude=s100/ --exclude=repos/'
+
+which flock > /dev/null || { show_error "No flock installed. You need to install flock first."; exit 1;}
+{
+    flock -n 9 || { show_error "Another copy of ${0##*/} is still running on $myhostname. Lock file $lock_file locked.\n"; exit 1; }
+    test -f $log_file && mv $log_file $log_file.1
+    {
+        if [ `date +%u` = "7" ]; then type='Weekly'; num_of_backup=4; local_days=1; fi
+        if [ `date +%d` = "01" ]; then type='Monthly'; num_of_backup=6; local_days=1; fi
+
+        show_notice "Backup started."
+
+        mkdir -p $backup_dir/$type/$current_date
+
+        check_free_space $backup_dir && {
+
+            compress_cfgs $backup_dir/$type/$current_date
+            compress_dir /home/neo/.Desktop $backup_dir/$type/$current_date/Desktop.tar.$compress_ext
+            compress_dir /home/neo/Документы $backup_dir/$type/$current_date/Documents.tar.$compress_ext
+
+            check_size $backup_dir/$type/$current_date 500M && \
+            gdrive_upload $backup_dir/$type $current_date && \
+            gdrive_clean $backup_dir/$type $num_of_backup && \
+            clean_dir $backup_dir/$type $local_days
+
+        save_backup_size $backup_dir/$type/$current_date
+        }
+
+        show_notice "Backup ended."
+
+        } >> $log_file 2>> >(tee -a $log_file | grep -vE "$stderr_exclude" >&2)
+        test $err -eq 1 && { show_error "Script ${0##*/} failed. Check logs below:\n$( cat $log_file )"; }
+} 9> $lock_file
+
+exit $err;
+

+ 92 - 0
databases/mysql/auto_restore_from_backup

@@ -0,0 +1,92 @@
+#!/bin/bash
+armb_ver='4_090617'
+
+log_file=/var/log/${0##*/}.log
+lock_file=/tmp/${0##*/}.lock
+
+dumps_dir='/var/backups/mysql/daily_from_vs05'
+mysql_data_dir='/var/lib/mysql'
+
+db_name=''
+db_user=''
+db_pass=''
+db_connection_host=''
+
+mysql_bin="`which mysql` -u root -h localhost -p`cat /root/.mysql`"
+latest_dump=`find $dumps_dir -maxdepth 1 -type f -name "*.snapshot.sql.*" -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f 2`
+
+function update_date() {
+    test -f $latest_dump || { echo "`date '+%Y/%m/%d:%H:%M:%S'` Error. File $latest_dump doesn't exist!"; return 1; }
+    latest_date=`date '+%Y-%m-%d %H:%M:%S' -r $latest_dump`
+    echo "`date '+%Y/%m/%d:%H:%M:%S'` Starting update date in table."
+    $mysql_bin -B -N -e "UPDATE db_date SET mydate=\"${latest_date}\";" $db_name && \
+    echo "`date '+%Y/%m/%d:%H:%M:%S'` Update date in table finished." || \
+    { echo "`date '+%Y/%m/%d:%H:%M:%S'` Error. Something go wrong on update date in table."; return 1; }
+}
+
+function init_db() {
+    echo "`date '+%Y/%m/%d:%H:%M:%S'` Starting function init_db"
+    $mysql_bin -B -N -e "USE $db_name; CREATE TABLE db_date (mydate DATETIME); INSERT INTO db_date SET mydate=NOW();" && \
+    echo "`date '+%Y/%m/%d:%H:%M:%S'` Table \"db_date\" created in $db_name. Starting function update_date" && \
+    update_date 
+    echo "`date '+%Y/%m/%d:%H:%M:%S'` Create privileges"
+    $mysql_bin -B -N -e "GRANT ALL PRIVILEGES ON $db_name.* TO db_user@$db_connection_host IDENTIFIED BY \"$db_pass\";"
+}
+
+function stop_mysql() {
+        echo "`date '+%Y/%m/%d:%H:%M:%S'` Starting function stop_mysql"
+        /etc/init.d/mysql stop
+        if pgrep -x mysqld >/dev/null
+                then 
+                        echo "`date '+%Y/%m/%d:%H:%M:%S'` Error. Something go wrong, Mysql process still running!!!"
+                        return 1
+        fi
+}
+
+function start_mysql() {
+        echo "`date '+%Y/%m/%d:%H:%M:%S'` Starting function start_mysql"
+        /etc/init.d/mysql start
+        if ! pgrep -x mysqld >/dev/null
+                then 
+                        echo "`date '+%Y/%m/%d:%H:%M:%S'` Error. Something go wrong, Mysql process don't start!!!"
+                        return 1
+        fi
+}
+
+function extract_dump() {
+    echo "`date '+%Y/%m/%d:%H:%M:%S'` Extract dump-file $latest_dump to $mysql_data_dir"
+    test -d $mysql_data_dir || mkdir -p $mysql_data_dir
+    tar -xvzf $latest_dump -C $mysql_data_dir && \
+    echo "`date '+%Y/%m/%d:%H:%M:%S'` Extract dump-file $latest_dump to $mysql_data_dir finished" || \
+    { echo "`date '+%Y/%m/%d:%H:%M:%S'` Error. Something go wrong on extract dump-file $latest_dump to $mysql_data_dir!"; return 1; }
+}
+
+function change_datadir() {
+        echo "`date '+%Y/%m/%d:%H:%M:%S'` Starting function move_datadir"
+        which innobackupex > /dev/null 2>&1 || { echo "`date '+%Y/%m/%d:%H:%M:%S'`Error. No innobackupex installed!"; return 1; }
+        stop_mysql && \
+        test -d $mysql_data_dir -a "$mysql_data_dir" != '/' && { 
+            echo "`date '+%Y/%m/%d:%H:%M:%S'` Remove $mysql_data_dir"
+            chattr -i $mysql_data_dir/my.cnf 
+            rm -rf $mysql_data_dir && \
+            extract_dump && \
+            chattr -i $mysql_data_dir/my.cnf && \
+            chown -R mysql:mysql $mysql_data_dir 
+        } && \
+        start_mysql
+}
+
+which flock > /dev/null || { echo "No flock installed. You need to install flock first."; exit 1;}
+{
+    flock -n 9 || { echo "Another copy of ${0##*/} is still running on $myhostname. Lock file $lock_file locked.\n"; exit 1; }
+        test -f $log_file && mv $log_file $log_file.1
+        {
+                echo "`date '+%Y/%m/%d:%H:%M:%S'` Importing dump-file $latest_dump"
+
+                change_datadir && \
+                init_db
+                echo "`date '+%Y/%m/%d:%H:%M:%S'` Importing dump-file $latest_dump finished."
+        } >> $log_file  2>> >(tee -a $log_file >&2)
+} 9> $lock_file
+
+test -f $lock_file && rm -f $lock_file

+ 146 - 0
databases/redis/redis_db.sh

@@ -0,0 +1,146 @@
+#!/bin/bash
+
+export LANG=C
+export LC_ALL=C
+
+rdbver=1.1
+
+keyname=$2
+
+test -d $3 || mkdir -p $3
+log_file=$3/${0##*/}.log
+
+## redis opts
+redis_cli_opts='--raw'
+redis_bgsave_timeout=300
+redis_bgsave_one_try_timeout=15
+
+redis_data_dir='/var/lib/redis'
+redis_rdp_location="$redis_data_dir/dump.rdb"
+
+test -f $redis_rdp_location || { 
+    redis_data_dir='/var/spool/redis'
+    redis_rdp_location="$redis_data_dir/dump.rdb"
+}
+
+redis_backup_dir=$redis_data_dir
+
+current_date=`date '+%Y%m%d-%H%M%S'`
+
+## choosing compress
+bzip=`which bzip2 2>/dev/null`; pbzip=`which pbzip2 2>/dev/null`; gzip=`which gzip 2>/dev/null`; bzcompress=${pbzip:-${bzip}}; compress_prog=${bzcompress:-${gzip}}
+test -x "$compress_prog" || { echo "[ERROR `date '+%Y/%m/%d-%H:%M:%S'`] No compress util or $compress_prog has no execute flag. Fix it first."; exit 1; }
+
+case "${compress_prog##*/}" in
+        'pbzip2' ) compress_ext='bz2'; tar --version | grep -oE '1.2[0-6]' > /dev/null || compress_prog+=' -l' ;;
+        'bzip2'  ) compress_ext='bz2' ;;
+        'gzip'   ) compress_ext='gz' ;;
+esac
+
+function show_error() {
+        local message="$1"; local funcname="$2"; local log_date=`date '+%Y/%m/%d:%H:%M:%S'`
+        echo -e "[ERROR.$funcname $log_date] $message" >> $log_file 2>&1
+}
+
+function show_notice() {
+        local message="$1"; local funcname="$2"; local log_date=`date '+%Y/%m/%d:%H:%M:%S'`
+        echo -e "[NOTICE.$funcname $log_date] $message" >> $log_file 2>&1
+}
+
+function show_usage() {
+    echo "Usage: ${0##*/} --(get|remove) <domain name> <log path>" 
+    echo -e "\t Example: ${0##*/} --get example.ru /var/log"
+    echo -e "\t Example: ${0##*/} --remove example.ru /var/log" 
+}
+
+function backup_redis() {
+    local err=0; local dir=$1; local seconds=0;
+    which redis-cli > /dev/null || { show_error "Not found redis-cli." "$FUNCNAME"; return 1;}
+    test $# -eq 1 || { show_error "Function wrong usage! Args=$@" "$FUNCNAME"; return 1; }
+    test -d $dir || mkdir -p $dir
+    local lastsave_before=`echo lastsave | redis-cli $redis_cli_opts`
+    echo bgsave | redis-cli $redis_cli_opts
+    while [  $seconds -lt $redis_bgsave_timeout ]; do
+        sleep $redis_bgsave_one_try_timeout
+        let seconds=seconds+$redis_bgsave_one_try_timeout
+        local lastsave_after=`echo lastsave | redis-cli $redis_cli_opts`
+        show_notice "Curent seconds: $seconds. Lastsave before: $lastsave_before, lastsave after: $lastsave_after." "$FUNCNAME"
+        test "x$lastsave_before" != "x$lastsave_after" && {
+            show_notice "Saving $redis_rdp_location to $dir/${current_date}-redis.rdb, and compress" "$FUNCNAME"
+            cp $redis_rdp_location $dir/${current_date}-redis.rdb && \
+            nice -n 19 ionice -c 3 $compress_prog -f $dir/${current_date}-redis.rdb || \
+            { show_error "Error on copy and compress $redis_rdp_location" "$FUNCNAME"; local err=1; }
+            return $err;
+        }
+    done
+    show_error "Attempt to backup redis rdb has ended by timeout: $redis_bgsave_timeout seconds!" "$FUNCNAME"; return 1;
+}
+
+function remove_redis_db() {
+        redis-cli $redis_cli_opts -n $dbnumber flushdb
+        redis-cli $redis_cli_opts DEL $keyname
+        show_notice "Database $keyname with dbnumber=$dbnumber removed." "get_redis_db_number"
+}
+
+function get_redis_db_number() {
+        which redis-cli > /dev/null || { show_error "Not found redis-cli." "$FUNCNAME"; return 1;}
+        dbnumber=`redis-cli $redis_cli_opts GET $keyname`
+        show_notice "Curent dbnumber for key $keyname=$dbnumber" "$FUNCNAME"
+}
+
+function get_redis_db_list() {
+        which redis-cli > /dev/null || { show_error "Not found redis-cli." "$FUNCNAME"; return 1;}
+        dblist=`redis-cli $redis_cli_opts INFO keyspace |grep db |grep -v db0 |awk -F':' '{print $1}' | sed 's/db//' | sort | uniq |tr '\n' ' '`
+        show_notice "Redis dblist: $dblist" "$FUNCNAME"
+}
+
+function get_redis_db_not_used_number() {
+        local i=1;
+        while true
+        do
+            echo $dblist | tr ' ' '\n' | grep -qE "^${i}$" || break
+            i=$((i + 1));
+        done
+
+        dbnumber=$i
+        show_notice "Uniq dbnumber: $dbnumber" "$FUNCNAME"
+}
+
+function create_redis_key_with_db_number() {
+        redis-cli $redis_cli_opts SET $keyname "$dbnumber"
+        show_notice "Created key: $keyname with value=$dbnumber" "$FUNCNAME"
+}
+
+test x"$1" = "x" -o x"$2" = "x" -o x"$3" = "x" && { echo "One or more arguments is missing!"; show_usage; exit 1; }
+test x"$1" == x"" && show_usage
+
+test x"$1" = x"--get" && {
+
+    get_redis_db_number
+
+    check=`echo $dbnumber | grep -E ^\-?[0-9]+$`
+    test "$check" = '' && {
+        show_notice "Database $keyname not exist, script generate new database number." "get_redis_db_number"
+        backup_redis $redis_backup_dir && \
+        get_redis_db_list && \
+        get_redis_db_not_used_number && \
+        create_redis_key_with_db_number
+    } >> $log_file 2>&1
+
+    echo $dbnumber
+    exit 0
+}
+
+test x"$1" = x"--remove" && {
+
+    get_redis_db_number
+    check=`echo $dbnumber | grep -E ^\-?[0-9]+$`
+    test "$check" = '' && {
+        show_notice "Can't remove database $keyname, not exist." "get_redis_db_number"
+        exit 1;
+    } >> $log_file 2>&1
+
+    backup_redis $redis_backup_dir && \
+    remove_redis_db
+    exit 0
+}

+ 206 - 0
vault/vault-init.sh

@@ -0,0 +1,206 @@
+#!/bin/sh
+
+test $VAULT_HOST || VAULT_HOST=localhost
+test $VAULT_PORT || VAULT_PORT=8200
+VAULT_ADDRESS="http://$VAULT_HOST:$VAULT_PORT"
+
+test $TARGET_DIR || TARGET_DIR=/scripts
+test $VAULT_KEYS_FILE || VAULT_KEYS_FILE=$TARGET_DIR/private_keys
+
+TOKENS_LIST_FILE=$TARGET_DIR/token.list
+
+test -d $TARGET_DIR || mkdir -p $TARGET_DIR
+
+
+function show_usage() {
+	echo "Usage: ${0##*/} --(init|unseal|authorise|create-policies|create-tokens|create-secrets)" 
+	echo -e "\t Example: ${0##*/} --init"
+	echo -e "\t Example: ${0##*/} --unseal"
+	echo -e "\t Example: ${0##*/} --authorise"
+	echo -e "\t Example: ${0##*/} --create-policies"
+	echo -e "\t Example: ${0##*/} --create-tokens"
+	echo -e "\t Example: ${0##*/} --create-secrets"
+}
+
+function show_error() {
+		local message="$1"; local funcname="$2"; local log_date=`date '+%Y/%m/%d:%H:%M:%S %Z'`
+		echo -e "[ERROR.$funcname $log_date] $message" >&2
+		err=1
+}
+
+function show_notice() {
+		local message="$1"; local funcname="$2"; local log_date=`date '+%Y/%m/%d:%H:%M:%S %Z'`
+		echo -e "\n[NOTICE.$funcname $log_date] $message"
+}
+
+
+function vault_init() {
+	local err=0;
+
+	show_notice "Vault init and save keys to file $VAULT_KEYS_FILE started."
+	vault operator init --address=$VAULT_ADDRESS | grep -E 'Initial Root Token:|Unseal Key .:' > $VAULT_KEYS_FILE
+
+	return $err;
+}
+
+function vault_unseal() {
+	local err=0;
+	test -f $VAULT_KEYS_FILE || { show_error "Vault keys file: $VAULT_KEYS_FILE doesn't exist!" "$FUNCNAME"; return 1; }
+
+	show_notice "Vault unseal started."
+	list='1 2 3'
+	for number in $list; do
+		vault_unseal_key=`grep "Unseal Key $number" $VAULT_KEYS_FILE | awk -F':' '{print $2}' | tr '\n' ' ' | sed "s/ //g"`
+		vault operator unseal -address=$VAULT_ADDRESS $vault_unseal_key
+		echo
+	done
+
+	return $err;
+}
+
+function vault_authorise() {
+	local err=0;
+	test -f $VAULT_KEYS_FILE || { show_error "Vault keys file: $VAULT_KEYS_FILE doesn't exist!" "$FUNCNAME"; return 1; }
+
+	show_notice "Vault authorise with root token started."
+	vault_root_token=`grep 'Root Token' $VAULT_KEYS_FILE | awk -F':' '{print $2}' | tr '\n' ' ' | sed "s/ //g"`
+
+	vault login --address=$VAULT_ADDRESS $vault_root_token
+}
+
+function vault_create_policies() {
+	local err=0;
+	test -d $TARGET_DIR/policies || { show_error "Dir: $TARGET_DIR/policies doesn't exist!" "$FUNCNAME"; return 1; }
+
+	show_notice "Create Vault policies started."
+	for policy in `ls -1 $TARGET_DIR/policies/ | grep '.hcl' | sed "s/\.hcl//g"`; do
+		vault policy write --address=$VAULT_ADDRESS $policy $TARGET_DIR/policies/${policy}.hcl
+	done
+
+	return $err;
+}
+
+function vault_create_tokens() {
+	local err=0;
+	test -f $token_list || { show_error "Tokens file: $token_list doesn't exist!" "$FUNCNAME"; return 1; }
+
+	show_notice "Create Vault tokens started."
+
+	for token in `cat $TOKENS_LIST_FILE | awk '{print $1}'`; do
+		show_notice "Create token: $token"
+		token_policy=`grep $token $TOKENS_LIST_FILE | awk '{print $2}' | sed "s/\,/ -policy=/g"`
+		vault token create -id=$token -policy=$token_policy --address=$VAULT_ADDRESS
+		echo
+	done
+
+	return $err;
+}
+
+function vault_create_secrets() {
+	local err=0;
+	test -d $TARGET_DIR/secrets || { show_error "Dir: $TARGET_DIR/secrets doesn't exist!" "$FUNCNAME"; return 1; }
+
+	show_notice "Create Vault secrets started."
+
+	for secret in `find $TARGET_DIR/secrets/ -iname '*.json' -type f ! -size 0 -print | sed "s!$TARGET_DIR/secrets/!!g" | sed "s/\.json//g"`; do
+		vault write --address=$VAULT_ADDRESS secret/$secret @$TARGET_DIR/secrets/${secret}.json
+	done
+
+	return $err;
+}
+
+function vault_check() {
+	local err=0; local check=0;
+	one_try_timeout=1
+	seconds_timeout=60
+
+	seconds=`date +%s`
+	endTime=$(( $(date +%s) + $seconds_timeout ))
+	while [  $seconds -lt $endTime ]; do
+		sleep $one_try_timeout
+		seconds=`date +%s`
+
+		show_notice "Seconds until timeout: $(( $(date +%s) - $endTime ))"
+		nc -vz $VAULT_HOST $VAULT_PORT >/dev/null && \
+		{ seconds=$(($endTime+1)); show_notice "Vault is available."; check=1; }
+	done
+
+	test $seconds -gt $endTime -a $check -ne 1 && \
+	{ show_error "Something go wrong, during $seconds_timeout seconds vault doesn't available on $VAULT_ADDRESS" "$FUNCNAME"; return 1; }
+
+	return $err;
+}
+
+function vault_check_init() {
+	local err=0;
+
+	show_notice "Starting check that Vault is initialised."
+	vault operator init --address=$VAULT_ADDRESS -status
+	result=$?
+	if [ "${result}" -eq "2" ] ; then
+		show_notice "Vault doesn't initialised, executing init."
+		vault_init
+	else
+		show_notice "All OK Vault aleready initialised."
+	fi
+
+	return $err;
+}
+
+function vault_check_unseal() {
+	local err=0;
+
+	show_notice "Starting check that Vault is unsealed."
+	vault status --address=$VAULT_ADDRESS
+	result=$?
+	if [ "${result}" -eq "2" ] ; then
+		show_notice "Vault sealed, executing unseal."
+		vault_unseal
+	else
+		show_notice "All OK Vault aleready unsealed."
+	fi
+
+	return $err;
+}
+
+
+
+#Check script usage
+test $# -eq 1 || { show_error "Wrong script usage!" ""; show_usage; exit 1; }
+test x"$1" == x"" && show_usage
+
+
+test x"$1" = x"--init" && {
+
+		show_notice "Vault init started."
+		vault_check && \
+		vault_check_init && \
+		vault_check_unseal && \
+		sleep 5 && vault_authorise && \
+		vault_create_policies && \
+		vault_create_tokens && \
+		vault_create_secrets
+
+		echo "Script ended with value: $?"
+}
+
+test x"$1" = x"--unseal" && {
+	vault_unseal
+}
+
+test x"$1" = x"--authorise" && {
+	vault_authorise
+}
+
+test x"$1" = x"--create-policies" && {
+	vault_create_policies
+}
+
+test x"$1" = x"--create-tokens" && {
+	vault_create_tokens
+}
+
+test x"$1" = x"--create-secrets" && {
+	vault_create_secrets
+}
+