martes, 29 de junio de 2010

Backup Linux Ubuntu - RSYNC

RSYNC COPIA LOCAL

rsync -altgvb /var/data/ /home/user/backup

RSYNC SSH - COPIA A UN SERVER.

rsync -e ssh -altgvb /data/ root@192.168.0.1:/home/user/backup

- Hasta aqui todo esta bien, pero si necesitamos hacer esto automaticamente cada hora mediante cron tenemos que lograr que ssh no nos pida contraseña. Para lograrlo realizamos el siguiente procedimiento:

Creamos una llave publica y una llave privada:
ssh-keygen -tdsa

Presionando solo enter en todas las preguntas que el comando anterior requiere vamos a tener nuestra llave publica en /home/usuario/.ssh/id_dsa.pub. Copiamos esta llave al servidor 192.168.0.1:

cd /home/usuario/.ssh/
cat id_dsa.pub | ssh bot@192.168.1.4 "cat - >> /home/bot/.ssh/authorized_keys"

Probamos que todo funcione haciendo:
ssh -l bot 192.168.1.4



Estas son las opciones de Rsync para entender mejor los comandos de arriba.



n no: no transferir solo mostrar lo que hay que hacer
# -a modo archivo (= -rlptDg)
# -r recursivo
# -l preservar soft links
# -p preservar permisos
# -t preservar fecha
# -D preservar dispositivos (solo root)
# -g preservar grupo
# -v modo verboso (-vv mas verboso)
# -z comprimir (si lo admite el servidor)
# -C ignorar archivos como lo hace CVS
# -u update: mantiene archivo destino si existe y es posterior
# -b backup: renombrar archivos destino preexistentes a extensión ~
# --stats imprimir estadisticas al final (solo si se ha puesto también -v)
# --delete borrar archivos en destino si no existen
# -R path relativos (crear rutas completas en el destino)


./rsync.sh install [push | pull] [local_dir] [remote_user] [remote_host] [remote_dir] [remote_ssh_port]

./rsync.sh install pull /home/brett/ brett 192.168.1.2 /home/brett 2222

./rsync.sh run [push | pull] localhost:'/home/paul /media/sdb1/music' [remote_user] [remote_host] [remote_dir] [remote_ssh_port]

The ssh-copy-id command is useful for copying the public key.
ssh-copy-id -i user@host




CLI (Command Line)
rsnapshot: a filesystem snapshot utility for making backups of local and remote systems. The disk space required is just a little more than the space of one full backup, plus incrementals. (My program does not use incrementals).
Code:

sudo aptitude install rsnapshot

Vocabulary

* ssh: a network protocol that allows data to be exchanged using a secure channel (encryption) between two computers
* rsync: a software application for Linux which synchronizes files and directories from one location to another using minimal bandwidth (only transfers files (or parts of files) that don't exist)
* mirror: an exact copy of a data set
* push: to send (or give) data from one computer to another
* pull: to receive (or ask) for data from one computer to another


Let's get started
It has several flags and options, but some are hard-coded, so you may have to edit the script by hand.

Options
General usage:
Quote:
./rsync.sh ['uninstall' | 'install' | 'run'] ['push' | 'pull'] [local_dir] [remote_user] [remote_host] [remote_dir] [remote_ssh_port]
Note: '[remote_ssh_port]' is usually 22 unless you change it from the default. If you don't know how to change the ssh port, its more than likely 22.

install
Use this option if you want to setup an automated, nightly backup between one computer and another.
Arguments:
Quote:
./rsync.sh install [push | pull] [local_dir] [remote_user] [remote_host] [remote_dir] [remote_ssh_port]
Example:
Quote:
./rsync.sh install pull /home/brett/ brett 192.168.1.2 /home/brett 2222

* Creates the following directories: $HOME/bin, $HOME/cron, $HOME/logs
* Creates 4096-bit RSA encryption key (illegal in the USA I think)
* Installs file to directories above


uninstall
Use this option if you want to remove a previously installed setup.
Arguments:
Quote:
./rsync.sh uninstall [remote_user] [remote_host]
Example:
Quote:
./rsync.sh uninstall brett 192.168.1.2

* Removes cron-job
* Removes RSA key


run
Use this option if you want to run the program without installing anything. Good to use as a test and on a single-needs basis.
Arguments:
Quote:
./rsync.sh run [push | pull] [local_dir] [remote_user] [remote_host] [remote_dir] [remote_ssh_port]
Example:
Quote:
./rsync.sh run pull /home/brett/ brett 192.168.1.2 /home/brett 2222

* Runs rsync with your parameters




That turns out to be really easy. If you’ve got ssh, then you’ve probably got ssh-keygen, which exists for the sole purpose of generating public and private keys, which when created without a passphrase can be used for password-free logins. So I ran ssh-keygen to generate a 2048 bit RSA key without a passphrase (aka a passphraseless key). I could also have generated a 1024 bit DSA key. I’m not sure I understand the difference. I’m not sure it matters.

ssh-keygen -b 2048

Inside ~/.ssh, ssh-keygen created two standard files, id_rsa and id_rsa.pub, the private and public keys respectively. The next and final step is to copy and “install” the public key on my backup server (192.168.0.100).

ssh-copy-id -i ~/.ssh/id_rsa.pub jwatt@192.168.0.100

ssh-copy-id uses ssh to copy the public key to the remote server and appends it to the ~/.ssh/authorized_keys file. Of course I didn’t know about ssh-copy-id when I started, so I just scp-ed the file over and pasted the public key into the authorized_keys file.

At which point I could use ssh to login without a password! ssh knows to automatically check for the existence of the id_rsa private key and try logging in with that.

ssh 192.168.0.100

Hot damn! That alone makes me want to start distributing my public key around to every server I access regularly. Of course the other benefit (and the whole point of this post!) is that now I’ll also be able to cron an rsync backup without requiring a password.

My ideal backup is a relatively current mirror of my home directory. I’m not looking for modified file snapshots or entire bootable filesystem images, I just want to know that if my hard drive crashes, most of my data (especially the photos) is recoverable. To that end, my rsync needs are relatively simple, though it took some tweaking to get to this point:

rsync -aze ssh --delete --exclude=".*/" /home/jwatt/ jwatt@192.168.0.100:/home/jwatt/backup/x23/

The -a option means archive files—it’s really an alias for a lot of other options having to do with maintaining permissions and timestamps, etc. The -z option uses compression when transferring files. The -e ssh option tunnels the file transfer over an encrypted ssh connection. The --delete option deletes any destination files that have been deleted from the source. The --exclude=".*/" option skips hidden files and directories. Finally the last two parts are the source (in this case everything under my home directory) and the destination I’ve already set up on my backup server.

And that’s it. I added it to my cron to run daily at 10pm. Set and forget it.

No hay comentarios:

Publicar un comentario