MySQL: Repair & Optimize all Tables in all Databases

$ mysqlcheck -u root -p --auto-repair --check --optimize --all-databases

Posted in MySQL | Tagged , , , | Leave a comment

Write-protect .svn directories

Write protect
$ tree -fiad | grep .svn$ | xargs -I '{}' chmod ugo-w '{}' -R

Write enable
$ tree -fiad | grep .svn$ | xargs -I '{}' chmod ugo+w '{}' -R

Posted in Linux, subversion | Tagged , , | Leave a comment

Debian: change timezone

$ date
Mon Apr 23 03:53:51 PDT 2012
$ sudo dpkg-reconfigure tzdata

Current default time zone: 'Europe/Zurich'
Local time is now: Mon Apr 23 12:58:04 CEST 2012.
Universal Time is now: Mon Apr 23 10:58:04 UTC 2012.

$ date
Mon Apr 23 12:58:08 CEST 2012

Posted in Debian, Linux Administration | Tagged , , | Leave a comment

sed one liner: remove closing PHP tag

$ sed -i 's/\(.*\)?>/\1/' your-script.php

Posted in Linux, PHP | Tagged , | Leave a comment

sed edit in place deleting matching lines

This command deletes the line that contains “remove-matching-line” in files that matches *.txt wildcard.
$ ls -1 *.txt | xargs -I '{}' sed -i '/^.*remove-matching-line.*$/d' '{}'

