Wednesday, November 25, 2009

How to back up a system to another server using rsync and get an email with a result

1. Create a trust relationship between the system which needs to backed up and the system where the files will get backed up.

Replace ALPHA and BETA with the proper server names. (to add more server names, "sudo nano /etc/hosts" and add the ip and the name you wish to assign to each server)

ALPHA = server 1, where to log in from (on ALPHA, do this as user root)
BETA = server 2, destination where we log in

ALPHA: ssh-keygen -t rsa
BETA: mkdir .ssh
ALPHA: cat .ssh/id_rsa.pub | ssh user@BETA 'cat >> .ssh/authorized_keys'
BETA: chmod 644 .ssh/authorized_keys

2. As root, create a backup script

Replace "abc" with the name of your server which you are backing up.

Create the file: "nano /usr/bin/backupabc" and paste the script below, change:
- the backup server name, ex: mybackupserver
- the user id you use on the backup server, ex: my-user-id-on-backup-server
- the backup paths on the backup server, ex: /mnt/mybigdrive/backups/abc/
- your email address, ex: my.lovely.email@gmail.com (make sure you install mail: "sudo apt-get install mailutils" )


#!/bin/sh

LOG=/tmp/backupabc.log

START=$(date +%s)
echo "" > $LOG
echo "Start " >> $LOG
echo `date` >> $LOG

rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /bin/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/bin/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /boot/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/boot/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /etc/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/etc/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /home/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/home/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /lib/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/lib/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /opt/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/opt/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /root/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/root/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /sbin/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/sbin/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /srv/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/srv/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /usr/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/usr/ >> $LOG
rsync --verbose --links --recursive --delete-during --human-readable --progress --itemize-changes /var/ my-user-id-on-backup-server@mybackupserver:/mnt/mybigdrive/backups/abc/var/ >> $LOG

END=$(date +%s)
DIFF=$(( $END - $START ))

echo "I have ran the /usr/bin/backupabc script and it took $DIFF seconds" >> $LOG
echo "\nEnd " >> $LOG
echo `date` >> $LOG

cat $LOG |  mail -s "mybackupserver: backed up abc" my.lovely.email@gmail.com



3. As root, run the script manually:
/usr/bin/backupabc
OR
add the script to the crontab to run every day at 10 pm (22 hrs) (as root):
crontab -e   (if prompted, use "nano" as the editor)
0 22 * * * /usr/bin/backupabc

To see the log while it's being built, open another shell and:
tail -f /tmp/backupabc

Tuesday, November 24, 2009

How to set up Apache and limit access per IP - mod_limitipconn.so module

# Get Apache with the apxs2 tool
apt-get install apache2-threaded-dev

# test that apxs works
which apxs2


nano /etc/apache2/apache2.conf

and add this at the bottom:

# This command is always needed
ExtendedStatus On

# Only needed if the module is compiled as a DSO
LoadModule limitipconn_module lib/apache/mod_limitipconn.so

<IfModule mod_limitipconn.c>

    # Set a server-wide limit of 10 simultaneous downloads per IP,
    # no matter what.
    MaxConnPerIP 10
    <Location /somewhere>
        # This section affects all files under http://your.server/somewhere
        MaxConnPerIP 3
        # exempting images from the connection limit is often a good
        # idea if your web page has lots of inline images, since these
        # pages often generate a flurry of concurrent image requests
        NoIPLimit image/*
    </Location>

    <Directory /home/*/public_html>
        # This section affects all files under /home/*/public_html
        MaxConnPerIP 1
        # In this case, all MIME types other than audio/mpeg and video*
        # are exempt from the limit check
        OnlyIPLimit audio/mpeg video
    </Directory>
</IfModule>

# Modify the "/somewhere" to match the alias (not directory) which you are protecting.



# Add this mod at the bottom of the actions.load file:
  cd /etc/apache2/mods-available
  nano actions.load
# Add this at the end of the file:
  LoadModule evasive20_module /usr/lib/apache2/modules/mod_evasive20.so

# edit the httpd conf (not the apache2.conf) config file:
  nano /etc/apache2/httpd.conf
# add the following 2 comments at the bottom of the file, with the pound sign in front,
# this will ensure that in the following steps, the "make install" won't barf.

# Dummy LoadModule directive to aid module installations
#LoadModule dummy_module /usr/lib/apache2/modules/mod_dummy.so




# Download the limit ip connection module and set it up
  wget http://dominia.org/djao/limit/mod_limitipconn-0.23.tar.bz2
  tar -jxvf mod_limitipconn-0.23.tar.bz2
  cd mod_limitipconn-0.23
  nano Makefile
# Look for apxs and modify it to apxs2
  make
  make install
