Image

Image

Search This Blog

Showing posts with label BASH. Show all posts
Showing posts with label BASH. Show all posts

Wednesday, February 05, 2025

RPI network reconnect

to be run */10 * * * * from cron

#!/bin/bash

wlan='wlan0'
gateway='192.168.1.1'

ping -c2 $gateway 2>&1 >/dev/null; rc=$?
if [[ $rc -eq 0 ]] ; then
        echo `date +"%b %d %T "`$0": The network is up."
else
        echo `date +"%b %d %T "`$0": Network down! Attempting reconnection."
        if [ -f /storage/.cache/nonet ]; then
                rm -f /storage/.cache/nonet
                reboot
        fi
        ifdown $wlan
        rmmod brcmfmac
        sleep 2
        modprobe brcmfmac brcmfmac_wcc
        ifup --force $wlan
        sleep 2
        connmanctl connect wifi_dc345a1743d_6e6545675289d3567_managed_psk &>/dev/null
        sleep 10
        ping -c2 $gateway 2>&1 >/dev/null; rc=$?
        if [[ $rc -ne 0 ]] ; then
                echo "1" > /storage/.cache/nonet
        fi
fi
exit 0

 

Wednesday, April 08, 2020

DD-WRT on a Videotron reflashed DIR-825

The Videotron custom firmware does not allow local upgrade. On the firmware update page there is a nice "Firmware upgrade: Disabled" and that's where the update ends in the main interface.
In order to update, I've rebooted in  recovery mode browser by keeping the reset pressed white plugin the power. However, the upload process was always staying at 0% and didn't finished;
Then I've hexedited the factory-to-ddwrt_NA.bin and changed the last byte in the file from "0" to "2" - still no luck;
I've tried uploading directly from the shell: "ifconfig enp0s25 192.168.0.34 up && curl -0vF files=@factory-to-ddwrt_NA.bin http://192.168.0.1/cgi/index" - nope, still nothing;

The solution was deceivingly simple: I've put a switch between the D-Link and the computer - then the curl upload worked perfectly!

Monday, October 03, 2016

Put the backup on a remote tape via ssh

#backup
   tar --verbose --exclude=/proc --exclude=/sys --exclude=/tmp --exclude=/mnt --totals -b2048 -jcpvf $SRC | ssh root@192.168.1.201 $(mt -f /dev/st0 rewind; cat > /dev/st0)

#restore
ssh root@192.168.1.201 "cat /dev/st0" | tar --exclude=/proc--exclude=/sys --exclude=/tmp --exclude=/mnt --totals -b2048 -jxpvf $DES

Monday, August 03, 2015

DNS Adbock on router

In case you have, like me, a secodary dns on your DD-WRT router, you need a dns adblock on it too. By modifying the excellent tutorial from http://www.howtogeek.com/51477/how-to-remove-advertisements-with-pixelserv-on-dd-wrt/ I came to this script:

#!/bin/sh
########Functions setup#########################
logger_ads()
{
logger -s -p local0.notice -t ad_blocker $1
}

softlink_func()
{
ln -s /tmp/$1 /jffs/dns/$2
if [ "`echo $?`" -eq 0 ] ; then
logger_ads "Created $3 softlink to RAM on JFFS"
else
logger_ads "The attempt to create $3 softlink to RAM on JFFS *FAILED*"
logger_ads "it is obvious something IS *terribly wrong*. Will now exit... bye (ads will not be blocked)"
exit 1
fi
}

note_no_space()
{
logger_ads "I assure you this only takes $1 blocks, but I guess your too close to the edge for JFFSs comfort"
logger_ads "deleting the half witted file, as to not confuse the DNS service and free up the JFFS space for other uses."
}
##################################################
nvram set aviad_changed_nvram=0
logger_ads "########### Ads blocker script starting ###########"

if [[ -z "$1" ]]; then
logger_ads "Sleeping for 30 secs to give time for router boot"
sleep 30
else
logger_ads "override switch given"
[[ $1 = "-h" || $1 = "/?" ]] && echo "use -m to override the 30 seconds delay and -f to force a list refresh" && exit 0
[ $1 = "-f" ] && rm /jffs/dns/dnsmasq.adblock.conf && rm /jffs/dns/dlhosts
fi