A hacker inserted eval(base64_decode(“obfuscated_code”)) on a client’s PHP files. I used this command to remove anything that matches “eval(base64_decode”
$ tree -fi | grep .php$ | xargs -I '{}' sed -i '/^.*eval(base64_decode.*$/d' '{}'

Posted in Bash script, Linux | Tagged , | Leave a comment

Remove duplicates from MySQL Table

ALTER IGNORE TABLE categories ADD PRIMARY KEY(categories_id);

Posted in MySQL, SQL | Tagged , | Leave a comment

Create Linux RAID Level 1 from 4 TB Harddisk

Partition the disk into two equal parts.

# parted /dev/sda
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
(parted) print
Model: Areca ARC-1212-VOL#000 (scsi)
Disk /dev/sda: 4,00TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags

(parted) unit TB
(parted) mkpart primary 0.00TB 2.00TB
(parted) p
Model: Areca ARC-1212-VOL#000 (scsi)
Disk /dev/sda: 4,00TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 0,00TB 2,00TB 2,00TB primary

(parted) mkpart primary 2.00TB 4.00TB
(parted) p
Model: Areca ARC-1212-VOL#000 (scsi)
Disk /dev/sda: 4,00TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 0,00TB 2,00TB 2,00TB primary
2 2,00TB 4,00TB 2,00TB primary

(parted) quit
Information: You may need to update /etc/fstab.

Create RAID Level 1 with two partitions

# mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sda2
mdadm: /dev/sda1 appears to contain an ext2fs file system
size=1953124352K mtime=Thu Jan 1 00:00:00 1970
mdadm: Note: this array has metadata at the start and
may not be suitable as a boot device. If you plan to
store '/boot' on this device please ensure that
your boot-loader understands md/v1.x metadata, or use
--metadata=0.90
Continue creating array? yes
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

Format the RAID device to make it usable

# mkfs.ext4 /dev/md0
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
122077184 inodes, 488280286 blocks
24414014 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
14902 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

See RAID stats

# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sda2[1] sda1[0]
1953121144 blocks super 1.2 [2/2] [UU]
[>....................] resync = 0.3% (5876672/1953121144) finish=971.4min speed=33405K/sec

unused devices:

See RAID summary

# mdadm /dev/md0
/dev/md0: 1862.64GiB raid1 2 devices, 0 spares. Use mdadm --detail for more detail.

See RAID details

# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Mon Feb 13 16:44:45 2012
Raid Level : raid1
Array Size : 1953121144 (1862.64 GiB 2000.00 GB)
Used Dev Size : 1953121144 (1862.64 GiB 2000.00 GB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent

Update Time : Mon Feb 13 16:45:11 2012
State : active, resyncing
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0

Rebuild Status : 0% complete

Name : 0
UUID : 2cdd2ca1:746afbbd:e64471cf:10746a7f
Events : 1

Number Major Minor RaidDevice State
0 8 1 0 active sync /dev/sda1
1 8 2 1 active sync /dev/sda2
# Done!

Posted in Uncategorized | Leave a comment

Vim set file encoding

:set bomb
:set fileencoding=utf-8

Posted in Uncategorized | Tagged , | Leave a comment

MySQL regex update

Simple solution:

UPDATE  table
SET     words = REPLACE(words, 'brown', 'green')
WHERE   words REGEXP 'brown ' OR words REGEXP ' brown'

Better solution from http://techras.wordpress.com/2011/06/02/regex-replace-for-mysql/:

SET sql_mode='NO_BACKSLASH_ESCAPES';

DELIMITER $$
CREATE FUNCTION  `regex_replace`(pattern VARCHAR(1000),replacement VARCHAR(1000),original VARCHAR(1000))

RETURNS VARCHAR(1000)
DETERMINISTIC
BEGIN
 DECLARE temp VARCHAR(1000);
 DECLARE ch VARCHAR(1);
 DECLARE i INT;
 SET i = 1;
 SET temp = '';
 IF original REGEXP pattern THEN
  loop_label: LOOP
   IF i>CHAR_LENGTH(original) THEN
    LEAVE loop_label;
   END IF;
   SET ch = SUBSTRING(original,i,1);
   IF NOT ch REGEXP pattern THEN
    SET temp = CONCAT(temp,ch);
   ELSE
    SET temp = CONCAT(temp,replacement);
   END IF;
   SET i=i+1;
  END LOOP;
 ELSE
  SET temp = original;
 END IF;
 RETURN temp;
END$$
DELIMITER ;
Posted in MySQL | Leave a comment

Number format by country

Source: International Number Formats

Format Country
1,234.56 - Invariant Language (Invariant Country)
ar-SA – Arabic (Saudi Arabia)
zh-TW – Chinese (Taiwan)
en-US – English (United States)
he-IL – Hebrew (Israel)
ja-JP – Japanese (Japan)
ko-KR – Korean (Korea)
th-TH – Thai (Thailand)
ur-PK – Urdu (Islamic Republic of Pakistan)
hy-AM – Armenian (Armenia)
af-ZA – Afrikaans (South Africa)
hi-IN – Hindi (India)
sw-KE – Kiswahili (Kenya)
pa-IN – Punjabi (India)
gu-IN – Gujarati (India)
ta-IN – Tamil (India)
te-IN – Telugu (India)
kn-IN – Kannada (India)
mr-IN – Marathi (India)
sa-IN – Sanskrit (India)
kok-IN – Konkani (India)
syr-SY – Syriac (Syria)
dv-MV – Divehi (Maldives)
ar-IQ – Arabic (Iraq)
zh-CN – Chinese (People’s Republic of China)
en-GB – English (United Kingdom)
es-MX – Spanish (Mexico)
ar-EG – Arabic (Egypt)
zh-HK – Chinese (Hong Kong S.A.R.)
en-AU – English (Australia)
ar-LY – Arabic (Libya)
zh-SG – Chinese (Singapore)
en-CA – English (Canada)
es-GT – Spanish (Guatemala)
ar-DZ – Arabic (Algeria)
zh-MO – Chinese (Macao S.A.R.)
en-NZ – English (New Zealand)
ar-MA – Arabic (Morocco)
en-IE – English (Ireland)
es-PA – Spanish (Panama)
ar-TN – Arabic (Tunisia)
en-ZA – English (South Africa)
es-DO – Spanish (Dominican Republic)
ar-OM – Arabic (Oman)
en-JM – English (Jamaica)
ar-YE – Arabic (Yemen)
en-029 – English (Caribbean)
ar-SY – Arabic (Syria)
en-BZ – English (Belize)
es-PE – Spanish (Peru)
ar-JO – Arabic (Jordan)
en-TT – English (Trinidad and Tobago)
ar-LB – Arabic (Lebanon)
en-ZW – English (Zimbabwe)
ar-KW – Arabic (Kuwait)
en-PH – English (Republic of the Philippines)
ar-AE – Arabic (U.A.E.)
ar-BH – Arabic (Bahrain)
ar-QA – Arabic (Qatar)
es-SV – Spanish (El Salvador)
es-HN – Spanish (Honduras)
es-NI – Spanish (Nicaragua)
es-PR – Spanish (Puerto Rico)
zu-ZA – Zulu (South Africa)
xh-ZA – Xhosa (South Africa)
tn-ZA – Tswana (South Africa)
quz-PE – Quechua (Peru)
cy-GB – Welsh (United Kingdom)
fil-PH – Filipino (Philippines)
iu-Latn-CA – Inuktitut (Latin) (Canada)
mi-NZ – Maori (New Zealand)
ga-IE – Irish (Ireland)
moh-CA – Mohawk (Canada)
ns-ZA – Northern Sotho (South Africa)
mt-MT – Maltese (Malta)
1.234,56 ca-ES – Catalan (Catalan)
da-DK – Danish (Denmark)
de-DE – German (Germany)
el-GR – Greek (Greece)
is-IS – Icelandic (Iceland)
it-IT – Italian (Italy)
nl-NL – Dutch (Netherlands)
pt-BR – Portuguese (Brazil)
ro-RO – Romanian (Romania)
hr-HR – Croatian (Croatia)
sq-AL – Albanian (Albania)
sv-SE – Swedish (Sweden)
tr-TR – Turkish (Turkey)
id-ID – Indonesian (Indonesia)
sl-SI – Slovenian (Slovenia)
lt-LT – Lithuanian (Lithuania)
vi-VN – Vietnamese (Vietnam)
eu-ES – Basque (Basque)
mk-MK – Macedonian (Former Yugoslav Republic of Macedonia)
fo-FO – Faroese (Faroe Islands)
ms-MY – Malay (Malaysia)
gl-ES – Galician (Galician)
fr-BE – French (Belgium)
nl-BE – Dutch (Belgium)
pt-PT – Portuguese (Portugal)
sr-Latn-CS – Serbian (Latin, Serbia)
ms-BN – Malay (Brunei Darussalam)
de-AT – German (Austria)
es-ES – Spanish (Spain)
sr-Cyrl-CS – Serbian (Cyrillic, Serbia)
de-LU – German (Luxembourg)
es-CR – Spanish (Costa Rica)
es-VE – Spanish (Venezuela)
es-CO – Spanish (Colombia)
es-AR – Spanish (Argentina)
es-EC – Spanish (Ecuador)
es-CL – Spanish (Chile)
es-UY – Spanish (Uruguay)
es-PY – Spanish (Paraguay)
es-BO – Spanish (Bolivia)
sr-Cyrl-BA – Serbian (Cyrillic) (Bosnia and Herzegovina)
fy-NL – Frisian (Netherlands)
se-SE – Sami (Northern) (Sweden)
sma-SE – Sami (Southern) (Sweden)
hr-BA – Croatian (Bosnia and Herzegovina)
bs-Latn-BA – Bosnian (Bosnia and Herzegovina)
bs-Cyrl-BA – Bosnian (Cyrillic) (Bosnia and Herzegovina)
arn-CL – Mapudungun (Chile)
quz-EC – Quechua (Ecuador)
sr-Latn-BA – Serbian (Latin) (Bosnia and Herzegovina)
smj-SE – Sami (Lule) (Sweden)
quz-BO – Quechua (Bolivia)
1’234.56 de-CH – German (Switzerland)
it-CH – Italian (Switzerland)
fr-CH – French (Switzerland)
de-LI – German (Liechtenstein)
rm-CH – Romansh (Switzerland)
1 234,56 bg-BG – Bulgarian (Bulgaria)
cs-CZ – Czech (Czech Republic)
fi-FI – Finnish (Finland)
fr-FR – French (France)
hu-HU – Hungarian (Hungary)
nb-NO – Norwegian, Bokmål (Norway)
pl-PL – Polish (Poland)
ru-RU – Russian (Russia)
sk-SK – Slovak (Slovakia)
uk-UA – Ukrainian (Ukraine)
be-BY – Belarusian (Belarus)
lv-LV – Latvian (Latvia)
az-Latn-AZ – Azeri (Latin, Azerbaijan)
ka-GE – Georgian (Georgia)
uz-Latn-UZ – Uzbek (Latin, Uzbekistan)
tt-RU – Tatar (Russia)
mn-MN – Mongolian (Cyrillic, Mongolia)
nn-NO – Norwegian, Nynorsk (Norway)
sv-FI – Swedish (Finland)
az-Cyrl-AZ – Azeri (Cyrillic, Azerbaijan)
uz-Cyrl-UZ – Uzbek (Cyrillic, Uzbekistan)
fr-CA – French (Canada)
fr-LU – French (Luxembourg)
fr-MC – French (Principality of Monaco)
sma-NO – Sami (Southern) (Norway)
smn-FI – Sami (Inari) (Finland)
se-FI – Sami (Northern) (Finland)
sms-FI – Sami (Skolt) (Finland)
smj-NO – Sami (Lule) (Norway)
lb-LU – Luxembourgish (Luxembourg)
se-NO – Sami (Northern) (Norway)
1,234/56 fa-IR – Persian (Iran)
1 234-56 kk-KZ – Kazakh (Kazakhstan)
ky-KG – Kyrgyz (Kyrgyzstan)
1 234.56 et-EE – Estonian (Estonia)

Thanks to Igor Krupitsky.

Posted in General | Tagged | Leave a comment