Perpetual rsync


Automatically resume rsync over SSH after broken connection
Create a file in /bin/prsync


#!/bin/bash
### COPY IN /bin/prsync - chmod 777 ###
# GitHub https://gist.github.com/pwenzel/4182838
# Extended by Gulshan Beejan - Persistent RSYNC# Make sure remote system has ssh key pairsif [[ -z $1 ]] ; then
echo "Persistent RSYNC Usage: prsync SOURCE DESTINATION"
exit
fi
if [[ -z $2 ]] ; then
echo "Persistent RSYNC Usage: prsync SOURCE DESTINATION"
exit
fiwhile [ 1 ]
do
rsync -avz --partial $1 $2
if [ "$?" = "0" ] ; then
echo "prsync completed normally"
exit
else
echo "==== prsync failure. ==== Backing off and retrying in 30 seconds..."
sleep 30
fi
done


chmod +x /bin/prsync

then you can run it as:

prsync mysource_file.tar user@destination_ip:/path/to/file

Read More

Linux: Sudoers


What is it?

Sudoers is a list of users which may execute root commands. A sudoers file is made up of two types of entries, aliases and user specifications

How to set it up?

Do a visudo on command line, then add the user to the list as below.

If you want to enter the user’s password before executing the command:

myuser ALL=(ALL) ALL

And without a password:

myuser ALL=(ALL) NOPASSWD: ALL

 

Read More

My MySQL Server went down today!! Troubleshooting.


Panic!!!

My MySQL Server went down today, which meant that my website was not working as well. When I checked, it clearly replied:

[root@sdb ~]# service mysqld status
mysqld is stopped

I automatically checked for the uptime of the server to see that it was a few hours ago. A chkconfig revealed that I forgot to add mysqld to autostart in run levels 2-5. Serious mistake, but since I reinstalled the server a few days ago, comprehensible.. i guess!

mysqld          0:off   1:off   2:off   3:off   4:off   5:off   6:off

I then switched it to autorun and started the service via the following commands:

 

chkconfig mysqld on

 

service mysqld start

Solved! But not proper..

..The big question why did the server reboot?

I checked the logs at /var/log/messages to see the reason of the reboot.

less /var/log/messages

which revealed

