Posts

Useful 'sed' examples

1. Replace the first line of a file with a string      sed -i '1 s/^.*$/string/' FILE 2. Replace an entire line containing a string with string2      sed -i '/string/c\string2' FILE                         0r      sed -i 's/.*string.*/string2/g' FILE 3. Remove an entire line containing a string          sed -i '/string/d' FILE 4. To replace a word with word2      sed -i 's/word/word2/g' FILE 5. Create a backup first before replacing the word     sed -i.bak 's/word/word2/g' FILE

Rsync Incremental Daily Backup

#!/bin/bash # SSH keys need to be added so that the backup can be transferred to backup server. # By Sileep Kumar M S <sileepkumar@gmail.com> # Source Directory SRC=/home # Destination server IP HOST=192.168.2.16      # Destination Directory Name DST_DIR=/BACKUPS       # Assuming SSH port is 22 PORT=22                # A list of files or directories to exclude. EXCLUDES=/root/excludes.txt     # Rsync binary path RSYNC=/usr/bin/rsync # Custom ssh command (like ssh -i keyfile -l remote_user) SSH=/usr/bin/ssh #Todays date in ISO-8601 format: DAY0=`date -I` #Yesterdays date in ISO-8601 format: DAY1=`date -I -d "1 day ago"` #31 days ago in ISO-8601 format (to keep 30days backup) DAY31=`date -I -d "31 days ago"` #Rsync Process rsync -e "$SSH -p $PORT" --rsync-path=$RSYNC -az --force --delete --ignore-errors --e...

Usable scripts for Cpanel Servers

1. To find last 30 min load average in a 5 min interval awk -F- '/average/ {print $2}' /var/log/dcpumon/toplog.* 2. To find spamming source directories awk '/cwd.*home/ {print $3}' /var/log/exim_mainlog|sed 's/cwd=//g'|sort|uniq -c|sort -nk 1 3. To find ongoing spamming script directories tail -f /var/log/exim_mainlog|awk '/cwd.*home/ {print $3}'|sed 's/cwd=//' 4. Grep last 10000 lines in exim log to find spam source tail -10000 /var/log/exim_mainlog|awk '/cwd.*home/ {print $3}'|sed 's/cwd=//'|sort|uniq                  Or add a 'c' to view the count                    tail -10000 /var/log/exim_mainlog|awk '/cwd.*home/ {print $3}'|sed 's/cwd=//'|sort|uniq -c 5. Find currently running scripts ps aux |awk -F: '/home/ {print $4}' 6. Find number of connections to port 80 and sort per IP ne...

How to mirror a remote server to local folder with lftp

To mirror a remote server to local folder with lftp lftp sftp://user:password@server.org:22 -e 'mirror --verbose --use-pget-n=8 -c /remote/path /local/path' sftp:// = uses SFTP protocol mirror = mirror mode verbose = shows the files being downloaded use-pget-n = number of segments, realy useful to speed up big files parallel = downloads multiplier files at the same time if you want to download files in parallel switch out use-pget-n=8 with --parallel=8

Add swap to a running VPS (KVM, dedi)

For 2GB swap: dd if=/dev/zero of=/swap bs=1M count=2000 mkswap /swap swapon /swap Add the below entry to fstab: vi /etc/fstab /swap swap swap defaults 0 0

MySQL table conversion

To convert all InnoDB Tables to MyISAM (all databases) SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ' engine=MyISAM;') FROM information_schema.TABLES WHERE ENGINE = 'InnoDB'; To convert all MyISAM Tables to InnoDB (all databases) SELECT CONCAT('ALTER TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ' engine=InnoDB;') FROM information_schema.TABLES WHERE ENGINE = 'MyISAM'; Run the single command from the provided list of commands got from the above result to convert the tables

Load kernel from grub menu

If OS fails to load kernel and it sends you back to grub menu, please follow the below steps to load the kernel manually. grub> find /boot/grub/stage1  (hd0,0) grub>root (hd0,0) grub>kernel /boot/vmlinuz-2.6.32-279.el6.x86_64 root=/dev/vda1 ro (Load appropriate kernel in your server) grub>initrd /boot/initramfs-2.6.32-279.el6.x86_64.img grub>boot If the certain kernel fails to load, reboot and select a lower version of kernel in the above commands to boot the OS. These changes are only temporary and will be flushed upon next reboot. Please make sure to update your grub.conf for a permanent solution.