# If the "make install" barfs with an error such as:
  apxs:Error: Activation failed for custom /etc/apache2/httpd.conf file..
  apxs:Error: At least one `LoadModule' directive already has to exist..
then you forgot to edit the httpd.conf file and add the dummy module entry (see above).

Friday, November 20, 2009

How to convert an .avi to .mpeg in Ubuntu

sudo apt-get install libavcodec-unstripped-51
sudo apt-get install ffmpeg
ffmpeg -i holiday.avi -aspect 16:9 -target ntsc-dvd holiday.mpeg
(and then wait a long time)

Sunday, November 15, 2009

How to convert uif to iso

This information is copied from: http://wesleybailey.com/articles/convert-uif-to-iso
Tested successfuly.
-----------------------------------


Convert UIF to ISO

The fastest way to convert an UIF image to ISO image is UIF2ISO. It is a speedy command line tool, that will save you the hassle of installing wine and MagicISO.

This is how I downloaded and installed UIF2ISO, written by Luigi Auriemma. - http://aluigi.altervista.org/

1. We first need to install zlib and OpenSSL with apt-get.

sudo apt-get install zlib1g zlib1g-dev libssl-dev build-essential

2. Now we can download UIF2ISO with wget from a terminal, or from the author’s site here.

wget http://aluigi.altervista.org/mytoolz/uif2iso.zip

3. Once you have the file downloaded, unzip it and cd into the directory.

unzip uif2iso.zip
cd src

4. Finally compile the source, and create the executable.

make
sudo make install

5. Now you can convert the .uif file to an .iso with the following command:

uif2iso example.uif output.iso

Mounting an ISO

You don't necessarily need to burn a cd in order to access the files within the ISO. You can mount it with some simple commands.

Here is how to mount the ISO from command line.

sudo modprobe loop
sudo mkdir ISO_directory
sudo mount /media/file.iso /media/ISOPoint/ -t iso9660 -o loop


Friday, November 13, 2009

Eratosthenes Sieve prime number benchmark in Java




// Eratosthenes Sieve prime number benchmark in Java
import java.awt.*;

public class Sieve // extends java.applet.Applet implements Runnable {
{
String results1, results2;

void runSieve()
{
int SIZE = 8190;
boolean flags[] = new boolean[SIZE+1];
int i, prime, k, iter, count;
int iterations = 0;
double seconds = 0.0;
int score = 0;
long startTime, elapsedTime;

startTime = System.currentTimeMillis();
while (true) {
count=0;
for(i=0; i<=SIZE; i++) flags[i]=true;
for (i=0; i<=SIZE; i++) {
if(flags[i]) {
prime=i+i+3;
for(k=i+prime; k<=SIZE; k+=prime)
flags[k]=false;
count++;
}
}
iterations++;
elapsedTime = System.currentTimeMillis() - startTime;
if (elapsedTime >= 10000) break;
}
seconds = elapsedTime / 1000.0;
score = (int) Math.round(iterations / seconds);
results1 = iterations + " iterations in " + seconds + " seconds";
if (count != 1899)
results2 = "Error: count <> 1899";
else
results2 = "Sieve score = " + score;
}

public static void main(String args[])
{
Sieve s = new Sieve();
}

public Sieve()
{
System.out.println("Running Sieve - please wait 10 seconds for results...");
runSieve();
System.out.println( results1 );
System.out.println( results2 );
}

}



Wednesday, November 11, 2009

Ubuntu: How to fix the apt-get update error: W: GPG error: http://ppa.launchpad.net intrepid Release: The following signatures couldn't be verified be

The problem is during apt-get update:

...
Reading package lists... Done
W: GPG error: http://ppa.launchpad.net intrepid Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 8B9FBE5158B3AFA9
W: You may want to run apt-get update to correct these problems


Solution:

gpg --keyserver keyserver.ubuntu.com --recv 8B9FBE5158B3AFA9
gpg --export --armor 8B9FBE5158B3AFA9 | sudo apt-key add -


Update should work now:

sudo apt-get update

Sunday, November 8, 2009

How to mount a remote file system in Ubuntu

# install the utility
sudo apt-get install sshfs

# make a directory where to mount the remote file system
sudo mkdir /mnt/backups
sudo chown YOURUSERNAME /mnt/alpha

# mount the remote drive
sshfs YOURUSERNAME@192.168.1.123:/home/YOURUSERNAME/backups /mnt/backups

# check to see that the files are mounted
ls -la /mnt/backups

How to listen to mp3s in Ubuntu/Linux

sudo apt-get install amarok
sudo apt-get install libxine1-ffmpeg

(Amarok needs the libxine codec to decode mp3s)

Saturday, November 7, 2009

How to log into another server without asking you for a pasword - in 4 steps.

ALPHA = server 1, where to log in from
BETA = server 2, destination where we log in


ALPHA: ssh-keygen -t rsa
BETA: mkdir .ssh
ALPHA: cat .ssh/id_rsa.pub | ssh user@BETA 'cat >> .ssh/authorized_keys'
BETA: chmod 644 .ssh/authorized_keys


To establish a mirror relationship, exchange server ALPHA with BETA and run through the 4 steps again.