VPS自动备份网站数据库至其它主机的脚本

Posted by Harid六月 - 4 - 2011 Leave comments

这是我自己VPS上用来定期备份网站数据库(将网站数减至1个后)的脚本。因为我是让它每天凌晨2点备份,所以定义为让它备份后在本地保留3天内的备份,在远程保留5天内的备份,备份后写本地logs目录下的文件并发送邮件给我。

脚本内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
##Program: This script will be executed at a known time to create a database duplicate and upload it to another host.
#Contact:http://www.timepoet.com
#
##
# 定义数据库的信息,其中Name为数据库名,Username为数据库用户名,Password为密码 #
Name=ninthday_sql
Username=ninthday_sql
Password=ninthday_passwd
# 定义本地备份路径 #
Localdir=/home/chroot/home/ninthday
# 定义本地备份的临时名称 #
LBackname=backup-blog-$(date +%Y%m%d).sql
# 定义本地三天前备份的名称 #
LOldbackup=backup-blog-$(date -d -3day +%Y%m%d).sql.tar.gz
# 定义远程备份名称 #
RBackname=backup-blog-$(date +%Y%m%d).sql.tar.gz
# 定义远程5天前备份名称 #
ROldbackup=backup-blog-$(date -d -5day +%Y%m%d).sql.tar.gz
# 定义远端FTP信息 #
FTPHost=216.18.217.23
FTPName=ninthday_ftp
FTPPasswd=ninthday_passwd
# 定义发送和接收通知邮件的地址(须在不同的域) #
sender=root@timepoet.com
receiver=****@126.com
# 开始创建本地文件 #
/usr/local/mysql/binmysqldump -u$Username -p$Password $Name > $Localdir/backups/$LBackname
# 压缩至本地 #
tar zPcf $Localdir/backups/$RBackname $Localdir/backups/$LBackname
#  上传数据 #
lftp $FTPHost -u $FTPName,$FTPPasswd << EOF
cd backups
mput $Localdir/backups/$RBackname
mrm $ROldbackup
bye
EOF
# 删除本地多余及过期数据 #
rm -f $Localdir/backups/$LBackname
if [ -e $Localdir/backups/$LOldbackup ] ; then
    rm -f $Localdir/backups/$LOldbackup
fi
# 写日志文件 #
echo "A datebase duplicate has been created and uploaded to the host on $(date +%F)." >> $Localdir/logs/dbbackup.log
# 发送通知邮件 #
sendmail -t <<EOF
From: Host <$sender>
To:  $receiver
Subject: Database succeeded in creating backup.
----------------------------------------------------------
The VPS has succeeded in creating and uploading database duplicate.
It holds 5 database duplicates created on the latest 5 sequential days
on remote host and 3 database's backups on localhost(/backups/).
Please download the backup files to your computer on a regular time schedule.
Don't reply this email!
Contact: harid@timepoet.com
http://www.timepoet.com
----------------------------------------------------------
EOF
exit 0

 

   声明:本文采用 BY-NC-SA 协议进行授权 | 星期九
   原创文章转载请注明:转自《VPS自动备份网站数据库至其它主机的脚本

    • @C瓜哥, 是在VPSYOU上买的,我买的是180MB内存的,一个月63,一年的话你算喽。如果你有意买的话,强烈建议买512MB内存的,180MB内存根本不够 用,很容易溢出。

      • @Harid,
        想来想去,最后还是决定买国内的虚拟主机,
        天朝打击国外的太严重了,我想求个稳定速度快

          • @Harid,
            想不到吧,我最后还是用的小张的美国主机了
            备案实在太麻烦了,成功率还低。我看到tanky woo的论坛也在这上面。每天几百IP的网站都跑得动,我也就不担心了

          • @C瓜哥, 小张的国内的服务器远没有国外服务器好,不过问题的关键是放国内不会抽风,在国外间歇性访问不了。

  1. 我是直接以附件的形式备份到我的邮箱里,这样比较保险,http://www.kwxiaozhu.tk/Articles/cpanel.html


分享按钮