1 Backup la partitie in retea
2 Backup recursiv la directoare specifice utelizand tar & bash
3 Backup recursiv la directoare specifice utelizand flexbackup
4 Backup incremental cu Rsync
5 Backup Incremental in retea cu rdiff-backup
6 Pregatirea pentru Backup
1 Backup la partitie in retea
Pentru a face un backup la o partitie intreaga pe o masina remote ce se poate afla atat in reteaua interna cat si in Internet vom utiliza netcat si dd
Netcat il avem in doua variante :
# emerge gnu-netcat
sau
# emerge netcat
sau
# emerge netcat
Pentru un backup a partitiei de pe hda "exemplu /dev/hda1" vom porni netcat in modul "ascultare" listen pe PC pe care vom copia backup-ul.
# netcat -l -p 10000 > image.gz
Pe PC care are nevoie de backup vom rula dd pt a citi partitia a a o comprima cu gzip si transfera cu netcat pe PC-ul ce asculta.
# dd if=/dev/hda1 | gzip | netcat -w 5 remote_ip 10000
Pentru informatii suplimentare cum a clona un Linux utelizand netcat consultati clone_linux
2 Backup recursiv la directoare specifice utelizand tar & bash
Voi prezenta un bash script foarte simplu de care am dat pe forums.gentoo.org ce face un backup "ne incrimental". La baza acestui script stau (bash, tar, xargs, etc.)
Acest script pote fi inclus in cron sa faca backup saptamanal sau lunar sau se poate rulat manul cand e nevoie.
#!/bin/sh
# for FULL backups
# this backs up the important stuffs listed in ${lists} to ${BKPDIR}
# the lists *should* be in ${BKPDIR} and named.lst
# the resulting backups will be..tgz
# notes:
# - assumes ${BKPDIR} is unmounted and has an /etc/fstab entry
# - assumes /boot is unmounted and has an /etc/fstab entry
# - variables in CAPS are ok for you to set... change the other
# vars if you know what you're doing
# - you can get fancy in the lists... think xargs *wink*, but
# you can't use thes spanning feature to break up an
# archive to smaller pieces of arbitrary size
# - follow your security policy when setting perms on ${BKPDIR}
# written by razamatan
# DISCLAIMER: razamatan didn't write this if something goes wrong
BKPDIR=/mnt/.backup # where the backups go
BOOT=sys # list that has /boot
NUMBKPS=4 # how many backups to keep
if [ ! -d ${BKPDIR} ] ; then
echo ${BKPDIR} is not a valid directory or does not exist
fi
mount ${BKPDIR} # i have my backup directory on a seperate partition
lists=${BKPDIR}/*.lst
ext=tgz
for list in `ls ${lists}`; do
type=`basename ${list} .lst`
if [ ${type} = ${BOOT} ] ; then mount /boot ; fi
cat ${list} | xargs tar zlcf
${BKPDIR}/${type}.`date +%Y-%m-%d-%H%M`.${ext} > /dev/null 2>&1
if [ ${type} = ${BOOT} ] ; then umount /boot ; fi
num=${NUMBKPS}
for evict in `ls -t ${BKPDIR}/${type}.*.${ext}`; do
if [ ${num} -le 0 ] ; then rm -f ${evict}
else num=$((${num}-1)) ; fi
done
done
umount ${BKPDIR} # and i like to keep it unmounted
# for FULL backups
# this backs up the important stuffs listed in ${lists} to ${BKPDIR}
# the lists *should* be in ${BKPDIR} and named
# the resulting backups will be
# notes:
# - assumes ${BKPDIR} is unmounted and has an /etc/fstab entry
# - assumes /boot is unmounted and has an /etc/fstab entry
# - variables in CAPS are ok for you to set... change the other
# vars if you know what you're doing
# - you can get fancy in the lists... think xargs *wink*, but
# you can't use thes spanning feature to break up an
# archive to smaller pieces of arbitrary size
# - follow your security policy when setting perms on ${BKPDIR}
# written by razamatan
# DISCLAIMER: razamatan didn't write this if something goes wrong
BKPDIR=/mnt/.backup # where the backups go
BOOT=sys # list that has /boot
NUMBKPS=4 # how many backups to keep
if [ ! -d ${BKPDIR} ] ; then
echo ${BKPDIR} is not a valid directory or does not exist
fi
mount ${BKPDIR} # i have my backup directory on a seperate partition
lists=${BKPDIR}/*.lst
ext=tgz
for list in `ls ${lists}`; do
type=`basename ${list} .lst`
if [ ${type} = ${BOOT} ] ; then mount /boot ; fi
cat ${list} | xargs tar zlcf
${BKPDIR}/${type}.`date +%Y-%m-%d-%H%M`.${ext} > /dev/null 2>&1
if [ ${type} = ${BOOT} ] ; then umount /boot ; fi
num=${NUMBKPS}
for evict in `ls -t ${BKPDIR}/${type}.*.${ext}`; do
if [ ${num} -le 0 ] ; then rm -f ${evict}
else num=$((${num}-1)) ; fi
done
done
umount ${BKPDIR} # and i like to keep it unmounted
download script
3 Backup recursiv la directoare specifice utelizand flexbackup
Flexbackup este un script scris in perl ce poate face usor si rapid backup la directoarele importante.
# emerge flexbackup
Cam asa trebuie sa arate /etc/flexbackup.conf fisierul de configurare a lui flexbackup:
$set{'research'} = "/home/david/research /var/cvsroot/python /var/cvsroot/latex";
$set{'mail'} = "/home/david/.thunderbird";
$set{'etc'} = "/etc /home/david /var/www/davidgrant.ca/htdocs";
$prune{'/home/david'} = ".jpi_cache konserve-backup .cxoffice .wine .mozilla .kde3.1 .thunderbird";
$prune{'/home/david/.thunderbird'} = "Junk News";
$compress = 'gzip'; # one of false/gzip/bzip2/lzop/zip/compress/hardware
$compr_level = '6';
$device = '/mnt/sata/backup'
$set{'mail'} = "/home/david/.thunderbird";
$set{'etc'} = "/etc /home/david /var/www/davidgrant.ca/htdocs";
$prune{'/home/david'} = ".jpi_cache konserve-backup .cxoffice .wine .mozilla .kde3.1 .thunderbird";
$prune{'/home/david/.thunderbird'} = "Junk News";
$compress = 'gzip'; # one of false/gzip/bzip2/lzop/zip/compress/hardware
$compr_level = '6';
$device = '/mnt/sata/backup'
Puteti modifica fisierul de configurare in functie de ce directoare vreti sa fie salvate ca copie de rezerva
Fiecare director in "research" va fi salvat cu propriul tarball, cum ar fi "home-david-research.0.tar.gz", "var-cvsroot-python.0.tar.gz", si "var-cvsroot-latex.0.tar.gz".
$prune$ este o optiune foarte utila ce permite mask-area anumitor directoare pe care nu doriti sa le includeti in tarballs.
A fost setat $compress pe 'gzip' dar puteti uteliza de exemplu bzip2 daca vreti un tarball de dimensiuni mai mici . bzip2 este mai lent la arhivare si dezarhivare decat gzip. $compr_level=6 va executa compresi mai rapid ra se poate schimba intro alta valoare pt a creste nivelul de compresie.
Pentru a crea un Backup sistematic vom utiliza crontab
0 3 1-7 * * flexbackup -set all -full -w 7
0 3 * * 6 flexbackup -set all -differential
0 3 * * 1-5 flexbackup -set all -incremental
0 3 * * 6 flexbackup -set all -differential
0 3 * * 1-5 flexbackup -set all -incremental
Aceasta inregistrare va efectua un backup complet la fiecare sambata a fiecarei luni si un backup incremental in fiecare zi a saptamanii la oara 3am
Pentru detalii vizitati Flexbackup home page
4 Backup incremental cu Rsync
In Internet este un tutorial foarte detaliat Easy Automated Snapshot-Style Backups with Linux and Rsync despre crearea si efectuare de backup utelizand rsync unde veti gasi si un script complet pentru crearea scripturilor.
Utelizand backup incremental, puteti crea multiple snapshot-uri a discului si tot odata eficientiza utelizarea spatiului pe disc si a consumului de resurse necesar crearii si mentenantei backup-urilor. Rsync ne ofera o modalitatea foarte comoda pentru a crea creating backup de tip snapshot.
Voi prezenta cateva scripturi simple pe care le puteti modifica pentru a va satisface nevoile proprii de backup
Backup la fiecare 7 zile ca va utiliza pe un server dedicat pentru stocare backup-urilor.
#!/bin/sh
# This script does personal backups to a rsync backup server. You will end up
# with a 7 day rotating incremental backup. The incrementals will go
# into subdirectories named after the day of the week, and the current
# full backup goes into a directory called "current"
# tridge@linuxcare.com
# directory to backup
BDIR=/home/$USER
# excludes file - this contains a wildcard pattern per line of files to exclude
EXCLUDES=$HOME/cron/excludes
# the name of the backup machine
BSERVER=owl
# your password on the backup server
export RSYNC_PASSWORD=XXXXXX
########################################################################
BACKUPDIR=`date +%A`
OPTS="--force --ignore-errors --delete-excluded --exclude-from=$EXCLUDES
--delete --backup --backup-dir=/$BACKUPDIR -a"
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
# the following line clears the last weeks incremental directory
[ -d $HOME/emptydir ] || mkdir $HOME/emptydir
rsync --delete -a $HOME/emptydir/ $BSERVER::$USER/$BACKUPDIR/
rmdir $HOME/emptydir
# now the actual transfer
rsync $OPTS $BDIR $BSERVER::$USER/current
# This script does personal backups to a rsync backup server. You will end up
# with a 7 day rotating incremental backup. The incrementals will go
# into subdirectories named after the day of the week, and the current
# full backup goes into a directory called "current"
# tridge@linuxcare.com
# directory to backup
BDIR=/home/$USER
# excludes file - this contains a wildcard pattern per line of files to exclude
EXCLUDES=$HOME/cron/excludes
# the name of the backup machine
BSERVER=owl
# your password on the backup server
export RSYNC_PASSWORD=XXXXXX
########################################################################
BACKUPDIR=`date +%A`
OPTS="--force --ignore-errors --delete-excluded --exclude-from=$EXCLUDES
--delete --backup --backup-dir=/$BACKUPDIR -a"
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
# the following line clears the last weeks incremental directory
[ -d $HOME/emptydir ] || mkdir $HOME/emptydir
rsync --delete -a $HOME/emptydir/ $BSERVER::$USER/$BACKUPDIR/
rmdir $HOME/emptydir
# now the actual transfer
rsync $OPTS $BDIR $BSERVER::$USER/current
Backup pe un Ald HDD
#!/bin/sh
export PATH=/usr/local/bin:/usr/bin:/bin
LIST="rootfs usr data data2"
for d in $LIST; do
mount /backup/$d
rsync -ax --exclude fstab --delete /$d/ /backup/$d/
umount /backup/$d
done
DAY=`date "+%A"`
rsync -a --delete /usr/local/apache /data2/backups/$DAY
rsync -a --delete /data/solid /data2/backups/$DAY
export PATH=/usr/local/bin:/usr/bin:/bin
LIST="rootfs usr data data2"
for d in $LIST; do
mount /backup/$d
rsync -ax --exclude fstab --delete /$d/ /backup/$d/
umount /backup/$d
done
DAY=`date "+%A"`
rsync -a --delete /usr/local/apache /data2/backups/$DAY
rsync -a --delete /data/solid /data2/backups/$DAY
Backup automat la /home/utelizator pe acelshi hdd
#!/bin/sh
cd ~susan
{
echo
date
dest=~/backup/`date +%A`
mkdir $dest.new
find . -xdev -type f ( -mtime 0 -or -mtime 1 ) -exec cp -aPv "{}"
$dest.new ;
cnt=`find $dest.new -type f | wc -l`
if [ $cnt -gt 0 ]; then
rm -rf $dest
mv $dest.new $dest
fi
rm -rf $dest.new
rsync -Cavze ssh . samba:backup
} >> ~/backup/backup.log 2>&1
cd ~susan
{
echo
date
dest=~/backup/`date +%A`
mkdir $dest.new
find . -xdev -type f ( -mtime 0 -or -mtime 1 ) -exec cp -aPv "{}"
$dest.new ;
cnt=`find $dest.new -type f | wc -l`
if [ $cnt -gt 0 ]; then
rm -rf $dest
mv $dest.new $dest
fi
rm -rf $dest.new
rsync -Cavze ssh . samba:backup
} >> ~/backup/backup.log 2>&1
Daca va simtiti incomod de a uteliza sau modifica scripturile bash vizitati rsnapshot unde veti gasi rsnapshot ce a ajuns la versiunea 1.2.1 si pe care puteti sa-l folositi cu incredere in prealabil citind documentatia de pe site.
Vom incerca cateva exepmle manuale pentru rsync
Sa presupunem ca vreiti sa
* creati o imagine exacta a discului sau partitie .
* Aveti deja un hdd separat pregatit pet backup ce poate fi IDE, USB in retea etc.(in examplu avem /mnt/usbharddrive):
Backup la intreaba partitie:
# rsync --progress --stats -avxzl --exclude "/mnt/usbharddrive/" --exclude "/mnt/usbharddriveboot/" --exclude "/usr/portage/" --exclude "/proc/"
--exclude "/root/.ccache/" --exclude "/var/log/" --exclude "/sys" --exclude "/dev" --exclude "tmp/" /* /mnt/usbharddrive
--exclude "/root/.ccache/" --exclude "/var/log/" --exclude "/sys" --exclude "/dev" --exclude "tmp/" /* /mnt/usbharddrive
Dublicam partitia si stergem _update la fisierele ce au fost rescriese de la primul backup:
# rsync --progress --stats --delete -avxzl --exclude "/mnt/usbharddrive/" --exclude "/mnt/usbharddriveboot/" --exclude "/usr/portage/"
--exclude "/proc/" --exclude "/root/.ccache/" --exclude "/var/log/" --exclude "/sys" --exclude "/dev" --exclude "tmp/" /* /mnt/usbharddrive
--exclude "/proc/" --exclude "/root/.ccache/" --exclude "/var/log/" --exclude "/sys" --exclude "/dev" --exclude "tmp/" /* /mnt/usbharddrive
Backup la partitia de boot:
# rsync --progress --stats -avxzl /boot /mnt/usbharddriveboot
# rsync --progress ---avxzl --stats --delete /boot /mnt/usbharddriveboot
# rsync --progress ---avxzl --stats --delete /boot /mnt/usbharddriveboot
Pentru restaurarea backup-ului putem utiliza Gentoo Live CD. Restaurarea se va efectia de pe exemplu /mnt/usbharddrive pe /mnt/driveRestore.
5 Backup Incremental in retea cu rdiff-backup
rdiff-backup este un utelitar foarte flexibil ce permite un backup total si/sau incrimental.rdiff-backup permite un control extins al backup-urilor prin pastrarea atat fisierelor vechi cat si integrarea celor noi care au fost modificate. rdiff-backupne permite crearea backup remote prin ssh. Pentru mai multe detalii rdiff-backup Main page.
Isntalam rdiff-backup
emerge rdiff-backup
Pentru a crea backup in retea pe ambele masini este necesara instalarea rdiff-backup. NU va fi necesar drepturile de root pe cand rdiff-backup poate fi utelizat si de utelizatori obisnuiti.
Dupa instalare ptem trece direct la utelizarea rdiff-backup
In cazul de mai sus subdirectorul mydir-backup este creat in HOME pe masina utelizatorului remoteuser la remotehost.remotedomain. Daca acest director este deja prezent in Home atunci directorul va fi update-at pentru a reflecta modificarile de pe masina sursa iar diferentele nu vor disparea ci vor fi salvate la randul sau. Pentru mai multe detalii si exemple concrete vizitat http://www.nongnu.org/rdiff-backup/examples.html.
In functie de configurarea serviciului sshd si schema de autentificare puteti fi intrebat de parola de acces a utelizatorului pe mashina pe care se va salva backup-ul.
Acest proces de autentificare poate fi evitat utelizand (ssh-agent) si este in special util cand utelizam backup ca proces automat in cron. In cazul in care nu aveti configurat (ssh-agent) vom proceda:
Primul lucru pe care il facem ne m,utam pe masina ce va stoca backup-urile si avem nevoie de passphrase-less key pair ce va fi folosita de rdiff-backup.
Creem passphrase-less key pair
ssh-keygen -t dsa
Vom salva cheia .ssh/backup_dsa si enter pentru passphrase.
Acum copiem cheia creata in mashina la care se va face backup
scp .ssh/backup_dsa.pub remoteuser@remotehost.remotedomain
Pe mashine care va crea backup vom adauga cheia copiat in lista authorized keys
ssh remoteuser@remotehost
cat backup_dsa.pub >> .ssh/authorized_keys
rm backup_dsa.pub
cat backup_dsa.pub >> .ssh/authorized_keys
rm backup_dsa.pub
Acum a venit timpul sa-i specificm mashii ca cheia respectiva va fi utelizata numai pentru anumite actiuni care ne intereseaza. Editam .ssh/authorized_keys si specificam comanda inaintea fiecarei chei.
Exemplu
nano -w .ssh/authorized_keys sau remote machine
command="/usr/bin/rdiff-backup --server" ssh-dss ....
command="/usr/bin/rdiff-backup --server" ssh-dss ....
Acum putem trece la mashina la care trebuie facut backup-ul si ne asuguram ca rdiff-backup sa fie pornit, si vom configura mashina pentru utelizarea passphrase-less, key pair prin crearea ssh aliasing prin editarea .ssh/config si adaugam;
nano -w .ssh/config
Host remote-backup
Hostname remotehost.remodomain
IdentityFile ~/.ssh/backup_dsa
IdentitiesOnly yes
Host remote-backup
Hostname remotehost.remodomain
IdentityFile ~/.ssh/backup_dsa
IdentitiesOnly yes
In acest fel cand vom uteliza remote-backup host identifier in ssh sau scp PC-ul ca contacta remotehost.remodomain si in loc sa utelizeze cheia defaul va fi fortat sa utelizeze /.ssh/backup_dsa.
Configurarile de mai susu ne permit crearea de remote backup prin conectarea ssh la o alta mashina specificata fara a mai fi nevoie de a introduce parolla utelizatorului la cere ne conectam.
Acum a venit timpul sa configuram rdiff-backup ca un cron job pentru un backup automat editam local cron table ce este separat la fiecare utelizator in parte.
File: cron table on local machine
01 1 * * * rdiff-backup /home/localuser/mydir remoteuser@remote-backup::mydir-backup[code]
6 Pregatirea pentru Backup
In anumite cazuri cand mashina ruleaza mai multe aplicatii sau demoni cum este mysql sau nu sunt montate anumite partitii la care trebuieste facut backup-ul cum este /boot avem nevoie de o pregatire prealabila a sistemului inainte de crearea bakupu-rilor. Vom crea un script ce va monta partitia in read-only mode pe durata crearii backup-ului.
[code]nano -w /etc/scripts/flexwrapper.sh
#!/bin/sh
# mount /boot pentru backup
mount /boot
# MySQL Read Only
# EXAMPLU!
# Aveti grija ca parola mysql va fi in plain-text sau utelizati criptarea cu shc ;)http://www.gentoo.ro/content.php?article.47
mysql -u root --password='yourpass' -e "SET GLOBAL read_only=1;"
# Rulam flexbackup, unde vom pasa si parametrii
flexbackup $*
# dam 10 secunde de respiro pentru un clean up
sleep 10
# MySQL Read/Write
mysql -u root --password='yourpass' -e "SET GLOBAL read_only=0;"
# umount /boot
umount /boot
01 1 * * * rdiff-backup /home/localuser/mydir remoteuser@remote-backup::mydir-backup[code]
6 Pregatirea pentru Backup
In anumite cazuri cand mashina ruleaza mai multe aplicatii sau demoni cum este mysql sau nu sunt montate anumite partitii la care trebuieste facut backup-ul cum este /boot avem nevoie de o pregatire prealabila a sistemului inainte de crearea bakupu-rilor. Vom crea un script ce va monta partitia in read-only mode pe durata crearii backup-ului.
[code]nano -w /etc/scripts/flexwrapper.sh
#!/bin/sh
# mount /boot pentru backup
mount /boot
# MySQL Read Only
# EXAMPLU!
# Aveti grija ca parola mysql va fi in plain-text sau utelizati criptarea cu shc ;)http://www.gentoo.ro/content.php?article.47
mysql -u root --password='yourpass' -e "SET GLOBAL read_only=1;"
# Rulam flexbackup, unde vom pasa si parametrii
flexbackup $*
# dam 10 secunde de respiro pentru un clean up
sleep 10
# MySQL Read/Write
mysql -u root --password='yourpass' -e "SET GLOBAL read_only=0;"
# umount /boot
umount /boot
Acum a venit timpul sa inregistram in cron job flexbackup:
0 3 1-7 * * /etc/scripts/flexwrapper.sh -set all -full -w 7
0 3 * * 6 /etc/scripts/flexwrapper.sh -set all -differential
0 3 * * 1-5 /etc/scripts/flexwrapper.sh -set all -incremental
0 3 * * 6 /etc/scripts/flexwrapper.sh -set all -differential
0 3 * * 1-5 /etc/scripts/flexwrapper.sh -set all -incremental
Astep critici, aprecieri, completari daca sunt necesare.
Acet tutorial a fost tradus si adaptat dupa WiKi HOWTO_Backup
No comments:
Post a Comment