while ! ping www.google.com -c 1 > /dev/null ; do
logger_ads "waiting for the internet connection to come up"
sleep 5
done

logger_ads "Adding a refresh cycle by puting the script in cron if it isnt there yet"
if [[ -z "`cat /tmp/crontab | grep "/jffs/dns/disable_adds.sh"`" ]] ; then
echo '0 0 * * * root /jffs/dns/disable_adds.sh -m' > /tmp/crontab
stopservice cron && logger_ads "stopped the cron service" startservice cron && logger_ads "started the cron service"
else
logger_ads "The script is already in cron"
fi

logger_ads "New IP and ports setup. Reserve the IP .100 for pixelserv"
pixel="`ifconfig br0 | grep inet | awk '{ print $2 }' | awk -F ":" '{ print $2 }' | cut -d . -f 1,2,3`.100"
mgmtip="`ifconfig br0 | grep inet | awk '{ print $2 }' | awk -F ":" '{ print $2 }'`"

# In my case, on IP .100 I have an apache serving null.html as error page. Comment
next 3 paragraphs, as I don't need pixelserv
#logger_ads "Move http interface to $mgmtip:88"
#if [[ -z "`ps | grep -v grep | grep "httpd -p 88"`" && `nvram get http_lanport` -ne 88 ]]
; then
# logger_ads "it seems that the http is not setup yet on port :88"
# stopservice httpd
# nvram set http_lanport=88
# nvram set aviad_changed_nvram=1
# startservice httpd
#else
# logger_ads "The http is already setup on $mgmtip:88"
#fi

#logger_ads "Redirect setup IP/Port from $mgmtip:80 to $mgmtip:88"
#[[ -z "`iptables -L -n -t nat | grep $mgmtip | grep 80`" ]] && logger_ads "did NOT find an active redirect rule with the iptable command, injecting it now." && /usr/sbin/iptables -t nat -I PREROUTING 1 -d $mgmtip -p tcp --dport 80 -j DNAT --to $mgmtip:88
#nvram get rc_firewall > /tmp/fw.tmp
#if [[ -z "`cat /tmp/fw.tmp | grep "/usr/sbin/iptables -t nat -I PREROUTING 1 -d $mgmtip  p tcp --dport 80 -j DNAT --to $mgmtip:88"`" ]] ; then
# echo "/usr/sbin/iptables -t nat -I PREROUTING 1 -d $mgmtip -p tcp --dport 80 -j DNAT --to $mgmtip:88" >> /tmp/fw.tmp
# nvram set rc_firewall="`cat /tmp/fw.tmp`"
# logger_ads "DONE appending forwarding to FW script"
# nvram set aviad_changed_nvram=1
#else
# logger_ads "The redirection $mgmtip:80 -> $mgmtip:88 in FW script is already in place"
#fi
#rm /tmp/fw.tmp

#logger_ads "Starting or ReSpawning pixelsrv on $pixel IP :80"
#/sbin/ifconfig br0:1 $pixel netmask "`ifconfig br0 | grep inet | awk '{ print $4 }' | awk -F ":" '{ print $2 }'`" broadcast "`ifconfig br0 | grep inet | awk '{ print $3 }' | awk -F ":" '{print $2 }'`" up
#if [[ -n "`ps | grep -v grep | grep /jffs/dns/pixelserv`" ]]; then
# logger_ads "the pixelserv is already up"
#else
# logger_ads "it seems that the pixelserv isnt up. starting it now"# /jffs/dns/pixelserv $pixel -p 80
#fi

