Archive for the ‘ Bash ’ Category

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

Backup database and rsync to offsite server

This is the current bash script for exporting the databases from each web server and sending them to the backup server.

#!/bin/bash
# db.sh
# Backup of database to offsite server
# Jason Bickley, Web Manager EWEA
# 9 JULY 2013

#==== RECEIVE VARIABLES FROM COMMAND LINE ====#
FREQ=$1
SERVER=$2

#==== SCRIPT OPTIONS ====#
USER=backup
PASS="mwbubCEsxCU6XVsW"
DIR=/root/backup/mysql/
FILE=localhost.sql.gz
DEST=backup.ewea.org
DATE=$(date +"%Y%m%d %T")
LOG=/var/log/backupDB_log

#==== EXCUTION OF COMMANDS ====#
# Change operating directory
cd $DIR

#==== Export database ====#
mysqldump -u$USER -p$PASS --all-databases --lock-all-tables | gzip > $SERVER.$FREQ.$FILE
chmod 600 $SERVER.$FREQ.$FILE

#==== rsync the export to offsite server ====#
rsync -aze "ssh -p 10022 -i /root/.ssh/"$SERVER"_prv_key" $SERVER.$FREQ.$FILE root@$DEST:/backups/$FREQ/$SERVER/db/

#==== delete the exported file ====#
rm -f $SERVER.$FREQ.$FILE

#==== Confirm success in log file ====#
echo $DATE Backup successful! $DEST:/backups/$FREQ/$SERVER/db/$SERVER.$FREQ.$FILE >> $LOG

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

cd /root/Scripts/backup/
./db.sh daily main