Category Archives: shell script

Send attachments from command line with mutt

To send e-mails from command line with attachments using mutt.

Set the from address with EMAIL=
-s – Subject
-a – attachment file
recipient name
-c – for CC
-b – for BCC
create a text file (eg: /tmp/testmessage) , with the body of the message.

EMAIL="foo@bar" mutt -s "Subject" -a test.doc foo1@bar -c foo2@bar < /tmp/testmessage

Upgrade, Restore Drupal 7

Shell script to upgrade and restore Drupal 7 website
This script will take care of the necessary actions required for upgrading drupal to higher versions.

USAGE

  • Copy the script to your webserver.
  • Edit the script and change the variables to match with your setup
  • Give execute privilege to the owner of the script (chmod u+x upgrade-restore-drupal7.sh)
  • Execute the script ./upgrade-restore-drupal7.sh

UPGRADE

$ ./upgrade-restore-drupal7.sh 
 Please enter your choice:
 1. Update drupal
 2. Restore an old installation from backup
 3. Exit
1
Please enter the new drupal version (eg: 7.15) : 
7.18
Downloading drupal-7.18
Downloaded the the drupal version drupal-7.18
Current site backup is created: /home/foo/backups/08-01-2013-0938
Database backup created: /home/foo/backups/08-01-2013-0938.sql
Site is in maintanence mode now
Removed all drupal core files from destination
Copied the new version contents
Drupal updated to drupal-7.18
Site is active again, but please update your database, please visit http://<yourwebsite>/update.php to finalize the process
Removed the source files

RESTORE

$ ./upgrade-restore-drupal7.sh 
 Please enter your choice:
 1. Update drupal
 2. Restore an old installation from backup
 3. Exit
2
List of available backups
08-01-2013-0753
08-01-2013-0758
08-01-2013-0804
08-01-2013-0841
08-01-2013-0849
08-01-2013-0858
08-01-2013-0900
08-01-2013-0904
08-01-2013-0905
08-01-2013-0938
Please enter the backup file name to restore: (eg: 08-01-2013-0753): 
08-01-2013-0905
Site is offline now
Removed production files
Restored the filesystem backup 
Restored the database
Site is restored

View on github

Mysql backup script

To backup mysql on a daily/hourly basis with time stamp and compress it after backup also it will remove the files older than x days.

#!/bin/bash
# Arun N S
# variables
DATE="$(date +"%d-%m-%Y")"
TIME="$(date +"%d-%m-%Y-%H%M")"
USER=username
PASSWORD=password
DATABASE=dbname


# Directories and dump
/bin/mkdir -p /backup/Mysql/$DATE
/usr/bin/mysqldump -l -F -u $USER --password=$PASSWORD $DATABASE > /backup/Mysql/$DATE/backup_$TIME.sql


# Compressing
/usr/bin/bzip2 /backup/Mysql/*/*.sql


#Removing files older than x days eg: 90 days
for i in `/usr/bin/find /backup/Mysql/ -maxdepth 1 -type d -mtime +90 -print`; do
/bin/echo -e "Deleting old directories $i"; /bin/rm -rf $i; done

Find Tips

Copy/Move files with find

find <path> -name "filename" -exec cp -prf {} /destination/{} ;
find /var/log/ -name "m*" -exec cp -prf {} /tmp/message/{} ;

This will create the same directory structure under /tmp/message, incase you want all subdirectory files under /tmp/message/ remove the {} .

Remove files older than certain days (using find/mtime)
find -name "" -mtime +N -exec rm -r {} ;

Eg : find /var/log/ -name "*.log" -mtime +5 -exec rm -r {} ;
This will remove the *.log files older than 5 days in directory /var/log/

Find with file type

directories : find / -type d -print0
files: find / -type f -print0

script to convert openssh keys to tectia and tectia to openssh


#!/bin/bash
# Arun N S
menu="
Make your choice:
1. Openssh key to Tectia format
2. Tectia key to Openssh format
3. Exit"
while :
do
printf "%sn: " "$menu"
read choice
case $choice in
"1") echo "Converting Openssh key to Tectia format"
echo "Enter key Path(full path):"
while read INPUT
do
ssh-keygen -e -f ${INPUT} > ${INPUT}.tectia
echo "OpenSSH format key is saved as: ${INPUT}.tectia"
exit 0
done
break;;
"2") echo "Converting Tectia key to Openssh format";
echo "Enter File Path(full path):"
while read INPUT
do
ssh-keygen -i -f ${INPUT} > ${INPUT}.openssh
echo "OpenSSH format key is saved as: ${INPUT}.openssh"
exit 0
done
break;;
"3") echo "3. Exit"
echo " Good Bye"
break;;
*) echo "Wrong Choice"
;;
esac
done

./arun