Posts tagged: filesystem

Bash shell script for test the availability about commands in the filesystem

The objective is the reuse about variables and why not all GNU/Linux distributions installs the commands by default in the same place

I am going to allocate  a variable per each different command. This file will be included in other shell scripts.

preferences.cfg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
CMD_AWK="/usr/bin/awk"
CMD_CAT="/bin/cat"
CMD_CHGRP="/bin/chgrp"
CMD_CHMOD="/bin/chmod"
CMD_DD="/bin/dd"
CMD_DIFF="/usr/bin/diff"
CMD_ECHO="/bin/echo"
CMD_FIND="/usr/bin/find"
CMD_GREP="/bin/grep"
CMD_LAST="/usr/bin/last"
CMD_LASTB="/usr/bin/lastb"
CMD_LS="/bin/ls"
CMD_LSB_RELEASE="/usr/bin/lsb_release"
CMD_MKDIR="/bin/mkdir"
CMD_MKFS="/sbin/mkfs.ext2"
CMD_MKNOD="/bin/mknod"
CMD_MOUNT="/bin/mount"
CMD_MYSQL="/usr/bin/mysql"
CMD_PHP="/usr/bin/php5"
CMD_SED="/bin/sed"
CMD_SEQ="/usr/bin/seq"
CMD_SORT="/usr/bin/sort"
CMD_STAT="/usr/bin/stat"
CMD_TAIL="/usr/bin/tail"
CMD_TOUCH="/bin/touch"
CMD_TUNE2FS="/sbin/tune2fs"
CMD_UNAME="/bin/uname"
CMD_YES="/usr/bin/yes"
CMD_WC="/usr/bin/wc"

 

This shell script "preferences.bash" checks the availability about each command defined in the variables saved in the data file "preferences.cfg".

preferences.bash

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
# Check the availability about the commands that will be used
 
# Include global preferences
source $PWD/preferences.cfg
 
CMD=$($CMD_CAT $PWD/preferences.cfg)
 
echo -e "Check the availability about the commands that will be used \r"
 
# Checks if path is ok
for line in $CMD;
do
        TEST=$($CMD_ECHO "$line" | $CMD_AWK -F= '{ print $2 }' | $CMD_SED 's/"//g')
 
        echo -n ""
        if [ -e $TEST ]; then
                echo -n "[Passed]"
        else
                echo -n "[Failed]"
        fi
 
        echo -n " ... $TEST"
        echo -e "\r"
done;

 

Sample output

Check the availability about the commands that will be used
[Passed] … /usr/bin/awk
[Passed] … /bin/cat
[Passed] … /bin/chgrp
[Passed] … /bin/chmod
[Passed] … /bin/dd
[Passed] … /usr/bin/diff
[Passed] … /bin/echo
[Passed] … /usr/bin/find
[Passed] … /bin/grep
[Passed] … /usr/bin/last
[Passed] … /usr/bin/lastb
[Passed] … /bin/ls
[Passed] … /usr/bin/lsb_release
[Passed] … /bin/mkdir
[Passed] … /sbin/mkfs.ext2
[Passed] … /bin/mknod
[Passed] … /bin/mount
[Failed] … /usr/bin/mysql
[Failed] … /usr/bin/php5
[Passed] … /bin/sed
[Passed] … /usr/bin/seq
[Passed] … /usr/bin/sort
[Passed] … /usr/bin/stat
[Passed] … /usr/bin/tail
[Passed] … /bin/touch
[Passed] … /sbin/tune2fs
[Passed] … /bin/uname
[Passed] … /usr/bin/yes
[Passed] … /usr/bin/wc
 

Filesystem, MySQL and Crontab Backup application system

I have used two proffesional scripts for backup the application filesystem and database.

Filesystem

# (c) 2001 Chris Arrowood (GNU LGPL V2.1)
# You may view the full copyright text at:
# <a href="http://www.opensource.org/licenses/lgpl-license.html">http://www.opensource.org/licenses/lgpl-license.html</a>
# <a href="http://simplebashbu.sourceforge.net/">http://simplebashbu.sourceforge.net/</a>
# DESCRIPTION:
# A simple BASH script to do nightly backups to tarballs
# on a hard drive (not to tape)  Ideal for home linux users
# to easily backup thier system, provided they have an extra
# hard drive.
#

