Skip to main content

VPS自动异地备份脚本 | VPS Automated FTP Backup Script

Please scoll down for English version.

VPS现在越来越流行,很多朋友都有了自己的私人服务器;但是另一个问题也随之而来,廉价的海外VPS服务通常没有备份功能,一旦出现故障,数据便付诸东流。这和虚拟空间的模式完全不同,虚拟空间的供应商通常会将用户的数据安全放在首位。
Martin现在正在使用VPS内存很紧张,只有128MB基本内存和256MB Burst内存(当然因此也便宜),跑Dropbox备份简直是个笑话,还没动起来就内存溢出了。因此采用了另一套节省资源的方案,tar打包、FTP异地存储、corn自动执行。将以下代码保存为beifen.sh并上传至/root/,在命令行输入chmod +x beifen.sh使其可以运行。

 #!/bin/bash
 today=`date +%Y%m%d`
 ftphost="FTP Address"
 user="FTP username"
 pw="FTP Password"
 backupdir="backup from path"
 zippedfile="backup to path"
 ftpdir="FTP upload path"
 #make a package of your www data
 tar -P -zcf $zippedfile/backup$today.tgz $backupdir
 #enter to the backup path
 cd $zippedfile
 #login to the FTP and process
 ftp -n $ftphost <<EOF
 user $user $pw
 cd $ftpdir
 put backup$today.tgz
 quit
 EOF

具体配置说明:
ftphost="FTP Address" FTP地址可以为IP或域名,例如ftp.echojoy.com205.185.120.225
user="FTP Username" 登录FTP的用户名
pw="FTP Password" 登录FTP的密码
backupdir="backup from path" 你需要备份的文件夹,可以是多个文件夹,用空格区分,例如www目录、MySQL数据库以及Apache配置文件/var/www /usr/local/mysql/var /usr/local/apache/conf,这些目录的位置在不同的Linux系统中不尽相同,请确认你要备份的目录
zippedfile="backup to path" 压缩包的存储路径,如/root/myserverbackup
ftpdir="FTP upload path" 登录FTP后上传文件的路径,如/httpfiles,请确认你使用的FTP服务器没有单个文件大小限制

增加自动备份功能:
编辑/etc/corntab文件,在最后加入
0 2 * * 0 root /root/beifen.sh
此代码表示“0分 2点 忽略 忽略 每周第一天(周日) 以root用户 运行/root/beifen.sh
在命令行运行/etc/init.d/crond restart重启计划任务程序,系统就会执行自动每周的备份啦。

Martin已经在CentOS + Nginx平台上实验通过,可以正常备份。


The stability of cheap VPS is always questionable. Regular backup (better to be remote backup) becomes a must for low-end users (just like me).

The VPS I am using now shipped with only 128MB ram and another 128MB swap,  it is too luxury to ran a Dropbox demon for remote backup. Instead, I scriblled following script with TAR, FTP and Corn to backup your web.

Please paste following codes to beifen.sh and upload to /root/,and then type chmod +x beifen.sh make it executable

 #!/bin/bash
 today=`date +%Y%m%d`
 ftphost="FTP Address"
 user="FTP username"
 pw="FTP Password"
 backupdir="backup from path"
 zippedfile="backup to path"
 ftpdir="FTP upload path"
 #make a package of your www data
 tar -P -zcf $zippedfile/backup$today.tgz $backupdir
 #enter to the backup path
 cd $zippedfile
 #login to the FTP and process
 ftp -n $ftphost <<EOF
 user $user $pw
 cd $ftpdir
 put backup$today.tgz
 quit
 EOF

Description:
ftphost="FTP Address" FTP Address could be either domain or IP, eg ftp.yourdomain.com or 172.120.10.9
user="FTP Username" FTP Username
pw="FTP Password" FTP Password
backupdir="backup from path" you can use space to seprate multiple folders, eg /var/www /usr/local/mysql/var /usr/local/apache/conf , please confirm your own backup from paths.
zippedfile="backup to path" where to store your local zipped backup file eg/root/myserverbackup
ftpdir="FTP upload path" path to sotre your remote zipped backup file eg/httpfiles

How to backup on a schedule:
Type corntab -e then add the following line as the new line.
0 2 * * 0 root /root/beifen.sh
Which means “0min 2o’clock ignore ignore first day of the week(which is sunday) run as root execute/root/beifen.sh

Save the editing  file and Corntab would update the scheduled programme. You will get your web backup automatically weekly.

Tested on CentOS and Debian.

Admin

Creativist 创意先锋 a.k.a 码钉

发表评论

电子邮件地址不会被公开。 必填项已用*标注