CyberTek - the future at the present!
Google

Archive for the ‘scripts’ Category

Script per salvataggio databases di mysql

Posted by: admin (set 21)

Oggi vi presento un comodo database ingrado di automatizzare il salvataggio dei nosri database mysql, in aggiunta allo script originale si può aggiungere al termine del salvataggio e compressione dei singoli databases in poche righe la copia su un server remoto e un messaggio di report nella nostra casella email.

 

#!/bin/bash
# Shell script to backup MySql database
# To backup Nysql databases file to /backup dir and later pick up by your
# script. You can skip few databases from backup too.
# For more info please see (Installation info):
# http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/01/mysql-backup-script.html
# Last updated: Aug – 2005
# ——————————————————————–
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2004, 2005 nixCraft project
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# ————————————————————————-
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
#Updated Sep – 2007 by CyberTek – http://www.cybertek.it

# ————————————————————————-

MyUSER="SET-MYSQL-USER-NAME" # USERNAME
MyPASS="SET-PASSWORD" # PASSWORD
MyHOST="localhost" # Hostname

# Linux bin paths, change this if it can't be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"

# Backup Dest directory, change this if you have someother location
DEST="/backup"

# Main directory where backup will be stored
MBD="$DEST/mysql"

# Get hostname
HOST="$(hostname)"

# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"

# File to store current backup file
FILE=""
# Store list of databases
DBS=""

# DO NOT BACKUP these databases
IGGY="test"

[ ! -d $MBD ] && mkdir -p $MBD || :

# Only root can access it!
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST

# Get all database list first
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"

for db in $DBS
do
skipdb=-1
if [ "$IGGY" != "" ];
then
for i in $IGGY
do
[ "$db" == "$i" ] && skipdb=1 || :
done
fi

if [ "$skipdb" == "-1" ] ; then
FILE="$MBD/$db.$HOST.$NOW.gz"
# do all inone job in pipe,
# connect to mysql using mysqldump for select mysql database
# and pipe it out to gz file in backup dir :)
$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
fi

done 

# copy on remote server
echo '*******************Example_Backup*******************' > /root/mybackup.txt
echo \ >> /root/mybackup.txt
date >> /root/mybackup.txt
echo \ >> /root/mybackup.txt
echo '*********************Details*********************' >> /root/mybackup.txt
#ricordati che nel comando scp puoi non specificare la password se hai autorizzato una key sul sistema di destinazione
#diversamente dovrai farlo

