CentOS定时备份文件

由于工作需要,写了一个备份的脚本,定时执行,步骤如下:
1.写shell脚本--bakup.sh

#!/bin/bash
#对svn目录进行备份,并记录日志
DAY=`date +%Y%m%d`
cp -rf /home/tools/svn /home/tools/svnbak/svn-${DAY}
echo "bakup finished !--$DAY" >> /home/tools/svnbak/log.txt

2.放入crontab

crontab -e
30 23 * * * /home/tools/svnbak/backup.sh

说明:crontab -e 命令使用的是vi编辑器。

crontab -l  查看已存在的定时任务

crontab -e  取消当前定时任务

3.如果要加入开机启动,有如下几种方式:

  1).把命令加入/etc/rc.d/rc.local

  

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/test/bakup.sh

  2).把脚本拷贝到/etc/rc.d/init.d/中,然后用chkconfig设置为开机启动

chkconfig --add test  //添加到列表
chkconfig test on     //开机启动

备份保留7天内的资料

#!/bin/bash
#对svn目录进行备份
DAY=`date +%Y%m%d`
DAY2=`date -d '7 days ago' +%Y%m%d`
cp -rf /home/tools/svn /home/tools/svnbak/svn-${DAY}
rm -rf /home/tools/svnbak/svn-${DAY2}
echo "bakup finished !--$DAY,delete $DAY2" >> /home/tools/svnbak/log.txt
原文地址:https://www.cnblogs.com/baby-bear/p/4123079.html