Image

Image

Search This Blog

Tuesday, February 04, 2014

Recover / from a full backup

If we have a nightly dd of the / partition and we need to put that on a new disk, we have to follow this procedure:
let's mount the image in /tmp/1 and the new disk in /media/sdb1:

fdisk -l media/server/disk2/backup/backup,img - note the start block and the sector size, then multiply them to obtain the mount offset.

Disk /media/server/disk2/backup/backup.img: 7.5 GiB, 8004304896 bytes, 15633408 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0000d331

Device                              Boot Start   End  Blocks  Id System
/media/server/disk2/backup/backup.img 1 * 62  15633407 7816673 83 Linux

512*62=31744

mount -t ext4 -o loop,ro,noexec,noload,offset=31744 /media/server/disk2/backup/backup.img /tmp/1

mkfs.ext4 -O ^has_journal -E stride=2,stripe-width=1024 -b 4096 -l root /dev/sdb1

tune2fs -o journal_data_writeback /dev/sdb1
mount /dev/sdb1/ /media/sdb1

then we can copy the content of that image to the new disk:
cd /tmp/1 && find . -depth -print | cpio -padmV /media/sdb1

we need to sed the new UUID (blkid /dev/sdb1) into /media/sdb1/boot/grub/menu.lst and /media/sdb1/etc/fstab, then
mount --bind /dev /media/sdb1/dev
mount --bind /proc /media/sdb1/proc
mount --bind /sys /media/sdb1/sys
followed by chroot /media/sdb1 /bin/bash

in the chroot we need to check the value of the actual kernel in /lib/modules/xxxxxx.xx and run a mv /boot/initrd-xxxxxxx.xx.img /boot/old.img then
dracut /boot/initrd-xxxxxxx.xx.img xxxxxx.xx

As an alternative we can change the UUID of the new disk with tune2fs -U `old_uuid` /dev/device-name

Now we can run an /boot/grub/install-grub (or run grub, followed by find /boot/grub/stage1 then root (hd1,0) and  setup (hd1) ) and finally put the new disk in the machine and boot!

Friday, January 10, 2014

This one is touchy and not very fairplay ;)

It is looking at your MAC address and based on that, it creates some binary ids in global.conf. Change your MACs, remove the confs and you should be able to help your friends without the pesky time limit :)


#!/bin/bash

hw1=`echo -n 00; dd bs=1 count=5 if=/dev/random 2>/dev/null |hexdump -v -e '/1 ":%02X"'`
sudo /sbin/ifconfig eth0 | grep HW
sudo /sbin/ifconfig eth0 hw ether $hw1
sudo /sbin/ifconfig eth0 | grep HW
hw2=`echo -n 00; dd bs=1 count=5 if=/dev/random 2>/dev/null |hexdump -v -e '/1 ":%02X"'`
sudo /sbin/ifconfig wlan0 down
sudo /sbin/ifconfig wlan0 hw ether $hw2

