一直以来都是单纯的使用 rsync 备份 wordpress 应用程序,但是并没有检查文件的完整性,总觉得这种方式有点不太安全,于是搞了一个小程序即备份文件并检查文件的完整性,分享给大家,有要改进的地方,希望提出来。
#!/bin/bash remote_host=192.168.1.27 #备份服务器ip remote_path=/backup #备份服务器的备份目录 local_backup_path=/backup local_file_path=/usr/html [ -d $local_backup_path ] || mkdir -p $local_backup_path cd ${local_file_path} && \ tar zcf $local_backup_path/www_tianfeiyu-$(date +%F).tar.gz htdocs && \ tar zcf $local_backup_path/www_17linux8-$(date +%F).tar.gz linux8 && \ find /backup -type f -name "*.tar.gz" | xargs md5sum > $local_backup_path/flag_$(date +%F) #copy file rsync -avz $local_backup_path/* $remote_host:$remote_path #del backup file find $local_backup_path -type f -name "*.tar.gz" -mtime +7 | xargs rm -f
上面的程序运行在服务器端,将所要备份的文件夹打包并进行校验后,将校验码和文件一同发送到备份服务器上。可以根据自己的备份策略做定时任务,我是每天备份一次的。
0 4 * * * /bin/bash /root/shell/www_backup.sh &> /dev/null
以下是备份服务器上需要运行的程序:
#!/bin/bash local_backup_path=/backup md5_file=flag_$(date +%F) cd $local_backup_path if [ $? -eq 0 ];then if [ -e $md5_file ];then md5sum -c ${md5_file} >> mail.txt if [ $? -eq 0 ];then mail -s "Success ! The backup task is ok !" xxxxxx@qq.com < mail.txt else mail -s "Failed ! The backup task is failed !" xxxxxx@qq.com < mail.txt fi else ls > mail.txt mail -s "Failed ! The md5_file is not exists!" xxxxxx@qq.com < mail.txt fi
此程序检查校验文件并根据校验文件来检查数据的完整性。此脚本需要在备份脚本执行后再执行:
0 5 * * * /bin/bash /root/shell/flag_check.sh &> /dev/null