There is a Web server in a company, the data is very important, but if the hard disk is spent, the data will be lost, now the leader requires you to make the data on the other servers to do a periodic scheduled backup, the requirements are as follows:
The backup Site Directory is packaged on Web server A every night at 00 o'clock and pushed to Server B by the rsync command (the backup idea can be packaged by date and then pushed to the backup server.) )
The specific requirements are as follows
1 , the backup directory for Web server A and backup Server B must both be/backup
2 , Web Server Site Directory assumptions (/var/www/html)
3 , the Web server is only locally backed up within 7 days
4 , check that the backup results are normal on the backup server, and send the daily backup results to the Administrator mailbox
5 , 6 of data per week on the backup server is retained, and the other backups are kept for only 180 days backup
Customer Service side
CD/&&\
ip= ' Ifconfig eth1|awk-f "[:]+" ' Nr==2{print $4} '
mkdir/backup/$ip-P
Tar zcfh/backup/$ip/bak_$ (date+%f_%w). tar.gz var/www/html app/logs Var/spool/cron/root
Etc/rc.local
&&\
find/backup/$ip-type f-name "*$ (date +%f_%w). tar.gz" |xargs md5sum >/backup/$ip/flag_$ (date+%f_%w).
TXT &&\
rsync-av/backup/$ip [Email Protected]::backup/--password-file=/etc/rsync.password
find/backup/$ip-type f-mtime +7-name "*.tar.gz" |xargs rm-fr
CD/&&\
The first command switches to the parent directory
ip= ' Ifconfig eth1|awk-f "[:]+" ' Nr==2{print $4} '
mkdir/backup/$ip-P
  Because we are simulating 50 ip as directory go out of the ip numeric variable is ip create with ip named directory
Tar zcfh/backup/$ip/bak_$ (date +%f_%w). tar.gzvar/www/html app/logs Var/spool/cron/root etc/rc.local
Package The data we want to back up and package it in the previous step to create the IP under directory because you want to delete the backup server the days before the backup data
, in order to differentiate which day the data is backed up, the package name is date+%f date named, the topic also required to retain every Saturday so added %w print out weeks to facilitate later operation
Rsync-av/backup/[email protected]::backup/--password-file=/etc/rsync.password
push the packaged data to the server to make rsync into a password-free mode
find/backup/$ip-type f-name "*$ (date+%f_%w) tar.gz" |xargs md5sum >/backup/$ip/flag_$ (date +%f_%w)
find the data packaged for the day and attach md5sum , at the same time the generated md5sum value is saved to flag_$ (date +%f_%w) compare to the server md5sum value with
find/backup/$ip-type f-mtime +7-name "*.tar.gz" |xargs rm-fr
Because the current directory only uses backup data for seven days, deleting data from seven days ago
Service side
lang=en
flag_num=/tmp/check_$ (date +%f). txt
find/backup/-type f-name "flag_$ (date +%f_%w). txt" |xargs md5sum-c|grep failed&> $flag _num
If [' Cat $flag _num|wc-l '-gt 0];then
Mail-s "$ (date +%f\%T) backup isfail!!" [Email protected] < $flag _num
Else
echo "Backup is OK" |mail-s "$ (date +%f\%T) Backup is successful" [email protected]
Fi
#find/backup/172.16.1.31/-type f-mtime-7 \! -name "bak*_6.tar.gz" |xargs rm-fr
lang=en
Tune the character set into English
flag_num=/tmp/check_$ (date +%f). txt
make a variable to /tmp/check_$ (date+%f). txt variable into Flag_num
find/backup/-type f-name "flag_$ (date+%f_%w). txt" |xargs md5sum-c|grep FAILED &> $flag _numif [' CAT$FLAG_NUM|WC- L '-GT 0];then
Compare client-packagedmd5sumvalues such as the service-sidemd5sumThere is no change,no change will be output asFAILEDand then usegrepfilter out the contents of the file is empty,md5sumif the value of the file does not change, there is content., the final view of the contents of the file, is empty is not a problem, if there is a content to represent the failure of the package
Mail-s "$ (date +%f\%T) backup is fail!!" [Email protected]< $flag _numelse
If there is content in it will send the wrong message to the set mailbox,
echo "Backup is OK" |mail-s "$ (date +%f\%T) backup issuccessful" [email protected]
Fi
If no content is sent to the set mailbox, the OK Information
find/backup/172.16.1.31/-type f-mtime +180 \! -name "bak*_6.tar.gz" |xargs rm-fr
Delete According to the final request of the topic the days before the data but the Saturday permanent save
To send an e-mail to your own mailbox, you must /etc/mail.rc configuration file Add content
Set [email protected] smtp=smtp.163.comsmtp-auth-user=15855157334 smtp-auth-password=cai1234 s
Mtp-auth=login
Rsync full-network backup enterprise case