sudo rm -rf ~/.teamxxxxxx*
sudo rm -rf ~/.config/teamxxxxxx*
sudo rm -rf /root/.teamxxxxxx*
sudo rm -rf /root/.config/teamxxxxxx*
sudo rm -rf /opt/teamxxxxxx9/config/*
sudo killall -9 teamxxxxxxd
if [ ! "$(pidof teamxxxxxxd)" ]; then
 sudo /opt/tteamxxxxxx9/tv_bin/teamxxxxxxd -d
 x=5
 while [ $x -ge 0 ]; do
  echo -en "Wait $x seconds..."\\r
  x=$(( $x - 1 ))
  sleep 1
 done
fi
sudo /opt/teamxxxxxx9/tv_bin/script/teamxxxxxx && sudo killall -9 teamxxxxxxd


For the not-officially-supported .tar.gz, just go and modify the launcher itself in the extracted folder:

#!/bin/bash

# If you can read this text, you probably attempted to start TeamXxxxxx.
# Please open a terminal (Konsole, gnome-terminal, xterm)
# Navigate to this folder (type 'cd /path/to/teamxxxxxx' [Enter])
# then execute TeamXxxxxx (type './teamxxxxxx' [Enter])

hw0=`echo -n 00; dd bs=1 count=5 if=/dev/random 2>/dev/null |hexdump -v -e '/1 ":%02X"'`
while read interface; do
sudo ip link set dev $interface address $hw0
done < <(sudo ifconfig | cut -d" " -f1 | sed '/^$/d' | grep -v lo)
sudo /usr/bin/systemctl restart NetworkManager.service

x=4
while [ $x -ge 0 ]; do
 echo -en "Wait $x seconds for the network to be up..."\\r
 x=$(( $x - 1 ))
 sleep 1
done

TV_SCRIPT_DIR="$(dirname "$(readlink -e "$0")")"
rm -rf $TV_SCRIPT_DIR/../../profile
rm -rf $TV_SCRIPT_DIR/../../config/*

source "$TV_SCRIPT_DIR/tvw_main"

Main "$@"

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

Sunday, December 22, 2013

Logon Script

@echo off
color 0B
TITLE Initialization de connexion avec le serveur...
echo (c)2012 Xxxxxxxx Inc. Test de la connexion...  ^


REM ********** define per location variables *********
set server=myserver
set serverip=123.45.67.89
set group=mygroup
set domain=MYDOMAIN
set mypassword=pass_1234
set maping1=net use X: \\%server%\home\%username% /yes
set maping2=net use W: "\\%server%\Dossier Partages" /yes
set maping3=net time \\%server% /set /yes
set groupmaping=net use Z: \\%server%\specialshare /yes
set vpn=rasdial "VPN connection" %username% %mypassword%
REM ********** end of specific variables *************

REM *** define generic variables ***
set tmpvar=0
set hostspath=%windir%\System32\drivers\etc\hosts
set lmhostspath=%windir%\System32\drivers\etc\lmhosts

REM *** check if the servername is added to hosts file ***
type %hostspath% | find "%server%" 1>nul 2>nul
if errorlevel 1 goto NOTFOUND

REM *** check if the server is accessible. launch vpn first if you have to ***
:FOUND
%vpn%
ping -n 5 %server% | find "TTL" 1>nul 2>nul
IF %ERRORLEVEL% == 0 goto GOOD
IF NOT %ERRORLEVEL% == 0 goto BAD

REM *** if the server is accessible map the drives and launch a second script from server ***
:GOOD
%maping1% 1>nul 2>nul
%maping2% 1>nul 2>nul
%maping3% 1>nul 2>nul
@SET LANG=ENU
@SET FILE_SERVER=%server%
\\%server%\netlogon\oxlogon.bat

REM *** Special settings for members of certain Security Groups ***
for /f %%f in ('"net user /domain %username% | findstr /i %group%"') do set /a tmpvar=1
if %tmpvar% lss 1 goto END
%groupmaping%  1>nul 2>nul
goto END

REM *** if the server is not accessible let the user know ***
:BAD
ECHO Pas de connexion avec HQ. Verifiez le access Internet!
goto END

REM *** if the servername is not into hostfile add it ***
:NOTFOUND
echo. >> %hostspath%
echo %serverip% %server% >> %hostspath%
echo. >> %hostspath%
echo. >> %lmhostspath%
echo %serverip% %server% #PRE #DOM:%domain% >> %lmhostspath%
echo. >> %lmhostspath%
echo Ajout de %server% dans %hostspath%
goto FOUND

REM *** end of script. let the user see the messages for a few seconds, then exit ***
:END
echo C`est fait, Merci!
ping -n 4 -w 1000 1.1.1.1 >nul
exit

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 03, 2013

AfterMarket Updater

That's what happen when the devs are not thinking how to update their product :D
No other comments! 
(Thou I should mention the trick that I've done in order to put the quotes in the right place )

@echo off
rem Update Logixxxx 2.0 
rem 2013/06/06 sorin@xxxxxxxx.com - rebuild the whole thing
rem 2014/03/04 sorin@xxxxxxxx.com - cleanup version detection
rem this script must be executed with administrative rights
cls

setlocal enableextensions enabledelayedexpansion

set source=\\192.168.35.214\Partage\LOGIxxxx_2_0_Mises_a_Jour
set exclude=%source%\deploy\exclude.txt
set destination32='%programfiles%\Groupe xxxxdure\Logixxxx 2.0'
set ver32="%destination32:'=%\version.txt"
set destination32="%destination32:'=%\"
set destination86='%programfiles(x86)%\Groupe xxxxdure\Logixxxx 2.0'
set ver86="%destination86:'=%\version.txt"
set destination86="%destination86:'=%\"
set /P version=<%source%\version.txt
if not defined version set version=0
set log=Logixxxx - 

if exist %destination32% (
 set log=!log! Found in Program Files.
  for /f "delims=" %%a in ('type %ver32%') do set actualversion=%%a
  if NOT "!actualversion!"=="%version%" (
  set log=!log! Update from version !actualversion! to %version%.
  xcopy %source% %destination32% /Y /S /E /H /R /I /C /EXCLUDE:%exclude%
  if exist "%allusersprofile%\Desktop" xcopy "%source%\deploy\MAJ - LOGIxxxx.lnk" "%allusersprofile%\Desktop\" /Y
  if exist "%allusersprofile%\Bureau" xcopy "%source%\deploy\MAJ - LOGIxxxx.lnk" "%allusersprofile%\Bureau\" /Y
  if exist "%public%\Desktop" xcopy "%source%\deploy\MAJ - LOGIxxxx.lnk" "%public%\Desktop\" /Y
  ) else (
  set log=!log! Same Version. No need to update.
  goto END
 )
 goto LOG
) else ( 
  set log=!log! Not found in %destination32%.
if exist %destination86% (
 set log=!log! Found in Program Files x86.
 for /f "delims=" %%i in ('type %ver86%') do set actualvers=%%i
 if NOT "!actualvers!"=="%version%" (
  set log=!log! Update from version !actualvers! to %version%.
  xcopy %source% %destination86% /Y /S /E /H /R /I /C /EXCLUDE:%exclude%
  if exist "%allusersprofile%\Desktop" xcopy "%source%\deploy\MAJ - LOGIxxxx.lnk" "%allusersprofile%\Desktop\" /Y
  if exist "%allusersprofile%\Bureau" xcopy "%source%\deploy\MAJ - LOGIxxxx.lnk" "%allusersprofile%\Bureau\" /Y
  if exist "%public%\Desktop" xcopy "%source%\deploy\MAJ - LOGIxxxx.lnk" "%public%\Desktop\" /Y
  ) else (
  set log=!log! Same Version. No need to update.
  goto END
 )
 goto LOG
) else ( 
set log=!log! Not found in %destination86%.
) )
set log=!log! Nothing to update.

:LOG
echo
echo %date% %time% - %computername%. !log! >> %source%\deploy\UpdateLogs.txt

:END
echo !log!
ping -n 2 1.1.1.1 >nul
endlocal

Saturday, October 19, 2013

Wednesday, October 02, 2013

Backup all custom MSSQL databases

@ECHO OFF
SETLOCAL

:: Set variables
set backuplocation="\\fileserver\files\Backup"
set mainsqlbase=SQLSERVER\MSSMLBIZ
set DBList=%temp%\SQLDBList.txt

:: Delete backups older than 7 days
forfiles -p %backuplocation% -s -m *.bak -d -7 -c "cmd /c del @PATH"

:: Get date in format YYYY-MM-DD (assuming the locale is US)
FOR /F "tokens=1,2,3,4 delims=/ " %%A IN ("%DATE%") DO SET NowDate=%%D-%%B-%%C

:: Build a list of databases to backup
SqlCmd -E -S %mainsqlbase% -h-1 -W -Q "SET NoCount ON; SELECT Name FROM master.dbo.sysDatabases WHERE [Name] NOT IN ('master','model','msdb','tempdb')" > "%DBList%"

:: Backup each database, prepending the date to the filename
FOR /F "tokens=*" %%I IN (%DBList%) DO (
ECHO Backing up database: %%I
SqlCmd -E -S %mainsqlbase% -Q "BACKUP DATABASE [%%I] TO DISK = N'%backuplocation%\%NowDate%_%%I.bak' WITH NOFORMAT, NOINIT,  NAME = N'MSSmallBusiness-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10"
ECHO.
)

:: Clean up the temp file
IF EXIST "%DBList%" DEL /F /Q "%DBList%"

ENDLOCAL

Yes, it can be done directly from the SQL itself, but it's funnier to write a script  :)

Blog Archive