scp /backup/mysql/* root@example.dom:/home/backup/example/mysql/
ls -oh /backup/mysql/ >> /root/mybackup.txt
echo '*************************************************' >> /root/mybackup.txt
#invia una email  col report del lavoro eseguito
mail -s "Example Backup" example@example.dom < /root/mybackup.txt

#remove after upload
rm -f /backup/mysql/*

Published in: development, mysql, scripts, tips

Script per backup su dvd

Posted by: admin (ago 29)

Nel seguente script viene effettuato un backup di alcune cartelle tramite masterizzazione su dvd, al termine viene mandato un report via email all'indirizzo indicato. Si è utilizzata la modalità -M (append) inquanto il parametro -Z se avviato in maniera non interattiva non funziona correttamente in presenza di dati sul supporto, è possibile cmq provare ad utilizzare growisofs -use-the-force-luke=tty -Z anche in modalità non interattiva pur avendo già dei dati sul supporto.

Esempio:

Crea un file e rendilo eseguibile:

> touch /bin/dvdbackup.sh

> chmod 755 /bin/dvdbackup.sh

Aggiungi le seguenti istruzioni:

    #!/bin/bash
    # Rimpiazza le locazioni del programma con quelle del tuo sistema operativo
    GROWISOFS=/usr/bin/growisofs
    ECHO=/bin/echo
    GREP=/bin/grep
    MAIL=/bin/mail
    today=`/bin/date +%A`
    # Backup begins below
    $ECHO "Creating $today backup" >> /tmp/backupoutput.tmp
    $ECHO >> /tmp/backupoutput.tmp
    $GROWISOFS -use-the-force-luke=tty -dvd-compat -M /dev/dvd -D -J -R -T -l
     -graft-points -joliet-long -hide-joliet-trans-tbl
     -iso-level 4 -speed=1 -overburn -V "$today Backup"
     etc/mail=/etc/mail
     var/spool/mail=/var/spool/mail
     home=/home
     root=/root >> /tmp/backupoutput.tmp 2>&1
    # Modifica i percorsi di salvataggio quì sopra come da necessità
    mycode=$?
    $ECHO >> /tmp/backupoutput.tmp
    $ECHO "Si è verificato un errore, codice errore: $mycode" >> /tmp/backupoutput.tmp
    $ECHO >> /tmp/backupoutput.tmp
    $ECHO "End of $today backup" >> /tmp/backupoutput.tmp
    $GREP -i -v sleeping /tmp/backupoutput.tmp > /tmp/backupoutput.tmp2
    $GREP -i -v formatting /tmp/backupoutput.tmp2 > /tmp/backupoutput.txt
    $MAIL -n -s "Risultato del backup su DVD" usermail@domain < /tmp/backupoutput.txt

 

In crontab basta poi aggiungere la schedulazione per avviare in maniera riscorsiva il backup:

> crontab -e

Esempio di linea da aggiungere:

0 20 * * * /bin/dvdbackup.sh 

Published in: development, scripts, tips

Trovare IP attivi in una rete LAN

Posted by: admin (lug 29)

Molto semplicemente per trovare gli indirizzi attivi in una rete LAN è sufficiente utilizzare il comando "nmap", naturalmente dalla tua Linux Box preferita.

Ex.: 

nmap -sP 192.168.1.0/24

Published in: linux, scripts, tips

Masterizzare CD & DVD da console Linux

Posted by: admin (lug 9)

In breve i comandi per masterizzare cd & dvd da console Linux:

//cancellazione/formattazione di un cd-rw inserito nel device "/dev/hdd"

myhost:~ # cdrecord -blank=fast -force dev=/dev/hdd

//scrittura dei files contenuti nel percorso "/backup" su device "/dev/hdd"

myhost:~ # mkisofs -v -R /backup | cdrecord -v fs=4 speed=40 dev=/dev/hdd -

//Scrittura dei files contenuti nel percorso "/backup" sul device masterizzatore di dvd "/dev/hdc"

myhost:~ # growisofs -Z /dev/hdc -R -J /backup/

//Per creare un'immagine iso da masterizzare

# mkisofs -r -o /tmp/var-www-disk1.iso /var/www

//Usare growisofs per scrivere l'immagine ISO sul dispositivo DVD

# growisofs -Z /dev/dvd=/tmp/var-www-disk1.iso
 

//Per aggiungere dati ad un dvd conente altri dati

# growisofs -M /dev/dvd /tmp/file.1
 

//Per formattare o cancellare un dvd

# dvd+rw-format -force /dev/dvd

//Oppure

# dvd+rw-format -force=full /dev/dvd

//Per visualizzare info sul supporto

# dvd+rw-mediainfo /dev/dvd

  

Dopo le pillole appena riportate raccomandiamo di leggere il seguente howto

Published in: linux, scripts, shell, tips

Impostare una rotta su Linux

Posted by: admin (mag 23)

Route è il comando Linux che viene utilizzato per manipolare le tabelle di routing. Permette di aggiungere ed eliminare route statiche e default gateway, oltre che semplicemente visualizzare la tabella di routing di un sistema. Non è comune in altri Unix.

route add [-net|-host] indirizzo [gw gateway] [netmask netmask] [mss mss] [metric metric] [dev device]
route del indirizzo


Per aggiungere una route statica per un'intera rete si usa l'opzione add e si devinisce la rete con -net. Per esempio:
route add -net 192.168.0.0 netmask 255.255.255.0 gw 10.0.0.254
Aggiunge una route statica per la rete 192.168.0.0/24 usando come gateway 10.0.0.254.

Per impostare il default gateway si può digitare qualcosa come:
route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.0.0.1 oppure:
route add default gw 10.0.0.1

Per cancellare una route esistente basta indicare il nome della rete:
route del -net 192.168.0.0

 

Per visualizzare la tabella di route basta: route, se si vuole evitare il reverse lookup degli IP e velocizzare l'operazione scrivere:

route -n

Per visualizzare la cache del sistema sulle route usate:

route -C

 

Published in: linux, scripts, shell, tips
Powered by CyberTek - Assistenza Remota