Tar Backup System

Source: Internet
Author: User
Tags add numbers
I. Overview

A few days ago, when I tried to tease Tom (Tomcat) via SSH, centos suddenly went down on the server. After grub is started, use the CD Linux rescue to fix the problem and prompt that the LINUX partition cannot be found. If you want to mount the hard disk backup system, the system prompts that the hard disk cannot be mounted. There is no way to reinstall the system. After a while, I decided to regularly back up the system.

When will backup be performed?

Every night

Where is the backup?

Obviously, it is not wise to back up data to the disk where the system is located. you should back up data to an external hard disk or tape.
To make the article look simpler, the example in this article is not backed up to an external device, but under the/backup directory.

What is backup?

Backing up the entire system requires a lot of disk space, so I decided to back up all the system on Sunday and back up important data at other times.

Back up the entire system. Therefore, you need to consider directories that do not need to be backed up:
/Backup
/Proc
/Lost + found
/Sys
/Mnt
/Media
/Dev
/Tmp

Backup Data:
/Home (You know)
/Work (my working directory)
/OPT (software installation directory)
/Var/lib/MySQL (Mysql Data File directory)
In addition, some important configuration files need to be maintained:
/Etc/profile (various environment variables)
/Etc/bashrc (various environment variables)
/Etc/crontab (set scheduled backup)
/Etc/fstab (storing file system information. In fact, my backup files are stored on other disks, and the disk mounting is set here)

Backup Mode

Tar supports three backup methods:
1. Full backup: Back up all specified target files or folders
2. Incremental Backup: only backing up different files or folders of atime and last backup
3. Differential backup: similar to Incremental backup, it only adds support for Windows file systems.
Select full backup here

When to delete it?

Two weeks for system backup data and one week for Data Backup

2. Basic Knowledge atime, ctime, and mtime

UNIX systems divide time into three types:
Atime (access time): the time when the file was last accessed. Ls-lu View
Ctime (Status Change Time): The time when the object property or content was last modified. Ls-LC View
Mtime (Modified Time): The last modification time of the file content. Ls-l view
Note that the Linux File System never stores the file creation time.

A script is provided here to facilitate viewing of various times.

#! /Bin/bash # usage: Script Name file if [$ #! = 1]; then Echo "the input parameter is incorrect. You need to enter a file" Exit 1 Export Ile = $ 1if [! -F "$ file"]; then Echo "$ file does not exist" else echo "ctime (Status Change Time ): $ (LS-LC $ file | awk '{print $6, $7, $8}') "Echo" atime (access time ): $ (LS-lu $ file | awk '{print $6, $7, $8}') "Echo" mtime (Modified Time ): $ (LS-L $ file | awk '{print $6, $7, $8}') "fi
Use cron for Scheduled Backup

Creating a Cron scheduled task is very simple. You can do the following:
Method 1:
$ crontab -e
Method 2:
# vim /etc/crontab
Method 1 can only run scheduled tasks with the permissions of the current user. method 2 can specify the user to which the program is executed, but the file must be edited as root.
To back up the entire file system, we only use/etc/crontab as an example:
# M h Dom mon Dow USER command
0 0 *** root/work/mybin/backup. Sh

This line of configuration indicates that the script/work/mybin/backup. Sh is executed at every day.
If this setting is invalid, You need to repeat the configuration file and restart the service by using the service crond reload and service crond restart commands.

Iii. Script
#! /Bin/bash # Back up all the system data on Sunday, and back up some data at other times. Data = "/work/home/opt/var/lib/MySQL/etc/profile/etc/bashrc/etc/crontab/etc/fstab" data_vali = 7 # data file validity period sys_vali = 14 # System File validity period set $ (date) if test "$1" = "sun "; thentar-czvpf "/backup/recovery" -- exclude =/proc -- exclude =/lost + found -- exclude =/sys -- exclude =/mnt -- exclude =/media -- exclude =/ dev -- exclude =/tmp/else tar-czpvf "/backup/data_backup_00006-00002-00003.tar.gz" $ datafifind/backup-type F-name "Data *"-mtime + $ data_vali-exec Rm-RF {}\; find/backup-type F-name "system *"-mtime + $ sys_vali-exec Rm-RF {}\;

Two points are described here.

Data time format problems:

My Linux environment is Chinese. The result of executing the date command in Shell terminal or shell script is as follows:
Tuesday, May 21, 2013 10:13:56 CST
However, when the crontab periodically executes shell scripts, the result is as follows:
Tue May 21 10:13:56 CST 2013
Therefore, if it is Sunday, "If test" $1 "=" sun "; then" is used"

Find and mtime

Find can be searched by time. Atime, ctime, and mtime have been mentioned above. Here we describe the numbers added after-mtime.
1. directly add numbers
Indicates the number of days from the current time. It should be noted that this time is not based on the date, it is based on the current time (May 21, 2013 13:19:10) Forward 24 hours to 0. that is, 0 indicates the files modified within 24 hours, 1 indicates the files modified within 24-48 hours, and 2 indicates the files modified within 48-72 hours.
2. + numbers
Indicates a modified file other than a few days. + 1 indicates a dayExternalModified files (more than 48 hours from now), + 2 indicates files modified two days away (more than 72 hours from now)
3.-Number
Indicates a modified file within a few days.-1 indicatesInternalModified files (<24 hours from now),-2 indicates files modified within 2 days (<48 hours from now)

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.