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