Archive for the ‘ Linux ’ Category

Jason’s default .bashrc file

alias mv='mv -i'
alias df='df -h'
alias du='du -sh'
alias vi=vim
alias scp='scp -P 10022'
alias dir='ls -lah'
alias clearall='history -c;clear'
alias cleard='clear;dir'
alias servicerestart='service httpd restart && service mysqld restart && service vsftpd restart'
alias networkrestart='service iptables restart && service network restart'
alias ipconfig=ifconfig
alias version='cat /etc/redhat-release && uname -a'
alias cdh='cd /var/www/html/sites/www.ewea.org'
alias apachify='chown -R apache:apache . ; find ./ -type d -print -exec chmod 775 {} \;; find ./ -type f -print -exec chmod 664 {} \;'
alias send='rsync -avze "ssh -p 10022 -i /root/.ssh/main_prv_key"'
alias wgett='wget --page-requisites'

Postfix doesn’t start after a re-install

Perhaps this might be the solution. Check if there are the necessary sendmail files below.
If you do not have /usr/sbin/sendmail, check if after installing postfix package, you have:

/usr/sbin/sendmail.postfix

If you do, simply make a symlink:

ln -s /usr/sbin/sendmail.postfix /usr/sbin/sendmail

Find out which services are using which ports

netstat -tulpn

[root@main www.ewea.org]# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 95.142.174.150:10022        0.0.0.0:*                   LISTEN      1289/sshd
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      6325/mysqld
tcp        0      0 0.0.0.0:10000               0.0.0.0:*                   LISTEN      25106/perl
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      6360/vsftpd
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      469/master
tcp        0      0 0.0.0.0:10080               0.0.0.0:*                   LISTEN      5944/monitorix-http
tcp        0      0 :::842                      :::*                        LISTEN      1247/xinetd
tcp        0      0 :::80                       :::*                        LISTEN      1750/httpd
tcp        0      0 ::1:25                      :::*                        LISTEN      469/master
udp        0      0 95.142.174.150:123          0.0.0.0:*                               1297/ntpd
udp        0      0 127.0.0.1:123               0.0.0.0:*                               1297/ntpd
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               1297/ntpd
udp        0      0 0.0.0.0:10000               0.0.0.0:*                               25106/perl
udp        0      0 2001:4b98:dc0:51:216:3ef:123 :::*                                    1297/ntpd
udp        0      0 ::1:123                     :::*                                    1297/ntpd
udp        0      0 fe80::216:3eff:fead:9e49:123 :::*                                    1297/ntpd
udp        0      0 :::123                      :::*                                    1297/ntpd

Create a local copy of a web page

Using the following command on linux, you can create a local copy of a web page with all images and files saved:

wget --page-requisites http://example.com/

 

MySQL commands

Backup server data and rsync to offsite server

This is the current backup script used on all the web servers.
(Last updated 26 May, 2016)

#!/bin/bash
# srv.sh
# Backup of files to offsite server
# Jason Bickley, Senior Web Manager, WindEurope
# 26 MAY 2016

#==== RECEIVE VARIABLES FROM COMMAND LINE ====#
FREQ=$1
SERVER=$2
EXCLUDE="--exclude-from=/root/backup/excludelist"
OPTION=$3
DEST=backup.ewea.org
DATE=$(date +"%Y%m%d %T")
LOG=/var/log/backupSRV_log

#==== SET TIMESTAMP =============#
touch /root/.timestamp /etc/.timestamp /var/.timestamp

#==== DAILY BACKUP OF SERVER ====#
rsync -avzhe "ssh -p 10022 -i /root/.ssh/"$SERVER"_prv_key" $EXCLUDE $OPTION / root@$DEST:/backups/$FREQ/$SERVER/

#=== CONFIRM SUCCESS IN LOG FILE ====#
echo $DATE Backup successful! $DEST:/backups/$FREQ/$SERVER/ >> $LOG


To use it, you just have to run: srv.sh {freq} {servername} {option}
For example:

cd /root/Scripts/backup/
./srv.sh daily web1 --delete

Contents of /root/backup/excludelist


/bin
/boot
/cgroup
/db
/dev
/lib
/lib64
/lost+found
/media
/mnt
/opt
/proc
/run
/sbin
/selinux
/srv
/sys
/tmp
/usr
/var/cache
/var/db
/var/empty
/var/ftp
/var/games
/var/gandi
/var/local
/var/lock
/var/lost+found
/var/mail
/var/nis
/var/opt
/var/lib/php/session
/var/preserve
/var/run
/var/tmp
/var/www/html/sites/events
/var/www/html/sites/globalwindday.org
/var/www/html/sites/solutionwind.com
/var/www/html/sites/stats.ewea.org
/var/www/html/sites/testing.ewea.org
/var/www/html/sites/www.ewea.org
/var/www/html/sites/www.ewea.org/fileadmin/videos/health-and-safety
/var/yp

Extracting one database from a large dump file

Use the following command:

sed -n '/^-- Current Database: `test`/,/^-- Current Database: `/p' fulldump.sql > oneDB.sql

Using “SED” command to replace URL strings in SQL files

We want to change all references of events.ewea.org to www.ewea.org in an SQL dump, so a sed command line for this may be:

sed 's/OLDPHRASE/NEWPHRASE/g' database.sql > database.new.sql

sed 's/events\.ewea\.org/www\.ewea\.org/g' event.sql > event.new.sql

Note: We have to escape the periods and any slashes.

This is particularly useful when migrating a WordPress website off the development server and onto the production server.

Passwordless ssh for rsync, etc.

First you need to generate an RSA key on the source server.

ssh-keygen -t rsa

By default, the following files are created: /root/.ssh/id_rsa and /root/.ssh/id_rsa.pub.

Go to /root/.ssh/ and rename the files to the following format:

id_rsa –> server_prv_key and id_rsa.pub –> server_pub_key (where “server” is the hostname)

Next copy the contents across to destination server

Copy contents of server_pub_key and paste it into the /root/.ssh/authorized_keys on the destination server.

Now you can ssh without a password from one machine to another

ssh -p 10022 -i "/root/.ssh/server_prv_key" root@destination

Clear last search highlighting in VIM

By default in VIM, the last searched for word stays highlighted.
Adding this line:

nnoremap <esc> :noh<return><esc>

to this file:

~/.vimrc

allows you to clear the search highlighting just by tapping the ESC key.