Archive | Misc RSS feed for this section

SED Trick – Add slashes to filenames if needed

This sed command will append / to the end of every line in filename.txt which doesn’t already have one. sed -i ‘/\/$/!s/$/\//g’ filename.txt Bookmark on Delicious Digg this post Recommend on Facebook share via Reddit Share with Stumblers Tweet about it Subscribe to the comments on this post Print for later Bookmark in Browser Tell [...]

This sed command will append / to the end of every line in filename.txt which doesn’t already have one.

sed -i '/\/$/!s/$/\//g' filename.txt
View Comments Continue Reading →

Syncing Content Between RackSpace Cloud Servers With Unison

We have two RackSpace Cloud Server running CentOS that need to have the web content kept in sync. Changes on either server need to be replicated to the other server. Easy way to get this accomplished is with the Unison File Syncronizer (http://www.cis.upenn.edu/~bcpierce/unison/). Here are the steps: 1. Install SSH keys for root on each [...]

We have two RackSpace Cloud Server running CentOS that need to have the web content kept in sync. Changes on either server need to be replicated to the other server. Easy way to get this accomplished is with the Unison File Syncronizer (http://www.cis.upenn.edu/~bcpierce/unison/). Here are the steps:

1. Install SSH keys for root on each server so SSH can run without passwords between the servers.

2. Add /etc/hosts file entries to map the private addresses of the servers to names.

192.168.10.1 web1-priv
192.168.10.2 web2-priv

3. Install Unison on each server:

yum install unison227

4. Create a profile on each server at /root/.unison/sync_web.prf containing:

# Reasonable defaults
auto=true
confirmbigdeletes=true
contactquietly=true
group=true
maxthreads=20
numericids=true
owner=true
times=true

# Skip confirmation
batch=true

# Log all sync operations
log=true
logfile=/var/log/unison.log

# Backup deleted files
backup=Name *
backuplocation=central
backupdir=/var/www/vhosts/mydomain.com/backup/
maxbackups=5

# Local root
root=/var/www/vhosts/mydomain.com/httpdocs/

# Remote root (the double forward-slash between IP and remote path is correct)
root=ssh://web2-priv///var/www/vhosts/mydomain.com/httpdocs/

# Resolve conflicts in favor of local root
prefer=newer

# Don't sync (can specify multiple ignore lines)
#ignore=Path */var/cache

Notice the hostname “web2-priv” in red above. The file on web1 should reference web2-priv. The file on web2 should reference web1-priv.

You’ll need to adjust the path listed in the file to match your environment.

5. Next create a script to run Unison with the profile created in the previous step. Name the script /root/sync_web.sh and make it executable.

#!/bin/bash

export HOME=/root
unison sync_web

6. Add an entry to /etc/cronjob to run Unison once per minute:

*/1 * * * * root /root/sync_web.sh > /tmp/sync.log 2>&1

Everything should now be ready to go. You can test by running the sync_web.sh script manually on each server. If there are no errors then try adding, changing and removing files on both servers and verify that the changes are synced within one minute.

View Comments Continue Reading →

Hide NGINX version

For security reasons, if you want to configure nginx to hide it’s version in the server header just add: server_tokens off; Bookmark on Delicious Digg this post Recommend on Facebook share via Reddit Share with Stumblers Tweet about it Subscribe to the comments on this post Print for later Bookmark in Browser Tell a friend

For security reasons, if you want to configure nginx to hide it’s version in the server header just add:

server_tokens off;

View Comments Continue Reading →

Compress Whitespace With tr Shell Command

To replace all whitespace with single spaces pipe the text through: tr -s [[:space:]] ” ” Bookmark on Delicious Digg this post Recommend on Facebook share via Reddit Share with Stumblers Tweet about it Subscribe to the comments on this post Print for later Bookmark in Browser Tell a friend

To replace all whitespace with single spaces pipe the text through:

tr -s [[:space:]] " "
View Comments Continue Reading →

Perl – Sort associative array by value

Here’s an alpha sort of the values of the associative array named “myarray”: foreach my $key (sort {$myarray{$a} cmp $myarray{$b}} keys %myarray) { print “$key : ” . $myarray{$key} . “\n”; }; And here’s the same sort numerically: foreach my $key (sort {$myarray{$a} $myarray{$b}} keys %myarray) { print “$key : ” . $myarray{$key} . “\n”; [...]

Here’s an alpha sort of the values of the associative array named “myarray”:

foreach my $key (sort {$myarray{$a} cmp $myarray{$b}} keys %myarray) {
        print "$key : " . $myarray{$key} . "\n";
};

And here’s the same sort numerically:

foreach my $key (sort {$myarray{$a} <=> $myarray{$b}} keys %myarray) {
        print "$key : " . $myarray{$key} . "\n";
};

And if you want numeric descending:

foreach my $key (sort {$myarray{$b} <=> $myarray{$a}} keys %myarray) {
        print "$key : " . $myarray{$key} . "\n";
};
View Comments Continue Reading →