logger_ads "Get the online dns blocking lists"
[ ! -e /jffs/dns/whitelist ] && echo google-analytics > /jffs/dns/whitelist && echo toma.guru >> /jffs/dns/whitelist
if [[ -n "$(find /jffs/dns/dlhosts -mtime +7)" || -n "$(find /jffs/dns/dnsmasq.adblock.conf mtime +7)" || ! -e /jffs/dns/dlhosts || ! -e /jffs/dns/dnsmasq.adblock.conf ]]; then
logger_ads "The lists are NOT setup at all yet, or more then 7 days old, will now retrieve them from the web"
logger_ads "Retrieving the MVPS hosts list..."
wget -q -O - http://www.mvps.org/winhelp2002/hosts.txt | grep "^127.0.0.1"
| grep -v localhost | tr -d '\015' >/tmp/dlhosts.tmp
logger_ads "adjusting the MVPS hosts list for our use"
cat /jffs/dns/whitelist | while read line; do sed -i /${line}/d /tmp/dlhosts.tmp
; done
sed -i s/127.0.0.1/$pixel/g /tmp/dlhosts.tmp
logger_ads "done adjusting the MVPS hosts list."
logger_ads "Retrieving the Yoyo domain list..."
wget -q
"http://pgl.yoyo.org/adservers/serverlist.php?hostformat=dnsmasq&showintro=0&mietype=plaintext" -O /tmp/adblock.tmp
logger_ads "adjusting the Yoyo domain list for our use"
cat /jffs/dns/whitelist | while read line; do sed -i /${line}/d /tmp/adblock.tmp
; done
sed -i s/127.0.0.1/$pixel/g /tmp/adblock.tmp
if [ "`df| grep /jffs | awk '{ print $4 }'`" -ge 65 ] ; then
logger_ads "Moving the Yoyo list to JFFS (as it looks that there is enough space for it)"
mv /tmp/adblock.tmp /jffs/dns/dnsmasq.adblock.conf
if [ "`echo $?`" -eq 0 ] ; then
logger_ads "Moving the YoYo domain list to JFFS operation was successful"
else
note_no_space 20
rm /jffs/dns/dnsmasq.adblock.conf
softlink_func adblock.tmp dnsmasq.adblock.conf YoYo
fi
else
logger_ads "*NOT* Moving the Yoyo list to JFFS (as it looks that there is *NOT* enough space for it)"
softlink_func adblock.tmp dnsmasq.adblock.conf YoYo
fi
if [ "`df| grep /jffs | awk '{ print $4 }'`" -ge 100 ] ; then
logger_ads "Moving the MVPS hosts list to JFFS (as it looks like there is enough space for it)"
mv /tmp/dlhosts.tmp /jffs/dns/dlhosts
if [ "`echo $?`" -eq 0 ] ; then
logger_ads "Moving the MVPS hosts list to JFFSoperation was successful"
else
note_no_space 72
rm /jffs/dns/dlhosts
softlink_func dlhosts.tmp dlhosts MVPS
fi
else
logger_ads "*NOT* Moving the MVPS list to JFFS (as it looks that there is *NOT* enough space for it)"
softlink_func dlhosts.tmp dlhosts MVPS
fi
else
logger_ads "The lists are less then 7 days old, saving on flash erosion and NOT refreshing them."
fi

logger_ads "Injecting the DNSMasq nvram options with the dynamic block lists"
nvram get dnsmasq_options > /tmp/dns-options.tmp
if [[ -z "`cat /tmp/dns-options.tmp | grep "/jffs/dns/dnsmasq.adblock.conf"`" || -z "`cat /tmp/dns-options.tmp | grep "/jffs/dns/dlhosts"`" && -e /jffs/dns/dnsmasq.adblock.conf ]] ; then
logger_ads "Did not find DNSMsaq options in nvram, adding them now"
echo "conf-file=/jffs/dns/dnsmasq.adblock.conf" >> /tmp/dns-options.tmp
echo "addn-hosts=/jffs/dns/dlhosts" >> /tmp/dns-options.tmp
nvram set aviad_changed_nvram=1
logger_ads "Added options to nvram DNSMasq options"
else
logger_ads "The DNSMaq options are already in place"
fi

