Simple Rsync Backup Script

Over the last 10 years i’ve tested and used many different backup solutions for my Linux and Mac machines.

I think the very first one i have used regularly was unison. A great tool for bidirectional syncing, but it always had problems with special characters.

And, there are many commercial ones out there. The good ones often cost US$ 40-80. Simply too much for what i need.

Last weekend i thought… Hey dude, you’re a Linux guy, do it the good old Unix way! The simple solutions are often so close…

First i simply used a very simple bash script which only executed some rsync commands. Then i extended it to do some checks, write some informational data to the screen, and make the configuration a bit simpler.

If you want to try it, download the file below, make the expanded “.sh”-file executable and add your directories to backup at the bottom within the configuration section as follows:

syncDir "Name" "Source Directory" "Destination Directory"

for example:
syncDir "Very important data" "/Users/myname/Important" "/Volumes/Backup/"
syncDir "Documents" "$HOME/Documents" "/Volumes/Backup/myuser/"

At the moment only local directories can be used. No remote systems (yet).

Download: Backup_1.0.sh

Posted in Backup, Bash, Linux, Mac | Tagged , , , , | Leave a comment

Getting RHT03 Temperature- and Humidity Sensor working at 3.3V

Around a week ago, I tried to get a RHT03 sensor to work with my JeeNode which runs at 3.3V.

Whatever i did, it always returned a timeout error. The datasheet specifies the sensor to work from 3.3 to 6V, so it should work. If i connect 5V to VCC, it worked fine , but not with 3.3V.

At the end i found out that i can connect Pin 3 and 4 (the two pins on the right when looking from the front) to make it work with 3.3V. Pin 3 id NULL, and pin 4 is GND.

Posted in Arduino, Projects, Quick tip | Leave a comment

Log mails sent by PHP mail() function

PHP 5.3 introduced a new feature many administrators have waited for. Now it’s possible to log mails sent by the mail() function of PHP.

Until 5.3 it was not that easy to find an insecure form/script which sends out spam messages.

But now, finally, after around two years after the patch was submitted, you can add this to your php.ini:

mail.add_x_header = On
mail.log = /var/log/phpmail.log

The first line adds an additional X-Header to the mail itself. It contains the “uid” and the file name of the script.

Te second line logs the full path to the script, the “To:” field and all headers to the specified file.

The log will look like this:

mail() on [/var/www/example.com/httpdocs/pages/formmail.php:50]: To: info@example.com -- Headers: From: "John Smith: " <js@example.com>

NOTE: If you want to leave this option enabled all the time, don’t forget to configure logrotate for the new logfile.

Posted in Linux, PHP, Quick tip | Tagged , , , | 2 Comments

Find out the website causing high load on a Apache webserver

If you’re running an Apache webserver with many customer websites, there will be a time (sooner or later) where your server is flooded with a lot of page requests, causing a high CPU-load and memory usage. Specially if PHP or other scripting is used behind. Most of the time this is caused by a harmful script somewhere in the net. But, how to find out which of the sites is the affected one? Looking at top/ps doesn’t helps much if PHP (f.ex.) is running as a apache module. You will only see a lot of “httpd” processes.

A good tool to get closer to it is apachetop. It takes the access.log as argument and shows you all accessed pages, hosts and more:

apachetop -f /var/log/httpd/access_log

Cool, but… What if you’re using Plesk? It stores the access_log of each website in a separate file within the corresponding vhost directory. The default access_log doesn’t help, as long the problem is not related to webmail for example.

You can add multiple “-f” arguments to apachetop manually. But if you have 300+ vhosts? Not really. Luckily we’re on Linux and can do something like this:

apachetop $(find /var/www/vhosts/*/statistics/logs/ -name "access_log" -print | sed 's/^/-f '/)

This adds all access-logs within our vhosts directory as arguments. Unfortunately, it fails:

Only 50 files are supported at the moment
Segmentation fault

OK, how we can limit the number of files passed to apachetop? Because we’re searching for a lot of request, we can assume the logfile already has some size. Most of our customer sites have a very low load anyway or are used for mail only. So, let us extend the used find command a bit:

apachetop $(find /var/www/vhosts/*/statistics/logs/ -type f -size +10k -name "access_log" -print | sed 's/^/-f '/)

Now, only logs are passed to apachetop which are bigger than 10 kilobytes. You can adjust it as needed. “c” => Bytes, “k” => Kilobytes “M” => Megabytes, “G” => Gigabytes.