Jun  1 03:08:06 sdb init: tty (/dev/tty1) main process (1972) killed by TERM signal
Jun  1 03:08:06 sdb init: serial (hvc0) main process (1974) killed by TERM signal
Jun  1 03:08:06 sdb init: tty (/dev/tty2) main process (1975) killed by TERM signal
Jun  1 03:08:06 sdb init: tty (/dev/tty3) main process (1977) killed by TERM signal
Jun  1 03:08:06 sdb init: tty (/dev/tty4) main process (1980) killed by TERM signal
Jun  1 03:08:06 sdb init: tty (/dev/tty5) main process (1982) killed by TERM signal
Jun  1 03:08:06 sdb init: tty (/dev/tty6) main process (1984) killed by TERM signal
Jun  1 03:08:12 sdb abrtd: Got signal 15, exiting
Jun  1 03:09:12 sdb init: Disconnected from system bus
Jun  1 03:09:12 sdb auditd[1824]: The audit daemon is exiting.
Jun  1 03:09:14 sdb kernel: Kernel logging (proc) stopped.
Jun  1 03:09:14 sdb rsyslogd: [origin software=”rsyslogd” swVersion=”4.6.2″ x-pid=”1839″ x-info=”http://www.rsyslog.com”] exiting on signal 15.
Jun  1 03:18:53 sdb kernel: imklog 4.6.2, log source = /proc/kmsg started.

What does this mean?

The system was shut down by an external person, Linode. I forgot they said that they would be having migration. Here’s the extract of the mail:

 * sdb – (7864940) System Shutdown – Completed Fri, 01 Jun 2012 03:08:07 GMT
* sdb – (7864941) Linode Initial Configuration – Completed Fri, 01 Jun 2012 03:09:28 GMT
* sdb – (7864942) Migrate Filesystem – swap from london62.linode.com – Completed Fri, 01 Jun 2012 03:09:29 GMT
* sdb – (7864943) Migrate Filesystem – Data Disk from london62.linode.com – Completed Fri, 01 Jun 2012 03:10:00 GMT
* sdb – (7864944) Migrate Filesystem – Backup Disk from london62.linode.com – Completed Fri, 01 Jun 2012 03:12:36 GMT
* sdb – (7864945) Migrate Filesystem – CentOS 6.2 64bit Disk Image from london62.linode.com – Completed Fri, 01 Jun 2012 03:15:02 GMT
* sdb – (7864946) Linode Migration Cleanup – Completed Fri, 01 Jun 2012 03:18:29 GMT
* sdb – (7864947) System Boot – SDB Config – Completed Fri, 01 Jun 2012 03:18:36 GMT

Happy as ever, I wrote a script to auto start a service if they have been found as not running, even if the server did not reboot. View it here.

Read More

Bash Script: Check service and auto start if needed


If you are running a service and for some reason the service gets broken, you will want it to automatically restart. For that reason, I have created a script as below. Feel free to amend the array autoservice as needed because in my case it is monitoring mysqld, cherokee and squid.

#!/bin/bash
#Add values to the array autoservice seperated by spaces
autoservice=(mysqld cherokee squid)

#the code
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
for i in “${autoservice[@]}”
do
:
service $i status # > /dev/null
if [ $? -eq 0 ]; then
echo $i “Running..”
else
service $i restart
fi
done

Created in /scripts/checkService.sh, you need to chmod 777 the file and add it to crontab to run every 5minutes via the following:

 

chmod 777 /scripts/checkService.sh

 

crontab -e

add this line to run it every 5 mintues

*/5     *       *       *       *       sh /scripts/checkService.sh

Read More

WordPress Mail – VPS CentOS 6.2


I recently re-installed centos on the VPS and almost forgot that I needed sendmail on the server to send out my daily backups. The backup is run from a plugin called WordPress Database Backup.

Installation is fairly easy with the following steps:

Check if sendmail has been installed

 [root@sdb ~]# rpm -qa sendmail
[root@sdb ~]#

if it is, update to the latest version

yum update sendmail

Else, let’s install it.

yum install sendmail

Output:

[root@sdb ~]# yum install sendmail
Loaded plugins: fastestmirror, priorities, security
Loading mirror speeds from cached hostfile
epel/metalink                                            |  16 kB     00:00
* base: mirror01.th.ifl.net
* epel: mirror01.th.ifl.net
* extras: mirror01.th.ifl.net
* ius: mirrors.ircam.fr
* rpmforge: www.mirrorservice.org
* updates: mirror01.th.ifl.net
base                                                     | 3.7 kB     00:00
epel                                                     | 4.0 kB     00:00
epel/primary_db                                          | 4.4 MB     00:00
extras                                                   | 3.5 kB     00:00
ius                                                      | 2.2 kB     00:00
ius/primary_db                                           |  71 kB     00:00
rpmforge                                                 | 1.9 kB     00:00
updates                                                  | 3.5 kB     00:00
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package sendmail.x86_64 0:8.14.4-8.el6 will be installed
–> Processing Dependency: procmail for package: sendmail-8.14.4-8.el6.x86_64
–> Processing Dependency: libhesiod.so.0()(64bit) for package: sendmail-8.14.4-                                                                                     8.el6.x86_64
–> Running transaction check
—> Package hesiod.x86_64 0:3.1.0-19.el6 will be installed
—> Package procmail.x86_64 0:3.22-25.1.el6 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package           Arch            Version                  Repository     Size
================================================================================
Installing:
sendmail          x86_64          8.14.4-8.el6             base          717 k
Installing for dependencies:
hesiod            x86_64          3.1.0-19.el6             base           20 k
procmail          x86_64          3.22-25.1.el6            base          163 k

Transaction Summary
================================================================================
Install       3 Package(s)

Total download size: 900 k
Installed size: 1.9 M
Is this ok [y/N]:

Y to continue, the installation will complete.

Start Sendmail

service sendmail start

Accept incoming mail

You will need to modify the file in /etc/mail/local-host-names through vi

vi /etc/mail/local-host-names

Add your server hostnames to accept mail from.

In my scenario, it would be gulshan.beejan.com

Restart Sendmail Service

service sendmail restart

Testing was done, and it worked. =)


Read More