This is a short script I use to symlink all my JPG image files on my file server to a flat folder accessible to my DLNA enabled devices.
My files are organized in year folders and under each year there are month folders:
-2011 --2011-01 --2011-02 --2011-03 ... -2012 --2012-01
I avoid duplicate symlinks by using file names like 20110102-1234_DSC…JPG or YYYYMMDD-HHMM_OriginalFileName.JPG
The script looks like this
#!/bin/bash
SRC="/media/Images"
DEST="/media/SymlinkTarget"
# Create a symlink under $DEST for each JPG file under $SRC
find $SRC -type f | grep -i jpg | while read PATHNAME; do
NEW="$DEST";
echo "$NEW";
ln -s "$PATHNAME" "$NEW";
done
Posted by Hans-Henry Jakobsen
This post is a short HOWTO and describes how you can install and run lsyncd to perform a rsync syncronization from local to a remote server using SSH.
Lsyncd is a daemon to continuously synchronize directory trees and relies on inotify. If you need real live syncronization DRBD might be a better alternative since it is a block level syncronization.
Installing Lsyncd 2.0 from source on CentOS 6
Lsyncd is not included as a package in CentOS 6, so you need to download the source file from http://code.google.com/p/lsyncd/downloads/list.
You should have rsync, GCC and lua-devel installed on your system before you continue installing Lsyncd.
# yum install rsync lua-devel
Unpack the lsyncd source file and run the following commands from the unpacked file
# configure # make # make install
make install copies the compiled files and install them to the right directories in your system.
I need to configure a non password SSH communication between the two servers with a shared SSH key.
On the source server run the following command to generate a SSH key, if you have not done this already.
Remember to do this as the user you are going to perform the sync with.
# ssh-keygen
Secure copy the generated SSH key from the source server to your target server
# scp ~/.ssh/id_rsa.pub root@remoteserver:/tmp
On the target server you need to add the copied SSH key to your existing authorized keys file.
Also remember to do this with the user you are going to connect with from the source server.
# cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys
If you do not have this file, just create it using the touch command described below
# touch ~/.ssh/authorized_keys
Test if you can ssh without a password from your source server to the target server.
I have made a config file, /root/scripts/lsyncd.conf that tells Lsyncd where to put the log- and statusfile. That it should be running as a daemon in the background, and a sync should occur after 900 seconds (15 minutes) if there have not been any filesystem changes and there should not be more than 6 parallell Lsyncd processes.
settings = {
logfile = "/tmp/lsyncd.log",
statusFile = "/tmp/lsyncd.status",
nodaemon = false,
maxDelays = 900,
maxProcesses = 6,
}
sync{default.rsyncssh, source="/path/on/source/", host="hostnam.target.server.tld", targetdir="/path/on/target/"}
To start lsyncd you run the command
# lsyncd /root/scripts/lsyncd.conf
You should now see a Lsyncd process running as a daemon on your system. It performs a sync when you start and then waits for any filesystem changes or sync after 900 seconds.
If you would like Lsyncd to start at boot, just add the following line to the bottom of file /etc/rc.local
lsyncd /root/scripts/lsyncd.conf
You do now have a working secure rsync syncronization between two servers.
What directories you are syncing
# tail -f /tmp/lsyncd.status
What is happening now
# tail -f /tmp/lsyncd.log
Tags: CentOS, howto, lsyncd, rsync, ssh, ssh-keygen
Posted by Hans-Henry Jakobsen
This was a short bash script I wrote to help document some startup problems on a server.
The script was grabbing screen dumps from iDRAC during boot on a RHEL6 server, but it can be used on other distributions as well since the console command xwd is common.
#!/bin/bash
# Description:
# Grab screenshot of a specified X-window and wait X-seconds
# Use xwininfo to find the Window id to the window you are going to
# grab screenshots from.
xwininfo="0x4c0001a"
# filename for output
outfile="outfile-"
# seconds sleep between grabs
sleeping=2
padding="000" # put as many padding zeros as you want on filename
for ((i=0; i<1000; i+=1))
do
# Perform the actual screenshot grab
xwd -id $xwininfo -out $outfile-${padding:${#i}}$i.xwd
# Convert the xwd file to a better image format like PNG
convert $outfile-${padding:${#i}}$i.xwd $outfile-${padding:${#i}}$i.png
# Delete the converted XWD-file
rm -f $outfile-${padding:${#i}}$i.xwd
# wait
sleep $sleeping
done
Stop the script after you have grabbed enough screenshots.
I ended up with several files named outfile-000.png outfile-001.png ...
Deleted those files that were not needed and sent the images a documentation of the booting process.
Tags: bash, convert, PNG, windowid, wxd
Posted by Hans-Henry Jakobsen
I wanted to test corruption of a 41TB XFS filesystem and the dd command was the perfect tool for the job.
I used a block size of 512 bytes and wrote 10 blocks of data from device /dev/urandom to the partition /dev/sdb1.
The “Seek” option specifies how many blocks are skipped, counting 100 blocks from the beginning of the partiton, before writing the random/corrupted data.
# dd bs=512 count=10 seek=100 if=/dev/urandom of=/dev/sdb1
Caution!
This procedure WILL most likely corrupt your disk partition or files on the partition. Use it ONLY on test systems and with a valid backup!
Tags: dd
Posted by Hans-Henry Jakobsen
This is a one liner to remove white space in filenames using a linux console
# rename 'y/ /_/' *
You could also do this the other way, remove underscore with space
# rename 'y/_//' *
Tags: rename
Posted by Hans-Henry Jakobsen