sigmoid

..oo..oo..oo..oo..oo..oo..

You don’t need Acrobat for this. GhostScript does an excellent job. I’ve used this under Cygwin as well as my gentoo, but should work on any platform gs runs on. Here is a stab at a simple bash script:


#!/bin/bash

usage() {
        echo "$0 {first page} {last page} {input pdf file} {output pdf file}"
}

extract() {
  gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER \
     -dFirstPage=$1 -dLastPage=$2 \
     -sOutputFile=$4 $3
}


EXPECTED_ARGS=4
E_BADARGS=65

if [ $# -ne $EXPECTED_ARGS ]
then
  usage
  exit $E_BADARGS
else
  extract $1 $2 $3 $4
fi

2+

Here is a bash script I wrote to enter and exit a chrooted environment on my Gentoo.

#!/bin/bash

# define here the chroot jail path
CHROOT=/home/chroot-jail

start() {

   echo "Mounting chroot dirs"

   # This allows DNS lookups via your system's networking
   cp -L /etc/resolv.conf ${CHROOT}/etc/

   mount -o bind /proc ${CHROOT}/proc
   mount -o bind /proc/bus/usb ${CHROOT}/proc/bus/usb

   mount -o bind /dev ${CHROOT}/dev
   mount -o bind /dev/pts ${CHROOT}/dev/pts
   mount -o bind /dev/shm ${CHROOT}/dev/shm

   mount -o bind /sys ${CHROOT}/sys

   chroot ${CHROOT} /bin/bash

}

stop() {

   echo "Unmounting chroot dirs"

   umount -f ${CHROOT}/proc/bus/usb >/dev/null
   umount -f ${CHROOT}/proc >/dev/null &

   umount -f ${CHROOT}/dev/pts >/dev/null
   umount -f ${CHROOT}/dev/shm >/dev/null
   umount -f ${CHROOT}/dev >/dev/null &

   umount -f ${CHROOT}/sys >/dev/null &

}

usage() {
   echo "$0 [start|stop]"
}

if [ "$1" == "start" ]
then
   start
elif [ "$1" == "stop" ]
then
   stop
else
   usage
fi

You can find more information on setting up a chroot at the:

0

I like to keep logs and since space is cheap, why not just keep 2 or 3, or maybe 10 years of logs? That’s all fine, but soon the logging directories will become cluttered. Well, there are plenty of ways to keep everything tidy and clean, but even a separate backup isn’t very suitable for me, as backups tend to cycle quicker, and I do want to keep my logs a lot longer. So, how can we go about moving anything older than a certain date into a subdirectory of our logs and let it sit there?

One way I like is a combination of find and rsync.

cd /var/logs/
find .                                                 \ 
     -name BACKUP -prune                               \ 
     -o                                                \ 
     -newermt 20080101 ! -newermt 20101231             \ 
     -type f                                           \ 
     -exec                                             \ 
     rsync -avR --remove-source-files {} ./BACKUP/ \;

It’s quite simple really, ahem… once you know it, but lets decompose the command and try to understand what each part of the command does:

  1. find . — Search into the current directory
  2. -name BACKUP -prune — exclude files/directories matching the pattern BACKUP
  3. -oexpr1 -o expr2 means if expr1 is true do not evaluate expr2, hence whichever file/dir does not match BACKUP will be subjected to the actions of the options next
  4. -newermt 20080101 ! -newermt 20101231 — the modification of the files is between 20080101 and 20101231
  5. -type f — process files only
  6. -exec rsync — execute the following command, in our case rsync. {} is the matched file
  7. -avR --remove-source-files — These are options to rsync!!
    • -a — archive
    • -v — verbose
    • -R — recursive
    • --remove-source-files — delete files once copied
  8. {} ./BACKUP/ — since {} is the filename matched, this is the file that will be copied to the destination ./BACKUP/

If you want to exclude more subdirectories/files, except BACKUP, then you could change step 2 above with:

  1. \( -name BACKUP -o -name mysql \) -prune — this will exclude both BACKUP and mysql
0

To login to remote hosts via ssh without having to type your password all the time, one way is to use Public Key authentication. The easiest way is to copy from your client machine the public key, for instance the contents of ~/.ssh/id_rsa.pub, into the .ssh/authorized_keys of the remote host. To do this use the special utility ssh-copy-id:

ssh-copy-id stathis@remote.host.com

One of the problems I encountered is when you try to use this utility with a remote ssh server that is not listening on the default port. To use a different port use the following syntax:

ssh-copy-id '-p 2222 stathis@remote.host.com'
0

When moving many files across the wire, for example via ftp, sometimes files get corrupted or are transferred partially. Just because two files have the same size it does not mean that they have successfully been transferred. This problem can manifest itself when moving many thousands of files. I usually use either of two methods to verify that files have been transferred properly:

(a) Use file checksums (e.g. using md5deep, sha256deep).
(b) Use rsync’s checksum option.

Using file checksums

To create hashes for a tree of files use the md5deep utility, or the included sha256deep, which computes a message digest for each file using the SHA-256 algorithm. The md5deep project is a collection of utilities using a variety of hashing algorithms to create a checksum for each file. That list of checksums can be then used on the receiving side of a transfer to verify the integrity of the transferred files.

To create a list of file checksums, having relative paths on the transmitting side:

cd files_to_send
sha256deep -rl * > hashes.sha

Now, copy the hashes.sha file to the receiving side, along with the files. To check against this file on the receiving side:

cd files_received
sha256deep -rl -x hashes.sha * > files_did_not_match.txt

The files_did_not_match.txt file will contain any files that have problems. Resend them and verify them again.

Using rsync –checksum

You can also use rsync’s built-in checksum functionality to upload files while verifying their checksum:

rsync -av --checksum user@source.host.com:/uploads/files_to_send/* \
                     user@destination.host.com:/uploads/files_received/ \
                     > sync.log

This is a one-step operation with less hassle, but implies you have rsync. There are many things you can do with rsync, so it is recommended to take a look into its various options if you transfer files, mirror, or sync between hosts often. On Windows, you can download cwrsync, a packaging of cygwin and rsync.

0