Linux server implements automatic data backup-Remote Backup

Source: Internet
Author: User
System backup is a very important part of system management. This article describes in detail the backup methods of various Linux systems, and I believe it will be helpful for your daily management work. Backup is an important task, but many people do not. Once data is lost due to improper use, backup becomes a real life-saving person. This article will discuss how to set backup policies

After two days of hard work, I had to write a script program to back up the server. Here we will sort out the entire process:

We first set the server to CA, IP: 10.144.49.32, and backup server to CA-BAK. IP: 10.144.49.33

 

Step 2: implement automatic backup of the CA server. First, determine the directory to be backed up. Here, we want to back up the following three directories:

/Usr/local/TrUsdata/var/trusdatauser,/var/lib/mysql and/usr/local/trusdata/var/trusdatapoicy

Step 2: To prepare an automatic backup program, first we are sure to use a directory to store these files, first create a directory and then create a file bakupcaadata. cron is implemented using the following script and assigned to the file as an executable file.

Cd/

MkdirBack

TouchBakupcaadata. cron

Chmod711 bakupcaadata. cron

Step 2: compile an automatic backup program: purpose. The system automatically backs up the target directory, which is backed up once every morning and named by date. Use the following scripts to store datauser, datamysql, and policy in the three BACK directories.

[Root @ localhost back] # vi bakupcaadata. cron
ExPort DATETIME ='Date+ % Y % m % d'
Cd/usr/local/trusdata/var/
Tar-czf datauser. bak trusdatauser/
MvDatauser. bak/back/datauser. $ DATETIME. bak

Cd/var/lib/
Tar-czf datamysql. bak mysql/
Mv datamysql. bak/back/datamysql. $ DATETIME. bak
Cd/usr/local/trusdata/var/
Tar-czf datapoicy. bak trusdatapoicy/
Mv datapoicy. bak/back/policy/datapoicy. $ DATETIME. bak

Step 2: implement automatic backup on the local machine. We create a rule in the task file. The file for automatic task execution isCrontab, Use the following script:

[Root @ localhost. ssh] # vi/etc/crontab
SHELL =/bin/bash
PATH =/sbin:/bin:/usr/sbin:/usr/bin
MAILTO = root
HOME =/

# Run-parts
01 *** root run-parts/etc/cron. hourly
02 4 *** root run-parts/etc/cron. daily
22 4 ** 0 root run-parts/etc/cron. weekly
42 4 1 ** root run-parts/etc/cron. monthly
01 1 *** root/back/bakupcaadata. cron

[Root @ localhost. ssh] #/etc/rc. d/init. d/crond restart

The above four steps enable automatic backup of the local machine. AutoCpTo another server.

 

Step 2: Use raIdImplement or reinstall a system that is the same as the CA server. Here I cloned a system with the same touch with raid. Of course, this work should be done before the first step, because automatic backup is not required here.

 

Step 2: automatically copy data from the CA-bak to the CA server. The copy process uses certificate authentication and encrypted transmission. First, we use rsa to generate a pair of key pairs for each machine. The procedure is as follows:

On the CA server, add a user, roger, who is used when the CA-BAK copies data to the CA server;

[Roger @ localhost roger] $ ssh-keygen-B 1024-t rsa
Generating public/private rsa key pair.
EnterFileInWhichTo save the key (/home/roger/. ssh/id_rsa ):
CreatEdDirectory \ '/home/roger/. ssh \'.
Enter passphrase (empty for no passphrase ):
Enter same passphrase again:
Your identifiCatIon has been saved in/home/roger/. ssh/id_rsa.
Your public key has been saved in/home/roger/. ssh/id_rsa.pub.
The keyFingerPrint is:
60: 97: c5: 99: 12: 5e: b4: c6: f1: 6f: b9: 9a: cd: 8b: 8b: 04 roger@localhost.localdomain

[Roger @ localhost roger] $ mv id_rsa.pub roger. pub (used for distinguishing two servers)

Similarly, in CA-BAK

[Root @ localhost root] # ssh-keygen-B 1024-t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/. ssh/id_rsa ):
Enter passphrase (empty for no passphrase ):
Enter same passphrase again:
Your identification has been saved in/root/. ssh/id_rsa.
Your public key has been saved in/root/. ssh/id_rsa.pub.
The key fingerprint is:
67: ef: a3: 2a: 40: fb: 34: 20: d9: 76: b9: b0: 90: ef: 68: 06 root@localhost.localdomain

After a key pair is generated, the public key of each other is sent to the other party and renamed

On CA-BAK

Scp-r-p roger@10.144.49.32:/home/roger. pub/root/. ssh/

Cd/root/. ssh

Mv roger. pub authorized_keys (required)

On CA

Scp-r-p root@10.144.49.33:/root/. ssh/id_rsa.pub/home/roger/. ssh/

Cd/home/roger/. shh/

Mv id_rsa.pub authorized_keys

In this way, both parties are certified.

In this way, both parties can automatically copy

For example:

Roger@10.144.49.32:/back/id_rsa.pub/back
Id_rsa.pub 100% | ************************************* **************************************** * ************ | 237

Write an automatic copy program from the CA Server

 

Similarly, create a directory back and an executable backup. cron on the CA-BAK server. The script is as follows:

ExportDATETIME = 'date + % Y % m % d'

Scp-r-p roger@10.144.49.32:/back/datauser. $ DATETIME. bak/back/datauser

Scp-r-p roger@10.144.49.32:/back/datamysql. $ DATETIME. bak/back/datamysql

Scp-r-p roger@10.144.49.32:/back/policy/datapoicy. $ DATETIME. bak/back/policy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.