Basic configuration:

###############################################
#              User Variables                 #
###############################################
#
# Modify these variables to suit your needs
#
# Which day of the week do we want to do full backups? 0=Sunday
  LEVEL0DAY=0
# Where to create the backups; It should already exist
  BACKUP_DIR=/backup/fs
# Filesystems to backup seperated by spaces and the entire string in double quotes; each must start with /
  FILESYSTEMS="/var/www/www.simonalsa.com"
# Should we email results? Also should we email critical errors?  0=false, 1=true
  EMAIL=1
# EMAIL address to send results to
  EMAILADDRESS=simonalsa@simonalsa.com
# Email Subject
  EMAILSUBJECT="$HOSTNAME www.simonalsa.com Filesystem Backup"
# Only keep last weeks level0 backup (0) or keep all lvl 0 backups (1).  Keeping all data may take a lot of space!
  KEEPALL=0
# Do we wnat to compress the backup file using gzip? 0=false, 1=true
  COMPRESS=1
# Should we compress the log file when we are done?  0=false, 1=true
  COMPRESSLOG=1
# If we are compressing, what level do we use?
  COMPRESSLEVEL=6
# Determines whether we see all output to screen. It will still go to log regardless of this value.   0=false, 1=true
  QUIET=1
# Would you like to get detailed information from tar and gzip? 0=false, 1=true  
  VERBOSE=1

Script: backup_www.simonalsa.com.sh

Database 

# MySQL Backup Script
# VER. 2.5.1 - <a href="http://sourceforge.net/projects/automysqlbackup/">http://sourceforge.net/projects/automysqlbackup/</a>
# Copyright (c) 2002-2003 wipe_out@lycos.co.uk
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
Script: <a href="http://www.simonalsa.com/wp-content/uploads/2011/05/backup_mysql.simonalsa.com_.zip">backup_mysql.simonalsa.com</a>

Basic configuration:

 
### START CFG ###
 # Username to access the MySQL server e.g. dbuser
 USERNAME=dbo
 
 # Password to access the MySQL server e.g. password
 PASSWORD=
 
 # Host name (or IP address) of MySQL server e.g localhost
 DBHOST=db.simonalsa.com
 
 # List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3"
 DBNAMES="db"
 
 # Backup directory location e.g /backups
 BACKUPDIR="/backup/db"
 
 # Mail setup
 # What would you like to be mailed to you?
 # - log   : send only log file
 # - files : send log file and sql files as attachments (see docs)
 # - stdout : will simply output the log to the screen if run manually.
 # - quiet : Only send logs if an error occurs to the MAILADDR.
 MAILCONTENT="log"
 
 # Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs])
 MAXATTSIZE="4000"
 
 # Email Address to send mail to? (<a href="mailto:user@domain.com">user@domain.com</a>)
 MAILADDR="simonalsa@simonalsa.com"

Crontab

File: crontab_backup

Content:

1 1 * * * /script/backup_www.simonalsa.com.sh
30 1 * * * /script/backup_mysql.simonalsa.com.sh

Howto create a dynamic swap memory into filesystem dphys-swapfile

I installed “dphys-swapfile” Debian (lenny) package to add a swap memory file to my operating system.
 
This package “Autogenerate and use a swap file”.
 
If you want configure the package you must edit the default configuration file in /etc/dphys-swapfile
 
This is a configuration example that i looked across Internet…
 
Example
 
IOF>>
 
salonso@simonalsa.com:~$ cat /etc/dphys-swapfile

# /etc/dphys-swapfile – user settings for dphys-swapfile package
# author Neil Franklin, last modification 2006.09.15
# copyright ETH Zuerich Physics Departement
#   use under either modified/non-advertising BSD or GPL license
# this file is sourced with . so full normal sh syntax applies
# where we want the swapfile to be, this is the default

CONF_SWAPFILE=/backup/swap.file
 
# size we want to force it to be, default (empty) gives 2*RAM
#CONF_SWAPSIZE=2048
##
#  Give yourself three times the memory size of swap?
#
# mem=$(grep MemTotal /proc/meminfo |awk ‘{print $2}’)
# CONF_SWAPSIZE=$(expr $mem \* 3)

 
<<EOF

WordPress Themes