вторник, 19 сентября 2017 г.

How to send bash script output via email using AWS SES


  • Create SES SMTP user

https://console.aws.amazon.com/iam/home?#/s=SESHome

We need

SMTP Username:
AXXXXXXXXXXXXQ
SMTP Password:
AsFbCCCXXXXXXXXXXXCXXXXXXXXXXXqQ6

  • Validate email to send FROM
SES - Email Addresses - Verify a New Email Address
  • Create custom policy in IAM in order to give permissions to send emails and assign to a role. Assign role to instance.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ses:SendEmail",
                "ses:SendRawEmail"
            ],
            "Resource": "*"
        }
    ]
}
  • install mailx package ( yum install mailx) and test it :
-v verbose
-s Subject
-S settings
-S nss-config-dir can be found by  find / -name "cert*.db", /etc/pki/nssdb/ in my case
-S from "EMAIL@VALIDATED.DOMAIN" - watch step 2

mailx -v -s "Test" -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S smtp=smtp://email-smtp.us-east-1.amazonaws.com:587 -S from="EMAIL@VALIDATED.DOMAIN" -S smtp-auth-user=AXXXXXXXXXXXXQ -S smtp-auth-password= AsFbCCCXXXXXXXXXXXCXXXXXXXXXXXqQ6 -S nss-config-dir=/etc/pki/nssdb/ USER@DOMAIN.RECIPIENT

  • Edit global mailx conf (/etc/mail.rc)  and add account: 

account ses {
        set smtp-use-starttls
        set ssl-verify=ignore
        set smtp-auth=login
        set smtp=smtp://email-smtp.us-east-1.amazonaws.com:587
        set from="EMAIL@VALIDATED.DOMAIN"
        set smtp-auth-user= AXXXXXXXXXXXXQ
        set smtp-auth-password= AsFbCCCXXXXXXXXXXXCXXXXXXXXXXXqQ6
        set nss-config-dir=/etc/pki/nssdb/
}

  • Test mailx global config
echo -e "Test Mail body text" | mailx -v -s "Test" -A ses USER@DOMAIN.RECIPIENT

  • Create script and add it to cron

Linux: find and delete files periodically

Create script, that finds all files, older than 30 minutes:

#!/bin/bash

TARGET_FOLDER="/var/somedir/Archive"
cd $TARGET_FOLDER
find . -type f -mtime +30 -exec rm -rf {} \;


Add to cron to run script every day at 04:00

0 4 * * * /bin/bash /scripts/SCRIPT LOCATION


понедельник, 11 сентября 2017 г.

How to access MSSQL DB from Amazon Linux and to do a failover

To access MSSQL DB unixODBC is needed.
Amazon yum repo has unixODBC-2.2.14-14.el6.x86_64, but unixODBC-2.3.2 or more is needed.
unixODBC-2.3.2 and mssql-tools-14 could be found in official MS repo.


  • 1) Delete existing unixODBC (version is old, and mssql-tools can't use it)

sudo yum remove unixODBC*


  • 2) Download repo list

curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo


  • 3) Install packages 

sudo ACCEPT_EULA=Y yum install msodbcsql-13.0.1.0-1 mssql-tools-14.0.2.0-1


  • 4) Not mandatory, but it may be used by mssql sensitive applications (zabbix-mysql for example, it uses unixODBC 2.2) 

sudo yum install unixODBC-utf16-devel


  • 5) Create symlinks for convenient usage

ln -sfn /opt/mssql-tools/bin/sqlcmd-13.0.1.0 /usr/bin/sqlcmd
ln -sfn /opt/mssql-tools/bin/bcp-13.0.1.0 /usr/bin/bcp


  • 6) To connect to DB use:
sqlcmd -S IP.IP.IP.IP,1433 -U 'USERNAME' -P 'PASSWORD'
  • 7) To execute something:
SELECT * FROM sys.databases
GO
  • 8) To run SQL in script (to do a failover in that case) :
/usr/bin/sqlcmd -S IP.IP.IP.IP,1433 -U 'USERNAME' -P 'PASSWORD' -Q "ALTER DATABASE ffmain SET PARTNER FAILOVER"

Bash: MySql backup (file per db), restore+ users and privileges

Backup Mysql DB (file per db) #!/bin/bash USER="root" databases=`mysql -u $USER -e "SHOW DATABASES;" | tr -d "|...