Mysql backup script

To backup mysql on a daily/hourly basis with time stamp and compress it after backup also it will remove the files older than x days.

#!/bin/bash
# Arun N S
# variables
DATE="$(date +"%d-%m-%Y")"
TIME="$(date +"%d-%m-%Y-%H%M")"
USER=username
PASSWORD=password
DATABASE=dbname


# Directories and dump
/bin/mkdir -p /backup/Mysql/$DATE
/usr/bin/mysqldump -l -F -u $USER --password=$PASSWORD $DATABASE > /backup/Mysql/$DATE/backup_$TIME.sql


# Compressing
/usr/bin/bzip2 /backup/Mysql/*/*.sql


#Removing files older than x days eg: 90 days
for i in `/usr/bin/find /backup/Mysql/ -maxdepth 1 -type d -mtime +90 -print`; do
/bin/echo -e "Deleting old directories $i"; /bin/rm -rf $i; done

Fix for – – mixing * ports and non-* ports with a NameVirtualHost address is not supported

If you happend to see this error in apache logs,

– mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results

Check your apache configuration, to make sure that there is no VirtualHost defined without port numbers like : <VirtualHost 127.0.0.1>, if existing fix it with port number.

Mysql Queries and Tips

Some useful database queries:

login to mysql database:

$ mysql $database_name -u $user_name -h $host_name -p

Take a dump by locking transactions

$ mysqldump -l -F $database_name -u $user_name -h $host_name -p > file.sql
*need lock table and read privilege from the host you trying

Take backup of only some tables
$ mysqldump -l -F $database_name -u $user_name -h $host_name --tables $tables_name -p > file.sql

Take backup of only database structure , without data
$ mysqldump -l -F -d $database_name> -u $username -h $hostname -p > file.sql

List the permissions assigned for a user
login to mysql:
> show grants for 'user'@'hostname';

Find Tips

Copy/Move files with find

find <path> -name "filename" -exec cp -prf {} /destination/{} ;
find /var/log/ -name "m*" -exec cp -prf {} /tmp/message/{} ;

This will create the same directory structure under /tmp/message, incase you want all subdirectory files under /tmp/message/ remove the {} .

Remove files older than certain days (using find/mtime)
find -name "" -mtime +N -exec rm -r {} ;

Eg : find /var/log/ -name "*.log" -mtime +5 -exec rm -r {} ;
This will remove the *.log files older than 5 days in directory /var/log/

Find with file type

directories : find / -type d -print0
files: find / -type f -print0

Remove held messages from mailman queue

If you have too many mails/spams tend to pending moderator requests in mailman queue it can be removed by:

$ cd ~mailman
$ bin/discard /var/lib/mailman/data/heldmsg--*

And if the no. of argument list exceeds you can try:

$ find /var/lib/mailman/data -name heldmsg--* -print | xargs bin/discard

* the path of mailman depends on your implementation anyway.

script to convert openssh keys to tectia and tectia to openssh


#!/bin/bash
# Arun N S
menu="
Make your choice:
1. Openssh key to Tectia format
2. Tectia key to Openssh format
3. Exit"
while :
do
printf "%sn: " "$menu"
read choice
case $choice in
"1") echo "Converting Openssh key to Tectia format"
echo "Enter key Path(full path):"
while read INPUT
do
ssh-keygen -e -f ${INPUT} > ${INPUT}.tectia
echo "OpenSSH format key is saved as: ${INPUT}.tectia"
exit 0
done
break;;
"2") echo "Converting Tectia key to Openssh format";
echo "Enter File Path(full path):"
while read INPUT
do
ssh-keygen -i -f ${INPUT} > ${INPUT}.openssh
echo "OpenSSH format key is saved as: ${INPUT}.openssh"
exit 0
done
break;;
"3") echo "3. Exit"
echo " Good Bye"
break;;
*) echo "Wrong Choice"
;;
esac
done

./arun

configure NSD as slave for BIND

To configure name server deamon as slave for bind:

# useradd -c “NSD” -d /home/nsd -s /sbin/nologin -u 1005 nsd

Download and install the latest version of nsd

# ./configure –prefix=/home/nsd –enable-dnssec –enable-tsig –with-user=nsd
# make
# make install

Edit config: /home/nsd/etc/nsd/nsd.conf
>>>
server:
ip-address:
hide-version: yes
debug-mode: no
ip4-only: no
ip6-only: no
database: "/home/nsd/var/db/nsd/nsd.db"
identity: ""
logfile: "/var/log/nsd.log"
server-count: 1
tcp-count: 10
pidfile: "/home/nsd/var/db/nsd/nsd.pid"
port: 53
statistics: 3600
chroot: "/home/nsd/etc/nsd"
username: nsd
zonesdir: "/home/nsd/etc/nsd"
difffile: "/home/nsd/var/db/nsd/ixfr.db"
xfrdfile: "/home/nsd/var/db/nsd/xfrd.state"
xfrd-reload-timeout: 10

Add key and zone file details:

Start NSD with:
/home/nsd/sbin/nsdc start

to create zone files:
/home/nsd/sbin/nsdc patch

./arun

auto responder with postfix/virtual

To configure autoreponder for some address with postfix.

Edit the postfix virtual configuration:
Add –
email_address recepient_lists, email_address@autoreply.nic.net.sa
eg: wordpress@example.com recepient1@example.com, wordpress@google.com@autoreply.example.com

Edit the transport configuration:
Add –
autoreply.domain.com autoreply:
eg: autoreply.example.com autoreply:

Create pipe for the transponder in master.cf

autoreply unix – n n – – pipe
flags= user=nobody argv=/usr/local/bin/autoreply $sender $recipient

Created autoreply script at /usr/local/bin/autoreply
eg:

/usr/sbin/sendmail -oi -t << EOF From: nobody@example.com To: $1 Subject: Auto Response from example.com Content-Type: text/plain; charset=utf-8 This is an autoreponder email. EOF

There is other better way to do it with procmail and other tools, depends on your mail server setup

get rid of apache@localhost in received email headers

There is a good change that the foreign email servers will drop if the sender email headers are not properly formatted.
Anyway if you get apache@localhost in Received header (with postfix/apache/php), you can change the php.ini, sendmail_path program to use sendmail.postfix instead of sendmail binary.

like : sendmail_path = /usr/sbin/sendmail.postfix -t -i -f

./arun

Hylafax/FaxMail with unicode encoding

It is pretty hard to get unicode works with fax ..
here are some scripts that i used to get the unicode done for incoming html/text faxes.

HTML
vi /etc/hylafax/faxmail/text/html
#!/bin/bash
cp -pf $1 /tmp/email.html
/usr/bin/lynx -dump -display_charset=utf-8 /tmp/email.html > /tmp/html_txt
/usr/bin/uniprint -font /etc/hylafax/faxmail/Cyberbit.ttf -in /tmp/html_txt -out /tmp/html_txt.uni
cat /tmp/html_txt.uni|/etc/hylafax/faxmail/filter.pl
rm /tmp/email.html
rm /tmp/html_txt
rm /tmp/html_txt.uni


plain text

vi /etc/hylafax/faxmail/text/plain
#!/bin/sh
/usr/bin/uniprint -font /etc/hylafax/faxmail/Cyberbit.ttf -in $1 -out /tmp/plain_txt
# echo " "
# echo "showpage"
cat /tmp/plain_txt | /etc/hylafax/faxmail/filter.pl
$rm /tmp/plain_txt

filter.pl
#!/usr/bin/perl
# Read from the standard input
@text=;
$size=@text;
# Count the number of "showpage"
$count=0;
for($i=0;$i<=$size;$i++){if($text[$i] =~ /showpage/){$count++;}}
# Discard the last line that contain "showpage"
$num=1;
for($i=0;$i<=$size;$i++){
if($text[$i] =~ /showpage/){
if($num!=$count){$num++;}
else{$text[$i]=~s/showpage//g;}
}
print $text[$i];
}

Fix for Postfix X-Authentication Warning

X-Authentication-Warning can appear on the mail header , when the user sending email is not trusted by the mail server. There is a chance that email are considered as spam because of this.

To fix it you can add the user to /etc/mail/trusted-users

If your system has forms that used to send email as apache user, you can add “apache” user to the trusted-users file.