Use SHELL scripts to automatically back up Linux systems

Source: Internet
Author: User

Some time ago, I wrote a python script to automatically back up system files. Today I will write a shell script to incrementally back up system files. By the way, I will review shell script-related commands, this script can back up different files or folders as needed for complete backup and Incremental backup.

Use Python automated backup systems and websites

The script is as follows:
#! /Bin/sh
# Automatic Backup Linux System Files
# Author wugk 2013-11-22
# Define Variable
SOURCE_DIR = (
$ *
)
TARGET_DIR =/data/backup/
YEAR = 'date + % y'
MONTH = 'date + % m'
DAY = 'date + % d'
WEEK = 'date + % U'
FILES = system_backup.tgz
CODE = $?
If
[-Z "$ *"]; then
Echo-e "Please Enter Your Backup Files or Directories \ n -------------------------------------------- \ nExample $0/boot/etc ......"
Exit
Fi
# Determine Whether the Target Directory Exists
If
[! -D $ TARGET_DIR/$ YEAR/$ MONTH/$ DAY]; then
Mkdir-p $ TARGET_DIR/$ YEAR/$ MONTH/$ DAY
Echo "This $ TARGET_DIR is Created Successfully! "
Fi
# EXEC Full_Backup Function Command
Full_Backup ()
{
If
["$ WEEK"-eq "7"]; then
Rm-rf $ TARGET_DIR/snapshot
Cd $ TARGET_DIR/$ YEAR/$ MONTH/$ DAY; tar-g $ TARGET_DIR/snapshot-czvf $ FILES 'echo $ {SOURCE_DIR [@]}'
["$ CODE" = "0"] & echo-e "-------------------------------------------- \ nThese Full_Backup System Files Backup Successfully! "
Fi
}
# Perform incremental BACKUP Function Command
Add_Backup ()
{
Cd $ TARGET_DIR/$ YEAR/$ MONTH/$ DAY;
If
[-F $ TARGET_DIR/$ YEAR/$ MONTH/$ DAY/$ FILES]; then
Read-p "These $ FILES Already Exists, overwrite confirmation yes or no? : "SURE
If [$ SURE = "no"-o $ SURE = "n"]; then
Sleep 1; exit 0
Fi
# Add_Backup Files System
If
[$ WEEK-ne "7"]; then
Cd $ TARGET_DIR/$ YEAR/$ MONTH/$ DAY; tar-g $ TARGET_DIR/snapshot-czvf $ _ $ FILES 'echo $ {SOURCE_DIR [@]}'
["$ CODE" = "0"] & echo-e "----------------------------------------- \ nThese Add_Backup System Files Backup Successfully! "
Fi
Else
If
[$ WEEK-ne "7"]; then
Cd $ TARGET_DIR/$ YEAR/$ MONTH/$ DAY; tar-g $ TARGET_DIR/snapshot-czvf $ FILES 'echo $ {SOURCE_DIR [@]}'
["$ CODE" = "0"] & echo-e "------------------------------------------- \ nThese Add_Backup System Files Backup Successfully! "
Fi
Fi
}
Full_Backup; Add_Backup

The above scripts are for reference only, and there are many areas for optimization, improvement, and adjustment. You are welcome to give your comments.

Automatic backup script for Linux shell Version 2

Linux/Unix shell scripts call SQL and RMAN scripts

Passing variables between Linux/Unix shell SQL statements

Linux Command-sleep-delay specified time

Shell programming in Linux -- basic usage of sed command

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.