Deployment Recipes Part 10 – Python, Web2py, Apache and Debian

Welcome back to Deployment Recipes Part 10 – Backups are essential for any project. We look at basic postgres backup in this article. Also we would look at other troubleshooting areas and nano editor commands for smoother operation

Backup Databases to .sql files

As  a root user issue following commands

sudo su – postgres

pg_dump hms > hms_bak_oct08.sql

hms is the database name and hms_bak_oct08.sql is the file to which we want to export the data to

The backed up files reside here  –

/var/lib/postgresql

Troubleshoot not receiving mails

Opening up iptables

iptables -A OUTPUT -p tcp -m tcp –dport 465 -j ACCEPT

iptables -A OUTPUT -p tcp -m tcp –dport 587 -j ACCEPT

on Debian/Ubuntu

On Debian/Ubuntu, if you use iptables rule file provided by iRedMail, please update /etc/default/iptables, add one rule (third line in below code) for port 465, then restart iptables service.

# Part of file: /etc/default/iptables
-A INPUT -p tcp --dport 25 -j ACCEPT
-A INPUT -p tcp --dport 587 -j ACCEPT
-A INPUT -p tcp --dport 465 -j ACCEPT

How to enable SMTPS

To enable SMTPS, you should configure Postfix to listen on port 465 first, then open port 465 in iptables.

Please append below lines in Postfix config file /etc/postfix/master.cf (Linux/OpenBSD) or /usr/local/etc/postfix/master.cf(FreeBSD):

465     inet  n       -       n       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o content_filter=smtp-amavis:[127.0.0.1]:10026

Restart Postfix service to enable SMTPS.

Connection Refused Error

Connection refused is generated by the kernel because sender refuses to establish TCP connection. Possible reasons are:

  1. No on listen on this host/port
  2. iptables (or other firewall) specifically -j REJECT connections.

From your code it’s hard to say if you listen on a proper host/port or not.

How to debug?

  1. Run you service. Without connecting to it run ‘netstat -lnpt’, it should provide you with list of services listening on this host.
  2. Try nc host port (note – not a ‘:’, but a space here), if connection is refused, nc will terminate instantly. If not, you’ve established connection (try to type ‘GET / HTTP/1.1’ and Enter twice in this case).
  3. Just print output of socket.gethostname() and port variable in your code.

I suspect that in your case socket.gethostname() has been resolved into something different than localhost.

Tried – Opening connection in port 465 for mail.targetsoft.co.in

telnet mail.targetsoft.co.in 465

telnet mail.targetsoft.co.in 26

telnet mail.targetsoft.co.in 587

Then opened using

You can enable port 465 (smtps) in postfix as a workaround. Edit the /etc/postfix/master.cf file:

vi /etc/postfix/master.cf

and remove the # in front of the smtps line. Example for Debain 9, change the line:

#smtps     inet  n       –       –       –       –       smtpd

to:

smtps     inet  n       –       –       –       –       smtpd

and restart postfix:

/etc/init.d/postfix restart

Checked in python using

>>> import smtplib

>>> from email.mime.multipart import MIMEMultipart

>>> from email.mime.text import MIMEText

>>> from email.MIMEMultipart import MIMEMultipart

>>> from email.MIMEText import MIMEText

>>> from email.MIMEImage import MIMEImage

>>> sender=’targetplus@targetsoft.co.in’

>>> receiver=’rahul@gmail.com’

>>> password=’password’

>>> msg = MIMEText(body, ‘plain’, ‘utf-8’)

>>> msg[‘Subject’] = subj

>>> msg[‘To’] = to

>>> msg[‘Importance’] = ‘high’

>>> smtpserver=smtplib.SMTP(“mail.targetsoft.co.in”, 465)

>>> smtpserver.sendmail(sender, receiver, msg.as_string())

Copy paste this code in python shell

Test code –

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText

from email.MIMEImage import MIMEImage

sender=’targetplus@targetsoft.co.in’

receiver=’rahul.@gmail.com’

password=’;dfg03409s’

text = “Hello There”

msg = MIMEText(text, ‘plain’, ‘utf-8’)

msg[‘Subject’] = “test”

msg[‘To’] = receiver

msg[‘Importance’] = ‘high’

smtpserver=smtplib.SMTP(“mail.targetsoft.co.in”, 465)

smtpserver.sendmail(sender, receiver, msg.as_string())

Other web server Provided by ISP (Use with authentication, code not here)

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText

from email.MIMEImage import MIMEImage

sender=’targetplus@targetsoft.co.in’

receiver=’rahul@gmail.com’

password=’;dfg03409s’

text = “Hello There”

msg = MIMEText(text, ‘plain’, ‘utf-8’)

msg[‘Subject’] = “test”

msg[‘To’] = receiver

msg[‘Importance’] = ‘high’

smtpserver=smtplib.SMTP(“webmail.targetsoft.co.in”, 26)

smtpserver.ehlo()

smtpserver.starttls()

smtpserver.ehlo()

smtpserver.sendmail(sender, receiver, msg.as_string())

Local email —

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.MIMEMultipart import MIMEMultipart

from email.MIMEText import MIMEText

from email.MIMEImage import MIMEImage

sender=’targetpluz@targetsoft.co.in’

receiver=’targetpluz@targetsoft.co.in’

password=’;dfg03409s’

text = “Hello There! How are you? “

msg = MIMEText(text, ‘plain’, ‘utf-8’)

msg[‘Subject’] = “test”

msg[‘To’] = receiver

msg[‘Importance’] = ‘high’

smtpserver=smtplib.SMTP(“webmail.targetsoft.co.in”, 26)

smtpserver.sendmail(sender, receiver, msg.as_string())

NANO EDITOR COMMANDS

File Control in nano

nano index.php Open or create the file “index.php” with nano on command line.
Ctrl-o Y Enter Save changes.
Ctrl-r Alt-f Open a new file with a new buffer within nano.
Alt-> Switch to the next file buffer in nano.
Alt-< Switch to the previous file buffer in nano.
Ctrl-x Quit nano.

Navigating through file contents in nano

Ctrl-a Move to the beginning of the current line.
Ctrl-e Move to the end of the current line.
Ctrl-v Move down one page.
Ctrl-y Move up one page.
Alt-\ Go to the beginning of the file.
Alt-/ Go to the end of the file.
Alt-g Go to a target line number.
Alt-] Jump to matching open/close symbol.
Alt-a Alt-} Select a block and indent the block.
Alt-a Alt-{ Select a block and outden the block.

Copy and Paste in nano

Alt-a To select a block for copy or cut operation, do Alt-a again to unselect.
Alt-a Alt-^ Copy a highlighted block to the clipboard.
Alt-a Ctrl-k Cut a highlighted block to the clipboard.
Ctrl-k Cut from the current cursor position to the end of the current line.
Ctrl-u Paste the contents from the clipboard at the current cursor position.

Search and Replace in nano

Ctrl-w Search for a target string.
Alt-w Repeat the last search.
Alt-r Search and replace.

Handy Nano commands

Ctrl + M + _           à Go to line

Ctrl + G                 à Access Nano help

F6                         à To search for text in a file (including regular expressions)

Well that’s a lot of stuff to digest for this article today. What do you like or think about nano do let me know in the comments below. If you would like me to cover emacs or other editor let me know and I would try to incorporate that as well. Until next time Stay focussed.

Dhakate Rahul

Dhakate Rahul

Leave a Reply

Your email address will not be published. Required fields are marked *