Tag Archives: bash

Bash script to backup my configs (dotfiles)

I needed some script to take care of my config files and put them in a single directory. So here it is.

#!/bin/bash
 
# Display All Hidden Dot Files In a Directory
# ls -a | egrep "^\." > backup_dotfiles.sh
 
DESTINATION="/storage/dotfiles"  # do not use a trailing slash (/)
 
# Colors
blue="\e[0;34m"
green="\e[1;32m"
red="\e[0;31m"
bold="\e[1;30m"
reset="\e[0m"
 
# file list (use trailing slash for directories)
FILES="
.bash_aliases
.bash_logout
.bash_profile
.bashrc
.colours/
.config/openbox/
.config/terminator/
.config/tint2/
.config/zathura/
.devilspie/
.fehbg
.fonts.conf
.gtk-bookmarks
.gtkrc-2.0
.gtkrc-2.0.mine
.inputrc
.mplayer/
.nanorc
.rtorrent.rc
.screenrc
.synergy.conf
.vim/colors/
.vimrc
.xbindkeysrc
.Xdefaults
.xinitrc
.xmod
.Xmodmap
.xsession
.xxkb/
.xxkbrc
"
for file in $FILES
do
   if [ -d $file ]; then
      mkdir -p $DESTINATION/$file
      cp -f $HOME/$file* $DESTINATION/$file
   elif [ -f $file ]; then
      cp -f $HOME/$file $DESTINATION
   else
      echo -e "$red:: $file is not a file/directory! $reset"
   fi     
done
 
echo -e "$green:: Done! $reset"
exit 0

Search and remove duplicate files

Here a small piece of code which will search for the duplicates in the path you supply. It will generate a new file which contains the commands for deleting those duplicates.
Make sure you double check the resulting file!
The script requires that you have fdupes installed.
Install it in Ubuntu/Debian by running the following command in your terminal:

sudo aptitude install fdupes
#!/bin/bash
 
OUTF='rm-dups.sh'
echo "#!/bin/bash" > $OUTF
fdupes -r -f $1 | sed -r 's#(.+)#rm '\"'\1'\"'#' >> $OUTF
chmod +x $OUTF

Fix Google Earth 5.0 user interface font on Linux

I installed Google Earth 5.0 and the ugly UI font from the beta is still here.
Based on the comments from Google Earth Help page from here I’ve created the following script wich will fix the user interface font issue.

#!/bin/bash
 
sudo aptitude -y install libqt4-webkit
ge=`which googleearth`
 
if [ -n "$ge" ]; then
    ## get the path and check if it is a symlink
    if [ -h "$ge" ]; then
        real_path=`readlink -e $ge`
        dir_path=`dirname $real_path`
    else
        real_path=`dirname $ge`
    fi
 
    ## backup original Qt4 libraries
    for i in `ls $dir_path | grep libQt`; do
		sudo mv -v $dir_path/$i $dir_path/$i"_backup"
    done
 
	## backup original Qt4 plugins
    for j in `ls $dir_path/plugins/imageformats | grep libq`; do
		sudo mv -v $dir_path/plugins/imageformats/$j $dir_path/plugins/imageformats/$j"_backup"
    done
 
	## create a new qt.conf file
cat > $HOME/qt.conf << "EOF"
[Paths]
Documentation=/usr/share/doc
Libraries=/usr/lib
Plugins=/usr/lib/qt4/plugins
Translations=/usr/lib/qt4/translations
EOF
 
	sudo mv -v $dir_path/qt.conf $dir_path/qt.conf_backup
	sudo mv -v $HOME/qt.conf $dir_path
 
elif [ -z "$ge" ]; then
		echo -e "You don't have Google Earth installed!\n"
		exit
fi
 
exit 0

View Config Files Without Comments

Use this grep invocation to trim comments out of config files. Comments are great but can get in your way if you just want to see the currently running configuration. I’ve found files hundreds of lines long which had fewer than ten active configuration lines.
It’s really hard to get an overview of what’s going on when you have to wade through hundreds of lines of comments.