Now we see something like this:

last hit: 17:56:25 atop runtime: 0 days, 00:24:25 17:56:35</pre>
All: 747 reqs ( 0.5/sec) 14.5M ( 10.2K/sec) 19.9K/req
 2xx: 657 (88.0%) 3xx: 42 ( 5.6%) 4xx: 44 ( 5.9%) 5xx: 4 ( 0.5%)
 R ( 30s): 40 reqs ( 1.3/sec) 464.6K ( 15.5K/sec) 11.6K/req
 2xx: 38 (95.0%) 3xx: 1 ( 2.5%) 4xx: 1 ( 2.5%) 5xx: 0 ( 0.0%)

REQS REQ/S KB KB/S URL
 2 0.09 21.0 1.0*/plugins/system/yoo_effects/yoo_effects.js.php
 1 0.04 0.5 0.0 /index.php
 1 0.05 6.7 0.3 /
 1 0.05 10.9 0.5 /templates/mobile_elegance/jqm/jquery.mobile-1.2.0.min.css
 1 0.05 1.4 0.1 /media/zoo/assets/css/reset.css
 1 0.05 0.5 0.0 /media/zoo/applications/product/templates/default/assets/css/zoo.css
 1 0.05 1.0 0.0 /plugins/system/yoo_effects/lightbox/shadowbox.css
 1 0.05 1.8 0.1 /components/com_rsform/assets/calendar/calendar.css
 1 0.05 0.7 0.0 /components/com_rsform/assets/css/front.css
 1 0.05 5.2 0.2 /components/com_rsform/assets/js/script.js
 1 0.05 0.5 0.0 /components/com_zoo/assets/js/default.js

Missing something? Yes, the domain…
The access_log doesn’t contains the domain of the  vhost itself, just the path to the file. But maybe enough to find out which site is affected.

If you’re pressing the key “d” one time, you can switch to hosts view. Maybe there is one single IP the all the requests are coming from. If so, you can simply block this IP for some time.

Or, if you could identify the IP address, you can grep for it within all access_logs with (not tried with 50+ files, but think it should work):

grep "12.34.56.78" /var/www/vhosts/*/statistics/logs/access_log
Posted in Apache, Linux, Plesk | Tagged , , , , , , , | 2 Comments

Update kernel and modules of a Raspberry Pi

Last week, i received my new Raspberry Pi with 512MB RAM, which should replace my current 256MB RPi.

Unfortunately, i was not able to boot the system with the SD card from the old Pi. The old one was running on kernel 3.2.16-rpi1+, which is a bit older. Because of that, i decided to update the kernel and boot stuff to the most current version without touching the rest of the system. This should definitely work with the newer hardware.

Backup current system

It’s always a good idea to have a current backup of the system before playing around. Skip this step at your own risk…

Download current kernel package

Download the current firmware from Github:
https://github.com/raspberrypi/firmware/archive/master.zip

Unpack the file in a temporary folder.

Copy data to SD card

Insert the SD card into your card reader. You should get two new volumes (one on Windows):

  • Boot partition (FAT, Read and Writable, contains “kernel.img” and “bootcode.bin” f.ex. )
  • Data partition (ext2, Read-Only on Mac, Read-Write on Linux, Not visible in Windows)

Remove all data from the boot partition, except the config.txt if you have one.

The unpacked Zip-archive contains a folder named “boot”. Copy the content of this folder to the root of the boot partition.

Copy precompiled kernel modules

If you’re using Linux, you should be able to copy the kernel modules on your PC. If  not, skip the next step.

Linux

The kernel modules are located at the “modules” directory of the Zip-archive. Copy the folder within the modules folder (f.ex. 3.6.11+) to /lib/modules on the data partition of the SD card.

You now should be able to boot the system with all updated kernel modules

Other systems

Eject the SD card, insert it into your Raspberry Pi and boot it.

Connect to the Raspberry Pi.
Execute “uname -r” to veryfy the new kernel version (3.6.11+ in my case).

Now you can either copy the “modules” folder of the Zip-archive on your PC/Mac to your RPi, or download the file again. I did the latter:

wget https://github.com/raspberrypi/firmware/tarball/master
tar xfz master

Move files to mudules directory:

cd raspberrypi-firmware-*
mv modules/* /lib/modules/
cd ..
rm -rf raspberrypi-firmware-*

Reboot the Pi:

reboot

You’re done. The Pi should now boot with all current kernel modules.

Posted in Linux, Raspberry Pi | Leave a comment