logger_ads "Checking if the personal list is a file"
if [[ -z "`cat /tmp/dnsmasq.conf | grep conf-file=/jffs/dns/personal-ads-list.conf`" && -z "`nvram get dnsmasq_options | grep "/jffs/dns/personal-ads-list.conf"`" && -e /jffs/dns/personal-ads-list.conf ]] ; then
logger_ads "Yes the personal list is in the form of a file"
logger_ads "Removing whitelist from the personal file"
cat /jffs/dns/whitelist | while read line; do sed -i /${line}/d /jffs/dns/personal ads-list.conf ; done
echo "conf-file=/jffs/dns/personal-ads-list.conf" >> /tmp/dns-options.tmp
nvram set aviad_changed_nvram=1
else
[ ! -e /jffs/dns/personal-ads-list.conf ] && logger_ads "The personal list (assuming there is one) is not in a file"
[ -n "`nvram get dnsmasq_options | grep "/jffs/dns/personal-ads-list.conf"`" ] && logger_ads "The personal list is a file, and... it is already in place according to the NVRAM options readout"
[ "$1" = "-f" ] && cat /jffs/dns/whitelist | while read line; do sed -i /${line}/d /jffs/dns/personal-ads-list.conf ; done && logger_ads "overide switch given so removed whitelist from personal file"
fi
logger_ads "Final settings implementer"
if [ "`nvram get aviad_changed_nvram`" -eq 1 ] ; then
nvram set dnsmasq_options="`cat /tmp/dns-options.tmp`"
logger_ads "Found that NVRAM was changed and committing changes
now"
nvram commit
nvram set aviad_changed_nvram=0
logger_ads "Refreshing DNS settings"
stopservice dnsmasq && logger_ads "stopped the dnsmasq service"
startservice dnsmasq && logger_ads "started the dnsmasq service"
else
logger_ads "Nothing to commit"
fi
rm /tmp/dns-options.tmp
logger_ads "######### Ads blocker script has finished and you should be up and running ##########

Monday, May 04, 2015

dns_adblock

#!/bin/bash 
#dns_adblock.sh - poor man's adblock 
# (c)2015 sorin@toXX.guru                                                                                       
#replace add servers with a local blank page 
#this script works faster/better/cleaner if you have apache httpd configured to serve a null page in case of page not found 
#just create an empty file 'touch /var/www/html/null.html', then in /etc/httpd/httpd.conf add/modify the line 'ErrorDocument 404 /null.html'  


#define local variables 
target=/var/named/data/blacklist

nullzone=/var/named/null.zone.file 
namedconf=/etc/named.conf  
whitelisted=/var/named/data/whitelisted.personal 
blacklisted=/var/named/data/blacklisted.personal 
namesrv=192.168.18.100                                                                           
# if it's the first time we run this, add the right info into bind's configuration   
if [ ! -f $nullzone ]; then  
echo "\$TTL    86400   ; one day  
@       IN      SOA     ads.nodomain. root. (  
             2015021505 ; serial              
                  28800 ; refresh                  
                   7200 ; retry                        
                 864000 ; expiry                        
                86400 ) ; minimum                          
            NS      nameserver     
               A       $namesrv              
@       IN      A       $namesrv                    
*       IN      A       $namesrv" > $nullzone    
fi                                                                                
if ! grep -q $target $namedconf ; then                                             
echo "conf not found"  

echo " include \"$target\";" >> $namedconf 
fi           
                                               

