定时备份项目操作实例

2019-09-30  本文已影响0人  _不想翻身的咸鱼

题目

企业全网备份项目说明

  1. 所有服务器的备份目录必须都为/backup
  2. 要备份的系统配置文件包括但不限于:
    a) 定时任务服务的配置文件 (/var/spool/cron/root) (适合web和nfs服务器)。
    b) 开机自启动的配置文件 (/etc/rc.local) (适合web和nfs服务器)。
    c) 日常脚本的目录 (/server/scripts)。
    d) 防火墙iptables的配置文件 (/etc/sysconfig/iptables-config)。
    e) 自己思考下还有什么需要备份呢?
  3. Web服务器站点目录假定为 (/var/html/www)。
  4. Web服务器访问日志路径假定为 (/app/logs)
  5. 服务器保留打包后的7天的备份数据即可(本地留存不能多于7天,因为太多硬盘会满)
  6. 备份服务器上,不仅保留最近7天的备份数据,同时保留6个月内; 6个月之外每周一的所有数据副本保留
  7. 备份服务器上,要按照备份数据服务器的内网IP为目录保存备份,备份的文件按照时间名字保存。
  8. 需要确保备份的数据尽量完整正确,在备份服务器上对备份的数据进行检查,把备份的成功及失败结果信息发给系统管理员邮箱中。

服务器

web01 10.0.0.7
backup 10.0.0.41

服务端(backup服务器)

