Linux regularly automatically backs up folder scripts

Source: Internet
Author: User

According to the online code, this script is divided into two files, one is to execute the file backup.sh, and the other is to back up the project file Backup.txt. Backup.txt This file is better understood, is a text file, each line of it represents a project, that is, you need to back up the directory under the file or directory.
The code is as follows:

#!/bin/bash
#author: Rekfan
#date: 2013-01-11
#作用: Backing up files regularly
#website:http://blog.rekfan.com
##################################################
# Important Variable Name effect
# SourceDir directory to be backed up
# LogFile log file
# BakDir1 back up the upper directory
# BAKDIR2 Backup Directory
# Retainday Backup archive file retention days
# projecttxt Backup project file configuration file
##############★The following information needs to be configured★##################
Date= ' date + '%y-%m-%d '
Sourcedir=/var/web
Bakdir1=/web_bak
bakdir2=/web_bak/' date + '%y-%m-%d '
Logfile=/web_bak/log_ ' date + '%y-%m-%d '. log
Retainday=20
Projecttxt=/web_bak/backup.txt
##############★ Creating a backup directory ★##################
Mkdir-p $BakDir 2/
echo "Backup start at $ (date +"%y-%m-%d%h:%m:%s ")" > $LogFile
echo "--------------------------------------------------" >> $LogFile
echo "" >> $LogFile
echo "Create backup directory" $BakDir 2 "Success" >> $LogFile
CD $BakDir 2
##############★ Backup Core Code ★##################
projectlist= ' cat $ProjectTxt '
for Project in $ProjectList
do
  Projectdata= $SourceDir/$Project
 destdir= $BakDir 2/$Project
 packfile= $Project-$DATE. tar.gz
 &NBSP
 if [-F $BakDir 2/$PackFile]
 then
  echo "backup file has exist!" >> $LogFi Le
 else
cd $SourceDir
  TAR-ZCVF $BakDir 2/$PackFile $Project >/dev/null
  echo "directory or file $ sourcedir/$Project has been backed up to $BakDir 2/$PackFile ">> $LogFile
 fi
####### #######★   delete $retainday days ago   ★##############
 oldfile= "$BakDir 1/" $ (date--date= "$ Retainday days ago "+"%y-%m-%d ")
 if [-F $OldFile]
 then
  RM-RF $OldFile >/dev/null
&NB Sp echo "[$OldFile],delete old File success!" >> $LogFile
 fi

echo "" >> $LogFile
Done
echo "--------------------------------------------------" >> $LogFile
echo "Backup end at $ (date +"%y-%m-%d%h:%m:%s ")" >> $LogFile
echo "----------By:rekfan" >> $LogFile
echo "" >> $LogFile
Exit 0

Linux regularly automatically backs up folder scripts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.