一、使用shell 備份文件
1,/var/stu下有sh與py腳本,使用shell腳本備份到個人目錄下,被省略備份文件列表日誌,以月份分開備份文件。
$ cat backupfile.sh
#!/bin/bash
#
#定義備份源,目標,時間命名備份文件
#
sourcePath="/var/stu"
dateDir=`date +%Y%m`
targetPath="$HOME/$dateDir"
backupName=`date +%Y%m%d`
#
#創建目錄,~/date
if [ ! -e $targetPath ]
then
mkdir $targetPath
fi
#
#備份文件並壓縮,~/date/stu_date.tar.gz
cd $sourcePath && tar -zcvf "$targetPath/stu_$backupName.tar.gz" ./*
#
#記錄備份文件列表,~/date/stu_date.log
ls -Rl >> "$targetPath/stu_$backupName.log"
$ ./backupfile.sh
./python/
./shell/
./shell/backupfile.sh
$ ls -lR /home/damon
/home/damon:
total 0
drwxrwxr-x. 2 damon damon 55 Aug 18 14:50 201508
/home/damon/201508:
total 8
-rw-rw-r--. 1 damon damon 392 Aug 18 15:00 stu_20150818.log
-rw-rw-r--. 1 damon damon 473 Aug 18 15:00 stu_20150818.tar.gz
2,加入日程計劃,定時備份。
$ crontab -e
30 7 * * * /var/stu/shell/backupfile.sh