Commit 53cfa2b7366f139795843e1e07b3c56f5ca809ab

Authored by zhaojianjiang
1 parent e416e5d2
Exists in master

fix bug

Showing 1 changed file with 106 additions and 106 deletions   Show diff stats
backup_srv.sh
1   - #!/bin/bash
2   -#backup files srv
3   -
4   -backup_date=$(date "+%Y%m%d")
5   -backup_data_dir="/data/backup" #此目录必须和上面客户端的远程目录为同一个目录,并且rsync用户对current和flag目录有写权限
6   -important_data_dir="$backup_data_dir/important" #所有历史备份都放在这里面
7   -backup_log_file="${important_data_dir}/${backup_date}/backup.log"
8   -
9   -rmfile_date="60" #删除多少天前的备份数据
10   -
11   -mkdir -p ${important_data_dir}/${backup_date}
12   -chmod 700 ${important_data_dir} #最小化权限
13   -
14   -
15   -fun_backsrv(){
16   -
17   -
18   -echo "$(date +%F_%T) start backup"
19   -echo
20   -
21   -for dir in $(ls $backup_data_dir/current); do
22   - ls $backup_data_dir/flag | grep "$backup_date" | grep -q "$dir"
23   - if [[ $? -eq 0 ]];then
24   - cp -a $backup_data_dir/current/$dir ${important_data_dir}/${backup_date}/${dir}_${backup_date}
25   - cd ${important_data_dir}/${backup_date}
26   - tar zcf ${dir}_${backup_date}.tar.gz ${dir}_${backup_date}
27   - /bin/rm -rf ${dir}_${backup_date}
28   - echo "${dir}_${backup_date} backup ok"
29   - else
30   - echo "${dir}_${backup_date} backup error" #current里面有该主机目录,但是flag里面没有该主机,表明同步失败
31   - fi
32   -done
33   -
34   -echo
35   -du -sh ${important_data_dir}/${backup_date}/*
36   -echo
37   -
38   -current_total_file=$(ls $backup_data_dir/current | wc -l)
39   -date_total_file=$(ls ${important_data_dir}/${backup_date}/*.gz | wc -l)
40   -echo "current total file: $current_total_file"
41   -echo "backup total file: $date_total_file"
42   -echo
43   -
44   -if [[ $current_total_file -eq $date_total_file ]]; then
45   - echo "backup successful!!!"
46   -else
47   - echo "backup error"
48   -fi
49   -
50   -echo
51   -find ${important_data_dir} -maxdepth 1 -mtime +$rmfile_date -exec /bin/rm -rf {} \;
52   -[[ $? -eq 0 ]] && echo "rm before $rmfile_date file ok"
53   -find $backup_data_dir/flag -maxdepth 1 -mtime +$rmfile_date -exec /bin/rm -rf {} \;
54   -[[ $? -eq 0 ]] && echo "rm before $rmfile_date flag ok"
55   -
56   -echo "$(date +%F_%T) stop backup"
57   -
58   -}
59   -
60   -fun_sendmail(){
61   -
62   -#下面的变量中,如果没有就留空即可
63   -to_contact="a@b.com" #这里是收件人地址
64   -copy_contact="" #这里是抄送人地址
65   -from_address="c@b.com" #这里是发件人地址
66   -from_passwd="aaa" #这里是发件人密码
67   -smtp_srv="smtp.ym.163.com"
68   -subject="${backup_date} backup report" #标题
69   -content="${backup_log_file}" #如果是将文件内容当做邮件内容,这里是文件的路径,单独一句话使用"-m"选项
70   -send_file="${backup_log_file}" #如果有附件要发送,这里是附件位置,测试50MB以内可以成功
71   -MaxCount=5 #最大发送尝试次数
72   -
73   -echo
74   -echo "$(date +%F_%T) start sendEmail"
75   -while [[ $SendCount -le $MaxCount ]]; do
76   - /bin/sendEmail \
77   - -f $from_address \
78   - -t $to_contact \
79   - -cc $copy_contact \
80   - -s $smtp_srv \
81   - -u $subject \
82   - -a $send_file \
83   - -o message-file=$content \
84   - -o message-content-type=auto \
85   - -o message-charset=utf8 \
86   - -xu $from_address \
87   - -xp $from_passwd
88   -
89   - SendStatus=$?
90   - let SendCount=SendCount+1
91   -
92   - if [ $SendStatus -eq 0 ]; then
93   - break
94   - fi
95   -done
96   -
97   -echo "SendStatus: $SendStatus"
98   -echo "SendCount: $SendCount"
99   -
100   -echo "subject: $subject"
101   -echo "$(date +%F_%T) stop sendEmail"
102   -echo
103   -
104   -}
105   -
106   -fun_backsrv &>> $backup_log_file
  1 + #!/bin/bash
  2 +#backup files srv
  3 +
  4 +backup_date=$(date "+%Y%m%d")
  5 +backup_data_dir="/data/backup" #此目录必须和上面客户端的远程目录为同一个目录,并且rsync用户对current和flag目录有写权限
  6 +important_data_dir="$backup_data_dir/important" #所有历史备份都放在这里面
  7 +backup_log_file="${important_data_dir}/${backup_date}/backup.log"
  8 +
  9 +rmfile_date="60" #删除多少天前的备份数据
  10 +
  11 +mkdir -p ${important_data_dir}/${backup_date}
  12 +chmod 700 ${important_data_dir} #最小化权限
  13 +
  14 +
  15 +fun_backsrv(){
  16 +
  17 +
  18 +echo "$(date +%F_%T) start backup"
  19 +echo
  20 +
  21 +for dir in $(ls $backup_data_dir/current); do
  22 + ls $backup_data_dir/flag | grep "$backup_date" | grep -q "$dir"
  23 + if [[ $? -eq 0 ]];then
  24 + cp -a $backup_data_dir/current/$dir ${important_data_dir}/${backup_date}/${dir}_${backup_date}
  25 + cd ${important_data_dir}/${backup_date}
  26 + tar zcf ${dir}_${backup_date}.tar.gz ${dir}_${backup_date}
  27 + /bin/rm -rf ${dir}_${backup_date}
  28 + echo "${dir}_${backup_date} backup ok"
  29 + else
  30 + echo "${dir}_${backup_date} backup error" #current里面有该主机目录,但是flag里面没有该主机,表明同步失败
  31 + fi
  32 +done
  33 +
  34 +echo
  35 +du -sh ${important_data_dir}/${backup_date}/*
  36 +echo
  37 +
  38 +current_total_file=$(ls $backup_data_dir/current | wc -l)
  39 +date_total_file=$(ls ${important_data_dir}/${backup_date}/*.gz | wc -l)
  40 +echo "current total file: $current_total_file"
  41 +echo "backup total file: $date_total_file"
  42 +echo
  43 +
  44 +if [[ $current_total_file -eq $date_total_file ]]; then
  45 + echo "backup successful!!!"
  46 +else
  47 + echo "backup error"
  48 +fi
  49 +
  50 +echo
  51 +find ${important_data_dir} -maxdepth 1 -mtime +$rmfile_date -exec /bin/rm -rf {} \;
  52 +[[ $? -eq 0 ]] && echo "rm before $rmfile_date file ok"
  53 +find $backup_data_dir/flag -maxdepth 1 -mtime +$rmfile_date -exec /bin/rm -rf {} \;
  54 +[[ $? -eq 0 ]] && echo "rm before $rmfile_date flag ok"
  55 +
  56 +echo "$(date +%F_%T) stop backup"
  57 +
  58 +}
  59 +
  60 +fun_sendmail(){
  61 +
  62 +#下面的变量中,如果没有就留空即可
  63 +to_contact="a@b.com" #这里是收件人地址
  64 +copy_contact="" #这里是抄送人地址
  65 +from_address="c@b.com" #这里是发件人地址
  66 +from_passwd="aaa" #这里是发件人密码
  67 +smtp_srv="smtp.ym.163.com"
  68 +subject="${backup_date} backup report" #标题
  69 +content="${backup_log_file}" #如果是将文件内容当做邮件内容,这里是文件的路径,单独一句话使用"-m"选项
  70 +send_file="${backup_log_file}" #如果有附件要发送,这里是附件位置,测试50MB以内可以成功
  71 +MaxCount=5 #最大发送尝试次数
  72 +
  73 +echo
  74 +echo "$(date +%F_%T) start sendEmail"
  75 +while [[ $SendCount -le $MaxCount ]]; do
  76 + /bin/sendEmail \
  77 + -f $from_address \
  78 + -t $to_contact \
  79 + -cc $copy_contact \
  80 + -s $smtp_srv \
  81 + -u $subject \
  82 + -a $send_file \
  83 + -o message-file=$content \
  84 + -o message-content-type=auto \
  85 + -o message-charset=utf8 \
  86 + -xu $from_address \
  87 + -xp $from_passwd
  88 +
  89 + SendStatus=$?
  90 + let SendCount=SendCount+1
  91 +
  92 + if [ $SendStatus -eq 0 ]; then
  93 + break
  94 + fi
  95 +done
  96 +
  97 +echo "SendStatus: $SendStatus"
  98 +echo "SendCount: $SendCount"
  99 +
  100 +echo "subject: $subject"
  101 +echo "$(date +%F_%T) stop sendEmail"
  102 +echo
  103 +
  104 +}
  105 +
  106 +fun_backsrv &>> $backup_log_file
107 107 fun_sendmail &>> /var/log/sendEmail.log
108 108 \ No newline at end of file
... ...