Quantcast
Channel: Tech Support Forum » Wizard32
Viewing all articles
Browse latest Browse all 6

BackUp Apache and MySQL using scp / rSync and mysqldump

$
0
0

In the previous Tutorial we saw how to generate RSA key to authenticate the SSH connection with Server. In this specific tutorial we will present the process to realize our backup server with the scp protocol and also with the protocol rSync. Then we will see how we can do our database backup using the mysqldump software.

SCP – Secure Copy
The SCP protocol is a network protocol, which supports file transfers between hosts on a network. SCP uses Secure Shell (SSH) for data transfer and utilizes the same mechanisms for authentication, thereby ensuring the authenticity and confidentiality of the data in transit. A client can send (upload) files to a server, optionally including their basic attributes (permissions, timestamps). Clients can also request files or directories from a server. SCP runs over TCP port 22 by default.

RSYNC
Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, from/to another host over any remote shell, or from/to a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination.

MYSQLDUMP
The mysqldump client is a backup program originally written by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or transfer to another SQL server (not necessarily a MySQL server). The dump typically contains SQL statements to create the table, populate it, or both. However, mysqldump can also be used to generate files in CSV, other delimited text, or XML format.

(Note: For the purpose of this tutorial we will use BackTrack 5 R2 as OS, scp / rSync and mysqldump Protocol. If you are a Windows user, you can download PuTTY free software application, which provides SSH connection with your Server)

In this tutorial we will see and analyze the modules:

  • SSH using RSA-Key Authentication
  • Backup using scp Protocol
  • Backup using rSync Protocol
  • SCP vs rSync
  • Export Database using mysqldump option
  • Conclusion

SSH using RSA-Key Authentication
Follow this step from the first Tutorial to gain access through SSH. Create – Generate RSA key to authenticate the connection with the server.

Tutorial: http://www.liatsisfotis.com/2012/04/ssh-using-authentication-rsa-key.html

Backup using scp Protocol
First let’s create the folder on the desktop to store the files that will make backup from our server.

mkdir /root/Desktop/BackupFiles

As a start we will use the scp protocol. The command to copy files is:

scp --[option] user@host:directory / TargetFile

In our example the TargetFile is the folder which created on the Desktop. The directory by default is the path /var/www/

So, on terminal type the following command:

scp -r 192.168.1.70:/var/www /root/Desktop/BackupFiles/

As you can see after -r option I just type the host from which I want to realize the backup. This happens because we have created and authenticated the connection with our RSA key. If not after it type the username@host from the server you want to connect.

(Note: The -r option specifies that recursive copying of entire directories will be enabled. If you omit the -r option we will receive the following message which informs us that the path /root/www/ is not a file to be copied)

Some people may want to automate this process with a script, so it should probably be noted that passwords should never be included in commands entered into the command line on shared servers (which many people use). Any logged-in user can see the username and password for several seconds via a couple different commands.

In such situations, a password file should be used.

In the case of mysqldump, a defaults file can be used. Create one such as ~/.mysqldump.cnf with appropriate content and permissions:

[server.com: ~]$ cat > ~/.mysqldump.cnf <<EOF
 > [client]
 > user=myuser
 > password=mypassword
 > EOF
 [server.com: ~]$ chmod 600 ~/.mysqldump.cnf

Example script using the above:

#!/bin/bash
 cd ~/backup/
 mysqldump --defaults-file=~/.mysqldump.cnf DatabaseName1 > ~/backup/DatabaseName1.sql

Rsync can also use password files, though they are a bit different from the above. The password file should have the same permissions as above, but should contain only the password on a single line.

Usage is:

rync -avz --progress -e --password-file=passwd.file ssh 192.168.1.70:/var/www /root/Desktop/BackupFiles

 

Backup using rSync Protocol

(Note: Is recommended to use one of two ways to create backup. For the purposes of this tutorial I will show you both ways, but in my opinion to create backups, use the rSync protocol.)

In the next step we will backup the apache using the rsync protocol. The rsync command is:

rsync --[option1] --[option2] ssh user@host:directory / TargetFile

So, on terminal type the following command:

rync -avz --progress -e ssh 192.168.1.70:/var/www /root/Desktop/BackupFiles

 

(Note: The options -a –archive, -v –verbose – increase verbosity, -z –compress – compress file data during the transfer, -e –rsh – specify the remote shell to use. Also we can use the –progress if we want to display the status of the backup in percentage rate)

SCP vs rSync
The both ways are the same. The difference between scp and rsync is that SCP is just CP over SSH. Instead of just providing target and source, it also provides authentication in the form of uid/pwd and an SSHD on your end when copying down (downloading).

rSync is used for backing up entire systems, and just keeping single files synchronized on different systems. rsync, only copies parts of the file(s) that have been changed, keeping source A and source B in ‘sync’.
So in the future if we make a change to any of the files in the apache, we will use the rsync protocol to create our backup.

Export Database using mysqldump option
At this point we will backup the database from our Server. Firstly we must connect to the Server via SSH. After successful connection let’s create a folder to save the backup of our mysql database. So on terminal type:

mkdir /var/mysql

Next we will use the mysqldump option to backup the whole database.

The mysqldump command is:

mysqldump --[option] -u[username] -p[database_name] > /path/to/save/the/file.sql

On terminal type the command:

mysqldump --opt -u root -p databasename > /etc/mysql/mysqlBackup.sql

(Note: The –opt option omit the –add-drop-table argument in case we plan to merge this backup with an existing database when we import it. This option means the backup will totally replace the old database when it is imported.The -u option specifies the username with which we connect and the -p  means that we have to enter the password of the database.The password of the database differs from the password that we connect to the Server via SSH.)

After successful transferring logout from the Server.

logout

Finally we must transfer the mysqlbackup.sql file from Server to our local machine. We do that by typing the command:

scp -r 192.168.1.70:/root/mysql/mysqlBackup.sql /root/Desktop/BackupFiles

Conclusion
We can see that the folder in the desktop contains both apache backup and mysql database backup.

© 2013 Liatsis Fotis


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images