bash « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ bash ’

Bash script to open a terminal with multiple tabs on Linux start-up

Posted by on May 25th, 2012

All of the tasks performed by us after system startup such as opening a browser, MySql etc can be automated by following the steps mentioned below :

1. Write a script:-

sleep 1m;gnome-terminal --geometry=150x50 --tab --title="echo" -e "bash -c \"echo "hello";echo "there";exec bash\"" --tab --title="idea" -e "bash -c \"/opt/idea-IU-111.69/bin/idea.sh;exec bash\"" --tab --title="sql" -e "bash -c \"mysql -uroot -pigdefault;\"" --tab --title="firefox" -e "bash -c \"/usr/bin/firefox www.gmail.com;\""

The script above will open a terminal with 4 tabs :

  1. echo “hello there”
  2. Idea
  3. MySql
  4. Firefox

The script will also set the titles in the terminal for each tab.

One can personalize the above script accordingly.

Description of the script :-

sleep 1m                       : Executes the script after 1 minute so that system finishes its startup process.
gnome-terminal             : Open a terminal.
–geometry=150×50       : Set screen size for terminal.
–tab                               : Open new tab
–title                              : Set title for the terminal
-e                                   : Execute the argument inside the terminal.
exec bash                      : Starts a new bash after executing all the commands. This command is required if you do not want to close the current tab .

2. Save the script anywhere in the file system and make the file executable using the following command:

$chmod +x file.sh

3. Add this file to startup applications :-

Go to  System > Preferences > Startup Applications

Click on add  and in command write-

bash path/to/your/file.sh

Close and its done!

Next time when you start your system, all the tasks mentioned in the script will be automated.

Hope this helped!

Nitesh Goel
[Intelligrape Software Pvt. Ltd.]

Posted in Linux

Database Backup Script for windows

Posted by on December 14th, 2010

In one of the project I am working on, the application needs to be deployed on windows server. To take the database backup, I wrote the script which does the following :

  1. Takes database dump and copy it to the backup folder.
  2. Zip that backup file and rename it to the format “YYYYMMDD_HHMMSS”
  3. Removes  all the backups older than 30 days.

I used mysqldump command to take database backup. To zip the backup file using the command line, I found 7-zip (download) which is freely available.  We need to have forfiles.exe (download) which enables us to remove old backup files (say older than 30 days).

@echo off
CLS
set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
set year=%date:~-4%
set month=%date:~3,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%
set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%
set datetimef=%year%%month%%day%_%hour%%min%%secs%
"MYSQL_BIN_PATH\mysqldump" --user=root --password=MY_PASSWORD MY_DATABASE_NAME > "BACKUP_FOLDER_PATH\backup.sql"
"7ZIP_EXE_PATH\7z" a -tzip "BACKUP_FOLDER_PATH\"%datetimef%".zip" "BACKUP_FOLDER_PATH\backup.sql"
del "BACKUP_FOLDER_PATH\backup.sql"
"FORFILES_EXE_PATH\forfiles.exe" /p BACKUP_FOLDER_PATH /s /m *.* /d -30 /c "cmd /c del /q @path

Prerequisites for the above script to work would be :-

  1. Mysql and 7zip installed.
  2. Forfiles.exe is downloaded
  3. All the Path’s in script are replaced correctly.

I also used a background job to run this script automatically every hour on the working days. Though same can be achieved with windows scheduler too.

Hope this helped.

Cheers!
~~Amit Jain~~
amit@intelligrape.com

http://intelligrape.com/

Posted in Database