Automatic backup scripts in Linux systems

Source: Internet
Author: User
Tags md5

Requirements: All files in the/data directory are CP to the/var/backups directory, and the file is named on the day's date. For example (/var/backups/data.20151103). And to determine the day before the file in the directory and the difference, if no difference, then delete the previous days of the backup directory.


# Cat Auto_bak.sh
#!/bin/bash

Bak_from_name= "Data"
Bak_from_dir= "/data"
Bak_to_dir_par= "/var/backups"
Bak_dir= "${bak_to_dir_par}${bak_from_dir}.$ (date +%y%m%d)"
Yes_bak_dir= "${bak_to_dir_par}${bak_from_dir}.$ (date-d '-1 day ' +%y%m%d)"
New_sum_file= "${bak_to_dir_par}/new_sum. $bak _from_name.$ (date +%y%m%d)"
Old_sum_file= "${bak_to_dir_par}/old_sum. $bak _from_name.md5"

if [!-D $bak _to_dir_par];then
mkdir $bak _to_dir_par
Fi
Find $bak _from_dir |xargs-i {} md5sum {} >> $new _sum_file 2>/dev/null
Cp-a $bak _from_dir $bak _dir && echo "$bak _from_dir directory to $bak _dir directory backup successfully"

New_sum_error= ' md5sum-c $new _sum_file 2>/dev/null|awk ' $2!~ ' OK ' ' |wc-l '
new_sum_ok= ' md5sum-c $new _sum_file 2>/dev/null|awk ' $2~ ' OK ' ' |wc-l '
Old_sum_error= ' md5sum-c $old _sum_file 2>/dev/null|awk ' $2!~ ' OK ' ' |wc-l '
old_sum_ok= ' md5sum-c $old _sum_file 2>/dev/null|awk ' $2~ ' OK ' ' |wc-l '

if [$new _sum_error = = $old _sum_error-a $new _SUM_OK = = $old _sum_ok];then
echo "Because today and yesterday ' s content is the same, delete the backup content of Yesterday"
RM-RF $yes _bak_dir $new _sum_file
Fi
echo | CP $new _sum_file $old _sum_file &>/dev/null

Variable Description:

Bak_from_name: variable is the name of the directory to be backed up
Bak_from_dir: variable is the directory path to be backed up
BAK_TO_DIR_PAR: variable is the parent (ancestor) directory path of the directory to which you want to back up
Bak_dir: variable is the directory path to which you want to back up
Yes_bak_dir: variable is the backup directory path the day before the directory to be backed up
New_sum_file: variable is the MD5 list of the directories that are computed for the day to be backed up
Old_sum_file: variable is the MD5 list of the directories to be backed up for the day (this variable is current after the execution of the script, and the file specified by the variable is the MD5 list of the previous backup directory the day before the script starts executing)
New_sum_error: variable is the total number of entries that changed for the MD5 of the directory to be backed up today
NEW_SUM_OK: variable is the total number of items that have not changed for the MD5 of the day to back up the directory
Old_sum_error: variable is the total number of entries that have changed to the MD5 of the directory that was to be backed up the day before
OLD_SUM_OK: variable is the total number of items that have not changed MD5 the previous day to back up the directory
Script Description:
The first part of the script is to search for the MD5 value of each file in the directory you want to back up, then log to the MD5 list file for the day's backup directory, and then perform the backup.
The latter part is based on the same entry values for the unchanged and changed portions of the MD5 list file in the previous day's backup directory, depending on the MD5 list file that is being backed up by the current date. To determine whether the files in the directory you want to back up have changed, so that you can decide whether to delete the previous day's backups.

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.