Use rsync and crond servers for WEB Service backup, rsynccrond
Back up WEB services using rsync and crond servers
Scenario:
A company has a Web server. The data in the server is very important, but if the hard disk breaks down, the data will be lost.
Now the leaders ask you to back up data on other machines. The requirements are as follows:
At every night, package the backup system configuration files, website program directories, and access logs on Web server.
The rsync command is pushed to server B for backup retention.
Backup idea: You can package data locally and then push it to the backup server.
The specific requirements are as follows:
1) the backup Directory of Web server A and backup server B must be/backup.
2) system configuration files include but are not limited:
A. configuration file of the scheduled Task Service
B. Enable the configuration of the self-starting file/etc/rc. d/rc. local.
C. Directory of daily scripts/server/scripts
D. configuration file/etc/sysconfig/iptables of the firewall iptabls
E. Other directories to be backed up
3) The Web server site directory is assumed to be/var/html/www
4) The Log Path accessed by Web server A is assumed to be (/app/logs)
5) The Web server retains the backup data for 7 days after packaging. Backup Server B retains a 6-month copy of data.
6) the backup server B is saved in the directory according to the IP address of the backup data server, and the packaged files are saved by time name.
Logical architecture :.....
Note:
1) leaders at work may not tell you how to do it, but only ask for it. For example, Cui and WEB server are very important.
Back up data on another server.
2) the logical architecture diagram is not likely to be drawn by the leader, but you understand the meaning of the leader, and then come up with a backup solution.
You just made a drawing before implementation.
Environment Description:
192.168.0.252 web
192.168.0.20.backup
Abstract technical points:
A. Implement the rsync service on the backup server B. A is the client.
B. Back up important data on A. In practice, write A backup script.
C. Perform a scheduled task and execute the backup script in step B.
Start deployment
A. Implement the rsync service on the backup server B. A is the client.
1. What is rsync?
Rsync and remote synchronize know that rsync is a software that implements remote synchronization. While Synchronizing files,
You can retain additional information such as the permission, time, and soft/hard link of the original file. Rsync uses the "rsync algorithm" to provide a client and
The Remote File Server provides a fast method for file synchronization, and files can be transmitted through ssh, which ensures the confidentiality.
It is still free software.
Rsync includes the following features:
Update the entire directory and tree and file system;
Optional maintenance of symbolic chains, hard links, files belonging to, permissions, devices, and time;
No special permission requirements are required for installation;
For multiple files, the internal pipeline reduces the latency of file waiting;
You can use rsh, ssh, or direct port as the transfer port;
Supports anonymous rsync file synchronization and is an ideal image tool;
Start deploying rsync Service
1. Confirm to install the rsync package
[Root @ backup ~] # Rpm-qa | grep rsync
Rsync-3.0.6-12.el6.x86_64
2. Configure the rsync configuration file
The rsync configuration file is as follows:/etc/rsyncd. conf
### Rsyncd. conf start ###
Uid = rsync
Gid = rsync
Use chroot = no
Max connections = 200
Timout = 300.
Pid file =/var/run/rsyncd. pid
Lock file =/var/run/rsync. lock
Log file =/var/log/rsyncd. log
[Backup]
Path =/backup/
Ignore errors
Read only = false
List = false
Hosts allow = 10.0.0.0/24
Hosts deny = 0.0.0.0/32
Auth users = rsync_backup
Secrets file =/etc/rsync. password
# Rsync_config ____________________ end ###
3. Add a user Backup Directory
[Root @ backup ~] # Mkdir/backup
[Root @ backup ~] # Useradd rsync-s/sbin/nologin
[Root @ backup ~] # Id rsync
Uid = 502 (rsync) gid = 502 (rsync) groups = 502 (rsync)
[Root @ backup ~] # Chown-R rsync/backup/
[Root @ backup ~] # Ll/backup-ld
Drwxr-xr-x 2 rsync root 4096 Jan 14/backup
4. Start the rsync daemon
[Root @ backup ~] # Rsync -- daemon
[Root @ backup ~] # Netstat-lntup | grep 873
Tcp 0 0 0.0.0.0: 873 0.0.0.0: * LISTEN 1477/rsync
Tcp 0 0: 873: * LISTEN 1477/rsync
5. rsync auto-start
[Root @ backup ~] # Echo "# rsync bak by baizuo 20180120">/etc/rc. local; echo "/usr/bin/rsync -- daemon -- config =/etc/rsyncd. conf ">/etc/rc. local
[Root @ backup ~] # Tail/etc/rc. d/rc. local-n2
# Rsync bak by baizuo 20180120
/Usr/bin/rsync -- daemon
6. Configure the account, password, and account file permissions for rsync synchronization.
(Only virtual users used for rsync Account Verification, not users of the system)
[Root @ backup ~] # Echo "rsync_backup: baizuo">/etc/rsyncd. password
[Root @ backup ~] # Tail/etc/rsync. password
Rsync_backup: baizuo
[Root @ backup ~] # Chmod 600/etc/rsync. password
[Root @ backup ~] # Ll/etc/rsync. password
-Rw ------- 1 root 20 Jan 20 17:06/etc/rsync. password
7. Configure on the client
[Root @ web ~] # Echo "baizuo">/etc/rsync. password
[Root @ web ~] # Chmod 600/etc/rsync. password
[Root @ web ~] # Ll/etc/rsync. password
-Rw ------- 1 root 7 Jan 20 :04/etc/rsync. password
8. Perform a push test on the client.
[Root @ web ~] #/Backup
[Root @ web ~] # Touch/backup/test.txt
[Root @ web ~] # Rsync-avzP/backup/rsync_backup@192.168.0.251: backup/-- password-file =/etc/rsync. password
Sending incremental file list
./
Test.txt
0 100% 0.00kB/s 0:00:00 (xfer #1, to-check = 0/2)
Sent 78 bytes received 30 bytes 216.00 bytes/sec
Total size is 0 speedup is 0.00
A technical point -------------- OK ----------------------------
B. Back up important data on A. In practice, write A backup script.
Back up the configuration file mentioned above.
# Perform a packaging and compression test on the shell. (You can skip the test)
[Root @ web ~] # Mkdir/var/html/www/baizuo-p
[Root @ web ~] # Mkdir/app/logs/baizuo_log-p
[Root @ web ~] # Cd/var/html/www/
[Root @ web www] # tar zcvf baizuo_web _ $ (date ready before f).tar.gz./baizuo
./Baizuo/
[Root @ web www] # ll
Drwxr-xr-x 2 root 4096 Jan 20 17:32 baizuo
-Rw-r -- 1 root 114 Jan 20 baizuo_web_2018-01-20.tar.gz
2. Compile the script and test it.
(This script is not standard. You can improve it .)
[Root @ web www] # mkdir/server/scripts/-p
[Root @ web www] # mv rsync_copyall.sh/server/scripts/
[Root @ web www] # cat/server/scripts/rsync_copyall.sh
# Backup web_site
Cd/var/html & tar zcf/backup/www _ $ (date must have f0000.tar.gz./www
Cd/app/& tar zcf/backup/logs _ $ (date ready before f).tar.gz./logs
# Backup system config
Cd/& tar zcf/backup/etc _ $ (date ready before f).tar.gz./etc
# Backup crond root
Cd/var/spool/cron/& tar zcf/backup/crond_root _ $ (date 1_f1_.tar.gz./root
# Rsync data to bakserver
Rsync-avzP/backup/rsync_backup@192.168.0.251: backup/-- password-file =/etc/rsync. password>/dev/null 2> & 1
# Del data 7 days ago
Find/backup/-type f-name "* .tar.gz"-mtime + 7 | xargs rm-f
3. Test the script and view the result.
The client executes the script.
[Root @ web scripts] # sh rsync_copyall.sh
Service view result
[Root @ backup ~] # Ll/backup/
Total 10276
-Rw-r -- 1 rsync 161 Jan 20 crond_root_2018-01-20.tar.gz
-Rw-r -- 1 rsync 10509606 Jan 20 etc_2018-01-20.tar.gz
-Rw-r -- 1 rsync 136 Jan 20 logs_2018-01-20.tar.gz
-Rw-r -- 1 rsync 0 Jan 20 :07 test.txt
-Rw-r -- 1 rsync 341 Jan 20 www_2018-01-20.tar.gz
PS: we can see that the push is successful. However, there is a problem here: Should files on the server be saved by IP address.
Solution:
Complete script functions:
[Root @ web scripts] # cat rsync_copyall.sh
IP = 'grep IPADDR/etc/sysconfig/network-scripts/ifcfg-eth0 | cut-d' = '-f2'
Mkdir/backup/$ IP
# Backup web_site
Cd/var/html & tar zcf/backup/$ IP/www _ $ (date ready before f).tar.gz./www
Cd/app/& tar zcf/backup/$ IP/logs _ $ (date ready before f).tar.gz./logs
# Backup system config
Cd/& tar zcf/backup/$ IP/etc _ $ (date ready before f).tar.gz./etc
# Backup crond root
Cd/var/spool/cron/& tar zcf/backup/$ IP/crond_root _ $ (date 1_f1_.tar.gz./root
# Rsync data to bakserver
Rsync-avzP/backup/$ IP rsync_backup@192.168.0.251: backup/-- password-file =/etc/rsync. password>/dev/null 2> & 1
# Del data 7 days ago
Find/backup/-type f-name "* .tar.gz"-mtime + 7 | xargs rm-f
Check the result on the backup server:
[Root @ backup] # ls/backup/192.168.0.252/
Crond_root_2018-01-20.tar.gz logs_2018-01-20.tar.gz
Etc_2018-01-20.tar.gz www_2018-01-20.tar.gz
Skill B: __________________________________ complete!
C. Perform a scheduled task and execute the backup script in step B.
(The client performs a scheduled task) backs up data at every day and stores the data for seven days.
[Root @ web scripts] # crontab-l
30 */usr/sbin/ntpdate cn.ntp.org.cn>/dev/null 2> & 1
# Bak local data by baizuo at 2018.1.20
00 00 ***/bin/sh/server/scripts/rsync_copyall.sh>/dev/null 2> & 1
(The server performs a scheduled task) the backup data is saved for six months.
[Root @ backup scripts] # crontab-l
30 */usr/sbin/ntpdate cn.ntp.org.cn>/dev/null 2> & 1
# Keep data 180 days
00 00 ***/bin/sh/server/scripts/keepdata180days. sh>/dev/null 2> & 1Copyright Disclaimer: This article is an original article by the blogger and can be freely reproduced, but please indicate the source. Http://blog.csdn.net/baizuo_1/article/details/79115950