# get the list of known adservers from yoyo.org, sanitize it and make it compatible with bind9   
wget -q -O - 'http://pgl.yoyo.org/as/serverlist.php?hostformat=bindconfig&showintro=0' | tail -n+29 | head -n -6 | sed -e 's/{/IN {/g' -e '/_/d' > $target-new 

                                  
# get a second list from mvps.org, sanitize it and add only the hostnames that were not given by yoyo.org 
while read adhost; do                                                              
if ! grep -q $adhost $target-new ; then  
echo "zone \"$adhost\" IN { type master; notify no; file \"null.zone.file\"; };" >> $target-new   
fi          
done < <(wget -q -O - http://www.mvps.org/winhelp2002/hosts.txt | sed -e '/^ *#/d;s/#.*//' -e '/^[[:space:]]*$/d' -e '/localhost/d' -e '/_/d' -e '/[\r\n]/d' | cut -d" " -f2 )          
             
# add personal blacklist    
blhost="nothing"  
sed '/^ *#/d;s/#.*//' $blacklisted | while read blhost; do  
if ! grep -q $blhost $target-new ; then                    
echo "zone \"$blhost\" IN { type master; notify no; file \"null.zone.file\"; };" >> $target-new  
fi        
done   

       
# remove whitelisted domains. ($wlisted variable have to be defined, otherwise sed might remove everything) 
wlisted="nothingatall"               
sed '/^ *#/d;s/#.*//' $whitelisted | while read wlisted; do 
sed -i /"$wlisted"'/d' $target-new                                                  done  

       
#remove duplicates, named is intolerant to multiple definitions for the same host  
cat  $target-new | sort -u > $target 


#cleanup line containing "empty" domain                                            
sed -i '/\"\"/d' $target 


# cleanup the last remanents of bad lines reported by named-checkconf  
if [ ! $(/usr/sbin/named-checkconf -t /var/lib/named -z /etc/named.conf >/dev/null 2>&1; echo $?) = 0 ]; then 
badlines=$(/usr/sbin/named-checkconf -t /var/lib/named -z /etc/named.conf | cut -d: -f2 | sed -e 's/$/d;/' | tr -d '\n')  
sed -i "$badlines" $target  
fi                                                                                    
rm -f $target-new                                                                                     
# reload bind  
/bin/systemctl reload named.service 




Wednesday, April 08, 2015

HP ILO Linux - reset password

(hpsum must be already installed)
Reset Administrator password to “newpass”:

vi reset-password.xml

<ribcl VERSION="2.0">
<login USER_LOGIN="Administrator" PASSWORD="something_without_importance">
  <user_INFO MODE="write">
   <mod_USER USER_LOGIN="Administrator">
    <password value="newpass"/>
   </mod_USER>
  </user_INFO>
</login>
</ribcl>

hponcfg -f reset-password.xml -l log.txt ; cat log.txt


Add user "admin" with password "nosecurity":

vi  adduser.xml

<ribcl version="2.0">
<login user_login="Administrator" password="blah_blah_it_doesnt_matter">
<user_info mode="write">
<add_USER
USER_NAME="admin"
USER_LOGIN="admin"
PASSWORD="nosecurity">
<reset_SERVER_PRIV   value = "Y" />
<admin_PRIV   value = "Y" />
</add_USER>
</user_info>
</login>
</ribcl>

hponcfg -f adduser.xml -l log.txt ; cat log.txt


Tuesday, September 02, 2014

A simple script to import .pst in thunderbird

#!/bin/bash
#
#(c)2014 sorinakis@g*il.com

if [ "$(whereis readpste | cut -d: -f2)" = "" ]; then.
 echo "Sending you to download readpst"
 kdialog --warningcontinuecancel "Go to http://www.five-ten-sg.com/libpst/ to download, then compile and install libpst
 Once libpst is installed please re-execute this script.
 If readpst is installed, but not in path, you have to comment the first section of $0" --continue-label "Go to site"
 if [ ! $? = 0 ]; then
  echo "Cancel Pressed. Exit"
  exit 2
 fi
 xdg-open http://www.five-ten-sg.com/libpst/
 exit 0
fi

# Prepare location
wrkfld=$TMP/outlook$$
export $(dbus-launch)
mkdir $wrkfld

# Convert pst
readpst -o $wrkfld -r "`kdialog --getopenfilename ~ '*.pst' 2>/dev/null`"

# Rename folder so thunderbird understands
find $wrkfld -type d | tac | grep -v '^$wrkfld$' | xargs -d '\n' -I{} mv {} {}.sbd
find $wrkfld.sbd -name mbox -type f | xargs -d '\n' -I{} echo '"{}" "{}"' | sed -e 's/\.sbd\/mbox"$/"/' | xargs -L 1 mv

#Cleanup empty folders
find $wrkfld.sbd -empty -type d | xargs -d '\n' rmdir

kdialog --msgbox "Conversion Done! Please create a subfolder in your Thunderbird's Local Folders,.
 then manuallly move $wrkfld.sdb into ~/.thunderbird/[profile]/Mail/Local Folders/[new folder]"

Wednesday, January 01, 2014

Recursively change owner/permisions

$1 is the source of the rights/ownership, $2 is the target
 
#!/bin/sh 
chmod --reference $1 $2
chown --reference $1 $2
if [ -d $1 ] 
  then 
 if [ "x`ls $1`" != "x" ] 
   then 
    for f in `ls $1` 
    do 
    $0 $1/$f $2/$f 
 done 
 fi 
fi

Thursday, December 05, 2013

Network Teaming

add the file bonding.conf to /etc/modprobe.d/
add the files ifcfg-* to /etc/sysconfig/network-scripts/ and modify them
accordingly (HWADDR, IP, GW etc)


bonding.conf
 alias bond0 bonding

ifcfg.bond0
DEVICE=bond0
IPADDR=192.168.1.178
NETMASK=255.255.255.0
ONBOOT=yes
BOOTPROTO=none
USERCTL=no
BONDING_OPTS="miimon=80 mode=2"
GATEWAY="192.168.1.1"
DNS1="192.168.1.4"
DNS2="192.168.1.253"
DOMAIN="mydomain.local"


ifcfg.eth0
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=no
HWADDR="9C:8E:99:00:00:00" #Use the REAL MAC of the card


ifcfg.eth1
DEVICE=eth1
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=no
HWADDR="9C:8E:99:
00:00:01" #Use the REAL MAC of the card

ifcfg.eth2 
DEVICE=eth2
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=no
HWADDR="9C:8E:99:
00:00:02" #Use the REAL MAC of the card

Sunday, November 10, 2013

My dual backup (rsync & cpio) (local & remote)

#!/bin/bash

# Define variables
interval=2
source=/mnt/raid3
diskid=backup
log=/var/log/backup-weekly.log
errlcl=254
excludefile=/tmp/exclude.$$

echo "/backup/wstn/backintime" >> $excludefile
echo "/VMWare_defaults" >> $excludefile
echo "/AllKind" >> $excludefile
echo "/Audio" >> $excludefile
echo "/Video/music" >> $excludefile
echo "/Video/Octonauts" >> $excludefile

echo `date +%Y/%m/%d\ %H:%M:%S`" Starting Backup" >> $log
# Find where the backup drive labeled "$diskid" is mounted
#location=`mount | grep \`while read input ; do blkid /dev/$input | grep $diskid | cut -d: -f1 ; done <  <(cat /proc/partitions | tail -n +2 | cut -c 26-)\` | cut -d" " -f3`
location=`mount | grep \`blkid | grep $diskid | cut -d: -f1\` | cut -d" " -f3` >/dev/null 2>>$log

# Run the backup only if the backup drive is mounted
if [ -n "$location" ]; then
    # Move all the backups one level up (recycling the oldest follewed by cp is indeed faster)
    echo `date +%Y/%m/%d\ %H:%M:%S`" Cycling local backups on $location" >> $log
    if [ -n  $location/backup$interval ]; then
    echo `date +%Y/%m/%d\ %H:%M:%S`" Temporary moving $location/backup$interval to $location/backup_tmp" >> $log
     mv -fu $location/backup$interval/ $location/backup_tmp >>$log 2>&1
    else
    echo `date +%Y/%m/%d\ %H:%M:%S`" Older backup not found. Creating an empty $location/backup_tmp" >> $log
     mkdir  $location/backup_tmp >>$log 2>&1
    fi
    while [ $interval -ge 1 ]; do
    echo `date +%Y/%m/%d\ %H:%M:%S`" Moving $location/backup$[ interval - 1 ] to $location/backup$interval" >> $log
     mv -fu $location/backup$[ interval - 1 ]/ $location/backup$interval >>$log 2>&1
     interval=$[ interval - 1 ]
    done
    # Create current backup by recycling the oldest and linking the last backup to it
    echo `date +%Y/%m/%d\ %H:%M:%S`" Moving the oldest backup from $location/backup_tmp to $location/backup$interval" >> $log
    mv -fu $location/backup_tmp/ $location/backup$interval >>$log 2>&1
    echo `date +%Y/%m/%d\ %H:%M:%S`" Using cpio to bring the differences from $location/backup$[ interval + 1 ] to $location/backup$interval" >> $log
    cp -falux $location/backup$[ interval + 1 ]/. $location/backup$interval/ >>$log 2>&1
    # cleanup old server and router backups
    echo `date +%Y/%m/%d\ %H:%M:%S`" Cleaning old backups." >>$log
    dest=$location/backup$interval/backup/server/
    find $dest -name "bkp_*.img.gz" -type f -mtime +6 -exec rm -v {} \; >>$log 2>&1
    # Take the new backup
    echo `date +%Y/%m/%d\ %H:%M:%S`" Creating the local backup." >>$log
    rsync --quiet --log-file=$log --stats --human-readable --partial --archive --recursive --times --compress --itemize-changes --one-file-system --hard-links --inplace --numeric-ids --del --links --exclude-from=$excludefile $source/ $location/backup$interval  >>$log 2>&1 ; errlcl=$?
    echo `date +%Y/%m/%d\ %H:%M:%S`" Local Backup finished." >>$log
    echo `date` > $location/backup$interval/backup_taken
else
errlcl=255
echo `date +%Y/%m/%d\ %H:%M:%S`" ERROR: Backup disk NOT FOUND!!!" >>$log 2>&1
fi


# Second backup to the offsite drive

rmtdest=/tmp/tmpmnt.$$ 
rmtsrv=root@rmtserv:/tmp/mnt/sda1
rmtftp=rmtserv:12221/USB/
rmtcifs=\\\\rmtserv\\USB
errlvl=254
echo -e '\n'`date +%Y/%m/%d\ %H:%M:%S`" Remote Backup starting." >>$log

# Check if rsync is available on the destination
needmount=`rsync -av $excludefile $rmtsrv 2>&1 | grep 'rsync: not found'`
if [ -n "$needmount" ]; then
    # Rsync is not available, mount the remote destination locally via sshfs (using ssh keys) - fallback to smb (as guest) or curlftp (need a valid ~/.netrc) 
    echo `date +%Y/%m/%d\ %H:%M:%S`" rsync not available, mounting sshfs $rmtsrv" >>$log
    mkdir -p $rmtdest >>$log 2>&1
    sshfs $rmtsrv $rmtdest >>$log 2>&1
    if [ -z "$(mount | grep $rmtdest)" ]; then
     echo `date +%Y/%m/%d\ %H:%M:%S`" sshfs not available, mounting cifs $rmtcifs" >>$log
     mount.cifs -o guest $rmtcifs $rmtdest >>$log 2>&1
    fi
    if [ -z "$(mount | grep $rmtdest)" ]; then
     echo `date +%Y/%m/%d\ %H:%M:%S`" cifs not available, mounting ftpfs $rmtftp" >>$log
     curlftpfs -o allow_other -o gid=0 -o uid=0 -o umask=000 $rmtftp $rmtdest >>$log 2>&1
    fi
     # Without rsync we'll use cpio. First we need to make sure the rmtdest has been mounted
    if [ -n "$(mount | grep $rmtdest)" ]; then
     # cleanup old server and router backups
     echo `date +%Y/%m/%d\ %H:%M:%S`" $rmtdest mounted. Cleaning remote folder." >>$log
     dest=$rmtdest/backup/server/
     find $dest -name "bkp_*.img.gz" -type f -mtime +6 -exec rm -v {} \; >>$log 2>&1
     # start syncing the backup
     echo `date +%Y/%m/%d\ %H:%M:%S`" Starting cpio data transfer." >>$log
     cd $source
     find . -depth -print |grep -v -f $excludefile | cpio --pass-through --reset-access-time --make-directories --preserve-modification-time $rmtdest  1>>$log 2> >(grep -vi "newer") >>$log ; errlvl=$?
     echo `date +%Y/%m/%d\ %H:%M:%S`" Cpio transfer finished." >>$log
     echo `date +%Y/%m/%d\ %H:%M:%S` > $rmtdest/backup_taken
    else
     errlvl=255
     echo `date +%Y/%m/%d\ %H:%M:%S`" ERROR: No rsync available and remote destination NOT MOUNTED!!!" >>$log
    fi
else
    # If rsync is available we don't need to mount anything. Cleanup old server and router backups over ssh
    echo `date +%Y/%m/%d\ %H:%M:%S`" Using rsync. Cleaning remote folder" >>$log
    ssh `echo $rmtsrv | cut -d: -f1` `find /tmp/mnt/sda1/backup/server/ -name "bkp_*.img.gz" -type f -mtime +6 -exec rm -v {} \;` >>$log 2>&1
    # perform rsync
    echo `date +%Y/%m/%d\ %H:%M:%S`" Starting remote rsync." >>$log
    rsync --quiet --log-file=$log --stats --human-readable --partial --archive --recursive --times --compress --itemize-changes --one-file-system --hard-links --inplace --numeric-ids --del --links --max-size=4G --exclude-from=$excludefile $source/ $rmtsrv >>$log 2>&1 ; errlvl=$?
    ssh `echo $rmtsrv | cut -d: -f1` $(echo `date` > /tmp/mnt/sda1/backup_taken)
    echo `date +%Y/%m/%d\ %H:%M:%S`" Remote rsync finished." >>$log
fi

# Cleanup traces
umount -fl $rmtdest >>$log 2>&1
sleep 5
if [ -z "$(mount | grep $rmtdest)" ]; then
 rmdir $rmtdest >>$log 2>&1
fi
rm -f $excludefile >>$log 2>&1
echo -e `date +%Y/%m/%d\ %H:%M:%S`" Backup finished."'\n\n' >>$log

# Send backup report
if [ "$errlcl" -eq "0" ] && [ "$errlvl" -eq "0" ]; then
 msg="Weekly Backup successfully completed."
else
 msg="Weekly Backup might contain errors."
fi
echo "Please see attached Backup Report. The exit codes were: local=$errlcl remote=$errlvl" | mail -r backup@mydomain.tld -s "$msg" -S smtp=smtp://relay.smtpserver.tld -a $log recipient@mydomain.tld

Sunday, November 06, 2011

IP Changing notification

Because sometimes the dyndns updater goes to hell, a little crontab script executed every couple of hours it's a life saver:

#!/bin/sh
#
# Send an email message to notify about the external IP.
#

#set a file where we can store the ip
logfile=/var/log/extip
#to whom we send the email
recipient="my_account@provider.tld"
#if we are using an external smtp relay
smtp=relay.smtp.provider.tld
export smtp=$smtp
#put a nice sender address on the email
sender="IP Checker <ipcheck@server.tld>"
#find the name of of this server
HOSTNAME=`hostname -a | cut -f1 -d" "`
# format a nice subject
subj="Change of IP on $HOSTNAME"
#now let's get the external ip
ip=`/usr/bin/lynx -dump 'http://whatismyip.org'`
#and create a message body
mesg="$HOSTNAME IP address on `date  +%a' '%b' '%e' '%H.%M.%S' '%Z' '%Y` is $ip"
#see if the ip has changed
oldip=`cat $logfile`
#or if the last message has been sent more than one week ago
age=`find $logfile -mtime +7`

if [ ! "$oldip" == "$ip" ] || [ ! "$age" == "" ]
then
 #keep the new ip into the logfile

 echo $ip &gt; $logfile
 #send email about it
 echo "$mesg" | mail -r"$sender" -s"$subj" "$recipient"
fi

exit 0


Of course, having an external web server, capable of running a little php script will allow us to replace http://whatismyip.org with our own webserver. The php script is really simple:


<?php
$ip = getenv('REMOTE_ADDR');
#$ip = $_SERVER['REMOTE_ADDR'];
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
' . $ip . '
</body>
</html>';
?>

Blog Archive