1:1 Copy of Hard Disks with Errors

Problem: When you get I/O errors on your computers hard disk, it’s usually urgent to copy all data to a new disk. If you try to do a 1:1 copy using dd, you will see that dd stops when it reaches the first bad sector.

Solution: use the following command to copy from one hard disk to second hard disk, with every bad sector replaced with an all zero sector on the target hard disk

dd if=/dev/sda of=/dev/sdb conv=noerror,sync

With “/dev/sda” as source disk and “/dev/sdb” as destination disk. You can add “bs=4096” or higher to speed up the copy process.

“dd” shows its progress

Problem: “dd” is a useful tool on linux. E.g. you can copy a disk image or wipe a disk, etc. But dd doesn’t show you any progress indicator, and you don’t know how long you have to wait until it has finished.

Solution:   send signal USR1 to the dd process and it will show you how much data it has already transferred at what speed

# dd if=/dev/zero of=/dev/sdc
other terminal # killall -USR1 dd
53768289+0 records in
53768289+0 records out
27529363968 bytes (28 GB) copied, 5025.8 s, 5.5 MB/s

Howto generate an SSL key and self signed cert with openssl

For SSH, HTTPS, TLS SMTP,POPS, IMAPS you need a RSA key pair. Most Linux package installers produce this pairs automatically, but if you like, you can generate them yourself.

The quickest method I found is:

openssl req -x509 -nodes -newkey rsa:2048 -keyout servername.key -out servername.crt -days 1024

This command asks you some questions. The most important one is:

Common Name (e.g. server FQDN or YOUR name)

Enter the hostname of your server here.

You can check the content of key and crt files with these commands:

openssl rsa -in servername.key -text
openssl x509 -in servername.crt -text

Unix/Linux date command fails when calculating “yesterday”

Problem: A shell-script running every day short after midnight doesn’t work properly once per year after daylight-saving time adjustment. e.g. an expected logfile is missing (“somefile.YYYYMMDD.log”)

Discussion: The Unix date command calculates the wrong date when used with “–date yesterday” on the day after daylight-saving time adjustment.

# date
Mon Mar 31 00:15:03 CEST 2014
# date --date 'yesterday'
Sat Mar 29 23:15:09 CET 2014

Date obviously uses “24 hours ago” for yesterday. The DST adjustment day has only 23 hours, so “yesterday” is calculated wrong, between 0:00 and 1:00 on these days.

It’s interessting that “yesterday” is not allways equal to “24 hours ago”:

# date --date "2014-03-31 0:15 yesterday"
Sun Mar 30 00:15:00 CET 2014
# date --date "2014-03-31 0:15 24 hours ago"
Sat Mar 29 23:15:00 CET 2014

 

Solution: I changed the “yesterday” date calculation for nightly jobs after midnight to:

# date --date "12:00 yesterday" +%Y%m%d