Menu Home

阿里云服务器安装后-网站自动备份

网站服务器为了安全的需要,配置CRON进行每日的备份任务.

1.编写自动备份网站的脚本.包括了备份网站文件及数据库文件,删除30天以前的备份数据.

tianyangtax@iZ25tc8mln7Z:~$ sudo vim /usr/sbin/site-backup-script
tianyangtax@iZ25tc8mln7Z:~$ sudo cat /usr/sbin/site-backup-script
#!/bin/sh                                                                                                                                   
rq=`date +%Y%m%d`
tar zcf /mnt/backup/www.tianyangtax.com/site/SiteFile-$rq.tar.gz /mnt/wwwroot/edusoho
mysqldump -u dbuser -pdbpwd edusoho | gzip > /mnt/backup/www.tianyangtax.com/mysql/MySql-$rq.gz
find /mnt/backup/www.tianyangtax.com/site/ -mtime +30 -name "*.tar.gz" -exec rm -rf {} \;
find /mnt/backup/www.tianyangtax.com/mysql/ -mtime +30 -name "*.gz" -exec rm -rf {} \;
tianyangtax@iZ25tc8mln7Z:~$ sudo chmod +x /usr/sbin/site-backup-script

2.添加CRON任务,每日04:27执行备份操作

tianyangtax@iZ25tc8mln7Z:~$ sudo vim /etc/cron.d/site-backup-cron
tianyangtax@iZ25tc8mln7Z:~$ sudo cat /etc/cron.d/site-backup-cron
27  4   *   *   *   root    /usr/sbin/site-backup-script

3.打开ubuntu的cron日志功能,可以看到我们的备份脚本正常执行了.

tianyangtax@iZ25tc8mln7Z:~$ sudo vim /etc/rsyslog.d/50-default.conf
tianyangtax@iZ25tc8mln7Z:~$ sudo cat /etc/rsyslog.d/50-default.conf
...
cron.*              /var/log/cron.log
...
tianyangtax@iZ25tc8mln7Z:~$ sudo service rsyslog restart
rsyslog stop/waiting
rsyslog start/running, process 3130
tianyangtax@iZ25tc8mln7Z:~$ sudo service cron restart
cron stop/waiting
cron start/running, process 3145
tianyangtax@iZ25tc8mln7Z:~$ sudo tail /var/log/cron.log
...
Jan 22 04:27:01 iZ25tc8mln7Z CRON[6231]: (root) CMD (/usr/sbin/site-backup-script)
...

Categories: ubuntu

Tagged as:

RoyAkon