grep ^[^#] /etc/samba/smb.conf

The regex ^[^#] matches the first character of any line, as long as that character that is not a #. Because blank lines don’t have a first character they’re not matched either, resulting in a nice compact output of just the active configuration lines.

Random crash of the X server in Jaunty

I experienced a lot of random X server crashes using the proprietary driver for the NVIDIA graphic card on my Thinkpad T61 running Ubuntu 9.04 x86 with Compiz activated.
I managed to fix the problem by installing the lastest NVIDIA driver. I wrote a little script to automate the process and save time. I recommend you to run this in command line mode. Here it is:

#!/bin/bash
 
# Check if the X server is running
pid_of_X=`ps aux | grep X | grep -v 'grep' | grep root | awk '{ print $2 }'`
 
if [ -z "$pid_of_X" ];
  then
    echo -e '\n\n\n\n\n\n\n\n\n'
    echo -e 'X is not running! - Good!\n'
	# Stop the xserver (GDM)
	sudo /etc/init.d/gdm stop
	cd $HOME/Desktop
 
	if [ -e latest.txt ]; # Check for 'latest.txt' file
	  then
	    rm -f latest.txt
	fi
 
	if [ -e NVIDIA-Linux-x86-* ]; # Check for 'NVIDIA-Linux-x86-*' file
	  then
	    rm -f NVIDIA-Linux-x86-*
	fi
 
	# Get the latest driver from NVIDIA's ftp
	nvidia_ftp='ftp://download.nvidia.com/XFree86/Linux-x86'
	wget -c -T 10 $nvidia_ftp/latest.txt
 
	new_driver_version=`cat latest.txt | awk '{ print $1 }'`
	new_driver_filename=`cat latest.txt | awk '{ print $2 }'`
 
	wget -c -T 10 $nvidia_ftp/$new_driver_filename
 
	chmod +x $HOME/Desktop/NVIDIA-Linux-x86-*
 
	# Remove all nvidia* packages in the system
	sudo dpkg -l | grep nvidia | awk '{ print $2 }' | xargs sudo aptitude -y purge
 
	# Remove all the nvidia kernel objects currently installed
	export kernel_version=`uname -r`
	sudo rm -f `find /lib/modules/$kernel_version -iname nvidia.ko`
 
	# Install the new driver
	sudo sh $HOME/Desktop/NVIDIA-Linux-x86-*.run
 
	rm -f NVIDIA-Linux-x86-*
	rm -f latest.txt
 
	echo -e '\n\n\n\n\n\n\n\n\n'
	echo -e '---------\n'
	echo -e 'All done!\n'
	echo -e 'Starting GDM in 5 seconds...\n'
	sleep 5
 
	# Start GDM
	sudo /etc/init.d/gdm start
 
  else
    echo -e '\n\n\n\n\n\n\n\n\n'
    echo -e 'X is running!\n'
    echo -e 'You should run this script in console mode!\n'
 
    echo -e 'Press CTRL+ALT+F1 to go to console mode, then run this script again!\n'
    echo -e '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n'
    echo -e 'Aborting in 5 seconds!'
    echo -e '\n\n\n\n\n\n\n\n\n'
    sleep 5
    exit 0
fi

Auto update for Debian/Ubuntu

I use the following script to automatically update my Ubuntu box.
I don’t recommend using this on your production servers!

#!/bin/bash
 
#################################################
##                                             ##
## auto-update.sh v1.0                         ##
## Use this script to set up automatic updates ##
## on your debian/ubuntu box.                  ##
##                                             ##
#################################################
 
## Creating /usr/bin/auto-update.sh file
sudo touch /usr/bin/auto-update.sh
sudo chmod 700 /usr/bin/auto-update.sh
sudo chown root:root /usr/bin/auto-update.sh
echo '#!/bin/bash' | sudo tee -a /usr/bin/auto-update.sh
echo 'touch /var/log/auto-update.log' | sudo tee -a /usr/bin/auto-update.sh
echo 'echo '------------------' >> /var/log/auto-update.log' | sudo tee -a /usr/bin/auto-update.sh
echo 'echo `date` >> /var/log/auto-update.log' | sudo tee -a /usr/bin/auto-update.sh
echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin' | sudo tee -a /usr/bin/auto-update.sh
echo 'export PATH' | sudo tee -a /usr/bin/auto-update.sh
echo '/usr/bin/aptitude update >> /var/log/auto-update.log' | sudo tee -a /usr/bin/auto-update.sh
echo '/usr/bin/aptitude -y safe-upgrade >> /var/log/auto-update.log' | sudo tee -a /usr/bin/auto-update.sh
echo 'exit' | sudo tee -a /usr/bin/auto-update.sh
 
# Creating a cron job for root user (it will run /usr/bin/auto-update.sh every day at 14:30)
echo '30 14 * * * /usr/bin/auto-update.sh > /dev/null' > cron_file.txt && sudo crontab -u root cron_file.txt && rm -f cron_file.txt

Note that there are some dangers regarding automatic updates. You can read more about it here.

Bash script to retrieve info about current playing track on ARM.FM

I am a big fan of ARM.FM.
Their playlist contains the freshest tracks in the dance music.
I wanted to keep track of the tunes I like the most, so I made a little script to update a file on my desktop with the name of the track currently playing.
Here is the script, you may use and/or modify it as you please.

#!/bin/bash
 
## arm_fm.sh 1.0
## Keep track of the best tracks playing on http://armageddonfm.net/
## It creates the file 'tracks' on your desktop
##
## Also, the file _output.html can be used on your site
## to display last 3 tracks played on ARM FM
 
 
curl -s http://armageddonfm.net/ -o _arm.txt
start_line=`grep -n 'current' _arm.txt | cut -f1 -d':'`
html_tag='
<table align="center" cellpadding="0" cellspacing="0">
    <tr>
    <td valign="top">
 
      <div style="width: 250px; margin: 20px 0px 0px 0px;">
        <div style="margin: 10px 0px 0px 0px;">
          <table cellspacing="0">
                                          <tr>
              <td style="padding: 10px;"></td>
'
echo $html_tag > _output.html
sed -n "$start_line,81p" _arm.txt >> _output.html
 
sed -i -e 's/<img src="\/tpl\/images\/dot.jpg">//g' _output.html
 
sed -n '2,2p' _output.html > _temp
sed -i -e 's/              <td valign="top" style="border: 1px solid #ccc; border-top: 0px; border-right: 0px; padding: 10px; font-size: 11px; color: #444"><b><font style="color: #900">//g' _temp
sed -i -e 's/<\/font><\/b><br> //g' _temp
sed -i -e 's/<br><\/td>//g' _temp
sed -i -e 's/  (current)/ - /g' _temp
 
artist=`cut -f1 -d '-' _temp`
track=`cut -f2 -d '-' _temp | sed 's/^ *//'`
 
echo $artist - $track >> $HOME/Desktop/tracks
rm _output.html _arm.txt _temp

My custom bash prompt (PS1)

Pimp out Linux / UNIX shell prompt!
You can find a nice guide here, and a nice collection here.
Here is mine:

export PS1="[\[\e[28;1m\]\H \[\e[0m\]\w]\$ "

Have fun!

Sample firewall script for Hardy Heron

I just thought that you might be interested in a small script you can use in Hardy Heron.

It uses the new UFW (Uncomplicated firewall) introduced in this new Ubuntu distro.

The script is well commented, so everything is easy to understand.

Here is the script:

#!/bin/bash
 
  ## set the default policy to drop (deny) all connections
  sudo ufw default deny
 
  ## set logging ON
  sudo ufw logging on
 
  ## permit unrestricted traffic from a specific static IP address
  sudo ufw allow from 192.168.1.1    # maybe your router
 
  ## permit TCP connections on ssh port 22
  sudo ufw allow 22/tcp
 
  ## Allow Apache2
  sudo ufw allow 80/tcp
 
  ## Allow MySQL
  sudo ufw allow 3306/tcp
 
  ## Allow Bittorrent
  for port in {7881..7889};
  do sudo ufw allow $port/tcp; done
 
  ## Allow eMule
  sudo ufw allow 4662/tcp
  sudo ufw allow 4672/udp
 
  ## Allow DC++
  sudo ufw allow 6845
 
  ## Allow Samba from internal network only 
  sudo ufw allow proto tcp from 192.168.1.0/24 to any port 135 # used by smbd
  sudo ufw allow proto udp from 192.168.1.0/24 to any port 137 # used by nmbd
  sudo ufw allow proto udp from 192.168.1.0/24 to any port 138 # used by nmbd
  sudo ufw allow proto tcp from 192.168.1.0/24 to any port 139 # used by smbd
  sudo ufw allow proto tcp from 192.168.1.0/24 to any port 445 # used by smbd
 
  ## Display rules
  sudo ufw status

You can modify it by adding/removing rules accordingly.

Backup your MySQL databases automatically with AutoMySQLBackup

If you are a MySQL user and you want to have a disaster recovery solution, or even as a simple backup, you can write small shell scripts based on mysqldump tool.

For this, you need AutoMySQLBackup. It doesn’t have any real requirements (mysqldump of course is needed – in any mysql client package – and gzip or bzip2 to compress the resulting file) and has all the features I was looking for in such a script.

AutoMySQLBackup has all the features I needed: it can backup a single database, multiple databases, or all the databases on the server; each database is saved in a separate file that can be compressed (with gzip or bzip2); it will rotate the backups and not keep them filling your hard drive (as normal in the daily backup you will have only the last 7 days of backups, the weekly if enabled will have one for each week, etc.). It has also some other features (check the project homepage for full details), that I am not using myself (like email logs for example), but other peoples might find interesting.

All you have to do is to modify a few things:

# Username to access the MySQL server e.g. dbuser
USERNAME=dbuser
# Username to access the MySQL server e.g. password
PASSWORD=password
# Host name (or IP address) of MySQL server e.g localhost
DBHOST=localhost
# List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3"
DBNAMES="all"
# Backup directory location e.g /backups
BACKUPDIR="/var/backup/mysql"
# Mail setup
MAILCONTENT="quiet"

You can run the script manually but you will want to enable it in cron
and run daily.

  • Page 1 of 2
  • 1
  • 2
  • >

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close