1.申請GMail
2.安裝軟體(Postfix、Mutt、Rar)
選擇Rar的原因是只用Tar加split哪怕是傳輸中+-1Byte,就完了,rar有的加入還原資訊的選項,比較保險!!!。
Ps.執行rar指令若出現下行訊息
rar: /lib/tls/i686/cmov/libc.so.6: version `GLIBC_2.4' not found (required by rar)
表示需要GLIBC_2.4。如果沒有GLIBC_2.4可以使用rar_static
cp rar_static /usr/local/bin/rar
3.Script
#!/bin/bash
#backup to local and mail
#Author: Brandon Chen
#Email: parkghost@gmail.com
src_path=/var/www/html
dst_path=/var/www/alumni/backup/
bak_file="alumni"`date +%Y%m%d%H%M`
bak_data_recovery=15%
bak_split_size=19m
bak_mail_to=*****@gmail.com
db_user=*****
db_name=*****
db_password=*****
if ! test -d $dst_path"/mail"
then
mkdir -p $dst_path"/mail"
fi
cd $dst_path
mysqldump -u$db_user -p$db_password $db_name > $bak_file".sql"
tar --exclude .svn -zcf $bak_file".tar.gz" $src_path
cd mail
rm -rf * #remove old files
rar a -rr$bak_data_recovery -m5 -v$bak_split_size $bak_file".rar" ../$bak_file".tar.gz" > /dev/null
rar a -rr$bak_data_recovery -m5 $bak_file".sql.rar" ../$bak_file".sql" > /dev/null
for name in `ls`
do
mutt -s $bak_file -a $name $bak_mail_to < /dev/null
done
4.修改Postfix的最大寄送郵件大小
[root@alumni ~]# vi /etc/postfix/main.cf
message_size_limit = 40960000 #增加此行
5.加入系統排程
[root@alumni ~]# crontab -e
0 0 15,30 * * /var/www/alumni/scripts/backup.sh