3 backup system files and mail-sent shell script sharing _linux Shell

Source: Internet
Author: User
Tags bz2 flush mkdir rar mysql backup

1. Install Mutt mail Client

# yum Install Mutt-y

2. Create a script

# vi/etc/rc.d/mail_to_bak_daily.sh

Version One:

#!/bin/bash # ========================================================= # Please enter which separate directory you want to put the backup data in basedir=/backup/ Daily # ========================================================= path=/bin:/usr/bin:/sbin:/usr/sbin::/usr/local/
Bin:/usr/local/sbin:/usr/local/mysql/bin; Export path Export LANG=C # define database Information User=root passwd=123456 # define MySQL backup path mysql_db_bak= $basedir/mysql_db_bak/$ (Date +%f_% H%M) [! d ' $mysql _db_bak] && mkdir-p $mysql _db_bak # define the www backup path www_src_bak= $basedir/www_src_bak/$ (Date +%f_%h %M) [! d ' $www _src_bak] && mkdir-p $www _src_bak # define message attachment path mail_attach_mysql= $mysql _db_bak/wordpress.sql Mail _attach_www= $www _src_bak/www.tar.bz2 # define message headers mail_subject= "Www.example.com_daily_backup" # Note: The characters between the headings here cannot have spaces,
If a space is issued according to the name of the message, for example daily@www.example.com,backup@www.example.com This example underline instead of # define the message content Mail_content=/tmp/mail_content.txt Mail_code= "Send Mysql_bak and WWW_SRC to/at $ (date +%f_%h%m)" # define mail address mail_address= "test@example.com" # 1. Backup MySQL (database directory in/usr/local/mysql) mysqldump-u $user-p$passwd--opt--skip-lock-tables--flush-logs--database wordpress > $mysql _db_bak/wordpress.sq  L # 2. Backup www source code: cd/usr/local tar-jpcf $www _src_bak/www.tar.bz2 www # 3. Message body cd/tmp echo $mail _code > $mail _content # 4. Send attachments yum install mutt-y cat $mail _content |mutt-s $mail _subject-a $mail _attach_mysql-a $mail _attach_www--$mail _ad
 Dress # 5. Purge Temporary files cd/tmp rm-rf $mail _content

Version two: recommended

Note: This version will send the attachment separately, to send several large attachments to the mailbox at the same time the probability of not receiving, you can reconfigure the following version

#!/bin/bash Path=/bin:/usr/bin:/sbin:/usr/sbin::/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin; Export path Export LANG=C # System file backup path basedir=/backup/daily # database Information user=root passwd=123456 # define MySQL backup path mysql_db_bak= $bas
 edir/mysql_db_bak/$ (date +%f_%h%m) if [!-d "$mysql _db_bak"]; 
 Then mkdir-p $mysql _db_bak fi # define the www backup path www_src_bak= $basedir/www_src_bak/$ (date +%f_%h%m) if [!-d "$www _src_bak"]; Then mkdir-p $www _src_bak fi # backup files mysql_bak= $mysql _db_bak/wordpress.sql.gz www_bak= $www _src_bak/www.tar.bz2 # define Backup text An array of all_bak= ($mysql _bak $www _bak) # message headers mail_subject= "www.showerlee.com_daily_backup" # Message contents mail_content_mysql= "Send Mysql_bak at $ (date +%f_%h%m)" mail_content_www= "Send www_src at $ (date +%f_%h%m)" # defines an array of content mail_content= ("$mail _con Tent_mysql "" $mail _content_www) # mail address mail_address= "test@example.com" # 1. Backup MySQL (database directory in/usr/local/mysql) mysqldump-u $user-p$passwd--opt--skip-lock-tables--flush-logs--database WordPress | gzip > $mysql _bak # 2. Backupwww source code: cd/usr/local tar-jpcf $www _bak www # 3. Send attachments yum install mutt-y for ((i=0; i!=${#all_bak [@]}; ++i)) {echo ${ma Il_content[i]} |
 Mutt-s $mail _subject $mail _address-a ${all_bak[i]} # 4. Clean mutt temporary files Cat/dev/null > ~/sent

Version three:

Implement RAR split and send attachments
Recommended for users with attachments exceeding 50M

#/bin/bash Path=/bin:/usr/bin:/sbin:/usr/sbin::/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin; Export path Export LANG=C # System file backup path basedir=/data/backup/daily # database Information user=root passwd=123456 # install rar rar=$ (rar | grep R AR | awk ' {print $} ' |
  Sed-n ' 1p ') if [$!= 0];then rar=null fi sys_version=$ (uname-m) rar_path= "/usr/local/rar" if [$rar!= ' rar '];then if [$sys _version = = ' x86_64 '];then wget http://www.rarlab.com/rar/rarlinux-x64-4.2.0.tar.gz tar zxvf rarlinux- x64-4.2.0.tar.gz mv RAR $rar _path CD $rar _path make else wget http://www.rarlab.com/rar/rarlinux-4.2.0.t Ar.gz tar zxvf rarlinux-4.2.0.tar.gz mv rar $rar _path CD $rar _path make fi fi # define MySQL backup path Mysql_db_bak
= $basedir/mysql_db_bak/$ (date +%f_%h%m) if [!-d "$mysql _db_bak"]; 
Then mkdir-p $mysql _db_bak fi # define the www backup path www_src_bak= $basedir/www_src_bak/$ (date +%f_%h%m) if [!-d "$www _src_bak"]; Then mkdir-p $www _src_bak fi # backup files mysql_bak= $mysql _db_bak/test.sql.gz
www_bak= $www _src_bak/www.rar www_bak_part1= $www _src_bak/www.part1.rar www_bak_part2= $www _src_bak/www.part2.rar www_bak_part3= $www _src_bak/www.part3.rar # define Attachment array all_bak= ($mysql _bak $www _bak_part1 $www _bak_part2 $www _bak_part3) # Message header mail_subject= "showerlee.com_and_yanwenbo.cn_daily_backup" # message content mail_content_mysql= "Send Mysql_bak at $ (date +%f _%h%m) "mail_content_www_p1=" Send Www_src rarball part1 at $ (date +%f_%h%m) "mail_content_www_p2=" Send Www_src rarball pa Rt2 at $ (date +%f_%h%m) ' mail_content_www_p3= ' send Www_src rarball part3 at $ (date +%f_%h%m) "# defines an array of content mail_content= (" $mai L_content_mysql "" "$mail _content_www_p1" "$mail _content_www_p2" "$mail _content_www_p3") # Mail address mail_address1= " Showerlee@vip.qq.com "mail_address2=" kevinywb@vip.qq.com "# 1. Backup MySQL mysqldump-u $user-p$passwd--opt--skip-lock-tables--flush-logs--database Test | gzip > $mysql _bak # 2. Back up the WWW source code (split several 45m of tar packets) if [-Z $rar];then exit 0 fi cd/data rar a-v45000k $www _bak www # 3 . Send attachment for (i=0; i!=${#All_bak[@]}; ++i)) ({if [F ${all_bak[i]}]; then echo ${mail_content[i]} | mutt-s $mail _subject $mail _address1 $mail _address2-a $ {All_bak[i]} else echo ${mail_content[i]} | Mutt-s $mail _subject $mail _address1 $mail _address2-a $www _bak break fi} # 4. Clean mutt temporary Files cat/dev/null > ~/sent # 5. Clean up backup Files 1 months ago Dir=/data/backup/daily/find $dir-mtime +30-type d-name "201*"-print |xargs-i rm-rf {} 2>/dev/null #
 6. Release system cache echo 1 >/proc/sys/vm/drop_caches

Note: If you report the following error

Copy Code code as follows:

Postdrop:warning:uid=0:file too large
sendmail:fatal:root (0): Message File too big
Error sending message, CHI LD exited (Deferred.).
Could not send the message.


is due to exceeding the system default send attachment size, modified to the following size can be (2G)

# postconf-e "Message_size_limit = 2048000000"
# postconf-e "mailbox_size_limit = 2048000000"

3. Execute script

# sh/etc/rc.d/mail_to_bak_daily.sh

Note: You can use tail-f/var/log/maillog to view send logs
4. Test results as shown:
Note: Large attachment Please wait patiently

Related Article

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.