[root@backup ~]# yum install -y rsync
[root@backup ~]# id rsync
uid=1004(rsync) gid=1004(rsync) groups=1004(rsync)
[root@backup ~]# mkdir /backup
[root@backup ~]# chown rsync.rsync /backup/
[root@backup ~]# ll /backup/ -d
drwxr-xr-x. 2 rsync rsync 6 Jun 14 23:21 /backup/
[root@backup ~]# cat /etc/rsync.password
rsync_backup:oldboy123
[root@backup ~]# ll /etc/rsync.password 
-rw-------. 1 root root 23 Jun  8 02:33 /etc/rsync.password
uid = rsync                         
gid = rsync                          
port = 873                           
fake super = yes                                     
use chroot = no                      
max connections = 200                
timeout = 300                           
pid file = /var/run/rsyncd.pid       
lock file = /var/run/rsync.lock      
log file = /var/log/rsyncd.log       
ignore errors                        
read only = false                    
list = false                         
hosts allow = 172.16.1.7       
auth users = rsync_backup            
secrets file = /etc/rsync.password   
[backup_all]                             
path = /backup/
[root@backup ~]# systemctl restart rsyncd #重启服务
[root@backup ~]# systemctl enable  rsyncd  #设置开机自启动
#检查是否成功
[root@backup ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
   Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-06-15 00:18:16 CST; 1min 11s ago
 Main PID: 7844 (rsync)
   CGroup: /system.slice/rsyncd.service
           └─7844 /usr/bin/rsync --daemon --no-detach

Jun 15 00:18:16 backup systemd[1]: Stopped fast remote file copy program daemon.
Jun 15 00:18:16 backup systemd[1]: Started fast remote file copy program daemon.

客户端(web01服务器)

[root@web01 backup]# echo 'oldboy123' > /etc/rsync.password
[root@web01 backup]# cat /etc/rsync.password 
oldboy123
[root@web01 backup]# ll /etc/rsync.password 
-rw-r--r--. 1 root root 10 Sep  9 15:46 /etc/rsync.password
[root@web01 backup]# chmod 600  /etc/rsync.password
[root@web01 backup]# rsync /etc/hostname rsync_backup@172.16.1.41::backup_all --password-file=/etc/rsync.password

[root@backup ~]# ll /backup/
total 4
-rw-r--r--. 1 rsync rsync 6 Jun 15 00:31 hostname
[root@backup ~]# cat /backup/hostname 
web01

以前部分已经把两个服务器打通,接下来就是编辑两个服务器的脚本

客户端(web01)测试命令

[root@web01 ~]# mkdir /backup
[root@web01 backup]# ifconfig eth0| awk -F' ' 'NR==2{print $2}'
10.0.0.7
[root@web01 backup]# ip a s eth0 | awk -F '[ /]+' 'NR==3{print $3}'
10.0.0.7
[root@web01 backup]# IP=`ip a s eth0 | awk -F '[ /]+' 'NR==3{print $3}'`
[root@web01 backup]# echo $IP
10.0.0.7

/var/spool/cron/root

/etc/rc.local

/server/scripts

/etc/sysconfig/iptables-config

/var/html/www

/app/logs

[root@web01 backup]# tar zcvfh /backup/`date +%F-%w`_all_data.tar.gz /var/spool/cron/root /etc/rc.local /server/scripts /etc/sysconfig/iptables-config /var/html/www /app/logs

[root@web01 /]# tree /backup/
/backup/
└── 2019-09-09-1_all_data.tar.gz
└── 2019-09-10-2_all_data.tar.gz

1 directory, 2 files

[root@web01 /]# find /backup/ -type f -name "*`date +%F-%w`*.tar.gz"|xargs md5sum >/backup/finger.txt
[root@web01 /]# cat /backup/finger.txt 
1b5dfd4bf240e744a658b1c50fc6d375  /backup/2019-09-09-1_all_data.tar.gz
[root@web01 /]# find /backup -type f -name "*.tar.gz"  -mtime +7 -delete 
[root@web01 /]# touch /server/scripts/backup_log.sh
[root@web01 /]# cat /server/scripts/backup_log.sh 
#!/bin/bash
source /etc/profile

#var
IP=`ip a s eth0 | awk -F '[ /]+' 'NR==3{print $3}'`
backup_path="/backup"

#mkdir file
mkdir $backup_path/$IP/ -p

#tar
cd / && tar zcfh $backup_path/$IP/`date +%F-%w`_all_data.tar.gz ./var/spool/cron/root ./etc/rc.local ./server/scripts ./etc/sysconfig/iptables-config ./var/html/www ./app/logs
sleep 2

#md5sum
find $backup_path -type f -name "*`date +%F-%w`*.tar.gz"|xargs md5sum >$backup_path/$IP/finger.txt

#rsync data
rsync -az $backup_path/  rsync_backup@172.16.1.41::backup_all --password-file=/etc/rsync.password

#delete
find $backup_path -type f -name "*.tar.gz"  -mtime +7 -delete
[root@backup /]# md5sum -c /backup/10.0.0.7/finger.txt 
/backup/10.0.0.7/2019-09-12-4_all_data.tar.gz: OK
[root@backup ~]# ll /backup/10.0.0.7/
total 32
-rw-r--r--. 1 rsync rsync 2356 Feb  4  2018 2018-02-04-0_all_data.tar.gz
-rw-r--r--. 1 rsync rsync 2356 Feb  1 18:23 2019-02-01-5_all_data.tar.gz
-rw-r--r--. 1 rsync rsync 2356 Feb  4 18:23 2019-02-04-1_all_data.tar.gz
-rw-r--r--. 1 rsync rsync 2356 Mar  1 18:23 2019-03-01-5_all_data.tar.gz
-rw-r--r--. 1 rsync rsync 2356 Sep  1  2019 2019-09-01-0_all_data.tar.gz
-rw-r--r--. 1 rsync rsync 2356 Sep  9  2019 2019-09-09-1_all_data.tar.gz
-rw-r--r--. 1 rsync rsync 2356 Sep 12  2019 2019-09-12-4_all_data.tar.gz
-rw-r--r--. 1 rsync rsync   80 Feb  4  2018 finger.txt

[root@backup ~]# find /backup -type f -name "*.tar.gz" -mtime +180 ! -name "*-1_.tar.gz"
/backup/10.0.0.7/2019-03-01-5_all_data.tar.gz
/backup/10.0.0.7/2019-02-01-5_all_data.tar.gz
/backup/10.0.0.7/2019-02-04-1_all_data.tar.gz
/backup/10.0.0.7/2018-02-04-0_all_data.tar.gz
vi /etc/mail.rc 
set from=XXXXXXX@qq.com                
set smtp=smtp.qq.com                     
set smtp-auth-user=XXXXXXX@qq.com      
set smtp-auth-password=pmdwqabguenabdja  
set smtp-auth=login   

[root@backup ~]# systemctl restart postfix.service
[root@backup ~]# systemctl enable postfix.service 
[root@backup ~]# systemctl status postfix 
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2019-09-09 19:10:09 CST; 58s ago
 Main PID: 8570 (master)
   CGroup: /system.slice/postfix.service
           ├─8570 /usr/libexec/postfix/master -w
           ├─8571 pickup -l -t unix -u
           └─8572 qmgr -l -t unix -u

Sep 09 19:10:08 backup systemd[1]: Starting Postfix Mail Transport Agent...
Sep 09 19:10:09 backup postfix/master[8570]: daemon started -- version 2.10.1, configuration /etc/postfix
Sep 09 19:10:09 backup systemd[1]: Started Postfix Mail Transport Agent.
[root@backup /]# touch  /server/scripts/web.sh
#!/bin/bash

backup_path="/backup"
mail="XXXXXXX@qq.com"

#md5sum
find $backup_path -type f -name 'finger.txt' |xargs md5sum -c > /tmp/mail.txt 2>/dev/null

#send mail info      code pmdwqabguenabdja
mail -s "每日备份信息 $(date -d '-1 day' +%F)" $mail </tmp/mail.txt

#delete +7 algo
find $backup_path -type f -name "*.tar.gz" -mtime +180 ! -name "*-1_.tar.gz" -delete

[root@backup /]# crontab -l
#!/bin/bash

#daily backup
00 02 * * * /bin/sh /server/scripts/web.sh > /dev/null 2>&1
[root@web01 /]# crontab -l
#!/bin/bash

#daily backup
00 01 * * * /bin/sh /server/scripts/backup_log.sh >/dev/null 2>&1

需要注意的一些问题:

问题:脚本中设置ip变量,运行脚本ok,定时任务不简析变量.

原因:crontab与环境变量

不要假定cron知道所需要的特殊环境,它其实并不知道。
当手动执行脚本OK,但是crontab死活不执行时。这时必须大胆怀疑是环境变量惹的祸。

第一种办法:把所有系统变量全部加载一下

!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
export PATH
第二种办法:通过source命令引入环境变量

#!/bin/bash
source /etc/profile
上一篇下一篇

猜你喜欢

热点阅读