目錄表
How to back up your data
You have several main ways of doing so:
1 : The tar.gz via SSH classic
This method allows you to back up your data whenever you need it, or to install an automated process. You simply need to execute a backup script as root on your server. This script will simply send the data to the location of your choice after having compressed it in a tar.gz file.
Create an SSH key for your backup
On the client, you need to create an SSH key :
$ if [ ! -d ~/.ssh ]; then mkdir .ssh && chmod 700 .ssh; fi $ ssh-keygen -f ~/.ssh/id_rsa_backup -N '' -t rsa -b 2048
The content of the public key is in the file : ~/.ssh/id_rsa_backup.pub.
Let's copy it on the server :
$ scp ~/.ssh/id_rsa_backup.pub <vm-user>@<vm-host>:/home/<mon-compte>/
Now, we need to add the key on the server, in the file: /root/.ssh/authorized_keys. Once connected on the server :
$ sudo su # if [ ! -d /root/.ssh ]; then mkdir /root/.ssh && chmod 700 .ssh; fi # if [ ! -f /root/.ssh/authorized_keys ]; then touch /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys; fi # echo "command=\"/root/bin/backup_wrapper.sh\" `cat id_rsa_backup.pub`" >> /root/.ssh/authorized_keys # exit $ rm ~/id_rsa_backup.pub
We just added the key and restricted its use to the command: /root/bin/backup_wrapper.sh
From the client, let's try to login and check that it will try to start the command: /root/bin/backup_wrapper.sh (which does not exist yet). Please replace <vm-host> by the name of your server).
$ ssh -i ~/.ssh/id_rsa_backup root@<vm-host>
Script for the backup
Let's go back on the server in order to create the script /root/bin/backup_wrapper.sh
$ sudo su # if [ ! -d /root/bin ]; then mkdir /root/bin; fi # touch /root/bin/backup_wrapper.sh # chmod 700 /root/bin/backup_wrapper.sh # touch /root/bin/backup.sh # chmod 700 /root/bin/backup.sh # exit
Another script: /root/bin/backup.sh has also been created. It works in a rather simple manner. Using the key, the script /root/bin/backup_wrapper.sh is 'called” at each SSH connection. The script will check that the requested command is /root/bin/backup.sh. If if is the case, it will start the script.
Voici le code de /root/bin/backup_wrapper.sh
#!/bin/sh if [ "$SSH_ORIGINAL_COMMAND" = "/root/bin/backup.sh" ]; then $SSH_ORIGINAL_COMMAND else echo "Rejected" fi
Finally : here is the file: /root/bin/backup.sh
#!/bin/sh /bin/tar cvfz - /etc /root /home /var/backups /var/lib /srv/<my-disk>
Please replace /srv/<my-disk> by the path of your data disk. Add all the paths to be backed up as necessary.
This script is really basic, and can be completed by a backup of the MySQL database if you have one. For example, before the tar command, we can add:
/usr/bin/mysqldump --defaults-file=/etc/mysql/debian.cnf -A -Q --opt | /bin/gzip > /var/backups/mysql-dump.sql.gz
Please note : this is only valid for a Debian or an Ubuntu server.
Executing the script from the client
From the client, simply use the following command:
$ ssh -i ~/.ssh/id_rsa_backup root@<vm-host> "/root/bin/backup.sh" > sauvegarde.tar.gz
Setup an automated backup
For an automated backup from the client using the backup command given above, we can use cron.
Edit your crontab on the client:
$ crontab -e
The command crontab -e
opens the crontab file ready to edit.
To execute the command everyday at 11PM for example, enter the command like so:
MAILTO=<votre-adresse-mail> 0 23 * * * ssh -i ~/.ssh/id_rsa_backup root@<vm-host> "/root/bin/backup.sh" > backup.tar.gz
Save the file and quit the editor to update the crontab.
The file backup.tar.gz will be created in your home directory, and you will receive a mail at the address specified in MAILTO with the backup command that will allow you to check the backup went well.
2: Rsync
This method allows you to copy the entire data on the server (Linux) on a local machine (Linux, or using Cygwin).
rsync is a GNU command allowing a synchronization of distant machines. You can access the online help with the command info rsync (there are also excellent articles on ubuntu.org).
We suggest that you log in via ssh.
The advantage of this method is that it clones the entire server (new files and modified files are copied locally, deleted files on the server since the last sync, are deleted from the local copy).
#!/bin/bash #Retrieves all the files from the server gdm rsync -r -t -v -z --progress --rsh=ssh my_server.com:/srv/d_my_disk/ /home/backup read -n 1 -p "Press a key to continue..."
Note: the /
at the end of the directory is not optional.
3. GSync
Under Linux, there is a graphic interface called GSync. See the GSync installation and setup guide.
À développer