Linux programming-11. ShellScript (bash) -- (3) automatically backed up ShellScript

Source: Internet
Author: User
Article title: Linux programming-11. ShellScript (bash) -- (3) automatically backed up ShellScript. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
A Shell Script for automatic backup
As we mentioned earlier, you can use Shell Script with crond for regular work. The job of coding is to use crond in combination with UNIX.
  
--------------------------------------------------------------------------------
  
First, we will study how to back up the system.
To back up the system, we need to use some compression tools. In many UNIX systems, tar and gzip are de facto data exchange standards. We often see some tar.gz or tgz files, which are called tarball. Of course, you can also use bzip2, zip, and other compression tools for compression, not limited to gzip. However, tar and gzip are the most common and convenient methods.
  
To compress and back up the desired data, you can combine tar and gzip. There are many methods, and the most common commands are as follows:
  
Tar-c file/dir... | gzip-9> xxxx.tar.gz
  
You can also do the following separately:
  
Tar-r file/dir...-f xxxx.tar
Gzip-9 xxxx.tar
  
Or
  
Tar-r file/dir...-f xxxx.tar
Gzip-9 <xxxx.tar> xxxx.tar.gz
--------------------------------------------------------------------------------
  
After understanding the basic knowledge of Archive backup in Linux, let's write a Script to back up the archive.
#! /Bin/sh
# Filename: backup
  
DIRS = "/etc/var/your_directories_or_files"
BACKUP = "/tmp/backup. tgz"
  
Tar-c $ DIRS | gzip-9> $ BACKUP
  
DIRS stores the files and directories you want to back up, and BACKUP stores the files you want to back up. Do not put/tmp in DIRS. in this way, you are backing up the disk and may plug up your hard disk.
--------------------------------------------------------------------------------
  
Next, test
[Foxman @ foxman bash] # chmod 755 backup
[Foxman @ foxman bash] #./backup
  
After the execution is complete, a backup. tgz will be created in/tmp, which stores your important information. You are available
  
Gzip-dc/tmp/backup. tgz | tar-vt
Or
Tar vtfz/tmp/backup. tgz
  
Let's take a look at the file list.
  
The following commands can be used to restore a data source:
  
Gzip-dc/tmp/backup. tgz | tar-xv
Or
Tar xvfz/tmp/backup. tgz
  
Backup is usually the most important part of the backup system./etc can be said to be an indispensable part. In addition, you need to back up the important information in your system. Generally, you do not need to back up the directory of/bin,/sbin,/usr/bin,/usr/sbin,/usr/X11R6/bin, and so on. You just need to back up your important files. don't back up the entire hard disk. it's a pretty cool operation.
--------------------------------------------------------------------------------
  
If you have many machines, you can use one of the internal network hosts with lighter tasks as the primary backup host. All machines are automatically backed up, and then the backup data is stored in the network file systems such as NFS, Coda, and Samba on the backup machine. the backup data is regularly collected, then, you can back up the data on the machine again.
Here is an illustration of the entire system backup solution.
  
Before you proceed, let's take a look at the ones in the system that are to be backed up and those that are not needed.
--------------------------------------------------------------------------------
  
New backup
#! /Bin/sh
HOSTNAME = 'hostname'
DIRS = "/etc/var/your_important_directory"
BACKUP = "/tmp/$ HOSTNAME. tgz"
NFS = "/mnt/nfs"
  
Tar-c $ DIRS | gzip-9> $ BACKUP
Mv-f $ BACKUP $ NFS
--------------------------------------------------------------------------------
  
Script in the backup host: collect_backup
#! /Bin/sh
NFS = "/mnt/nfs"
BACKUP = "/backup"
  
Mv-f $ NFS/*. tgz $ BACKUP
  
Here, you cannot directly store all backups in/mnt/nfs, which is dangerous. If all/mnt/nfs content is accidentally deleted on any machine, the backup will disappear. Therefore, you need to move/mnt/nfs to a directory that is accessible only to the backup host.
--------------------------------------------------------------------------------
  
When these individual scripts are tested, we will put them in crontab. Locate your crontab, which may be in/var/spool/cron/crontabs/root,/etc/crontab,/var/cron/tabs/root.
In the crontab, select one of the following (depending on your scheduled time ):
  
Slackware:/var/spool/cron/crontabs/root
01 *****/full_backup_script_path/backup 1>/dev/null 2>/dev/null # hourly (too much effort)
30 16 ***/full_backup_script_path/backup 1>/dev/null 2>/dev/null # every day, backup before work
30 16 ** 0/full_backup_script_path/backup 1>/dev/null 2>/dev/null # every Monday
0 5 1 **/full_backup_script_path/backup 1>/dev/null 2>/dev/null # every month
RedHat/Debian:/etc/crontab
RedHat can directly put backup into/etc/cron. hourly,/etc/cron. daily,/etc/cron. weekly,/etc/cron. monthly. Or add/etc/crontab as follows:
For crontab usage, see "man 5 crontab", which is not described here.
  
The backup host settings are similar.
  
Note: do not back up all machines at the same time, otherwise the network will be congested. The backup time on the backup host must be set to the last, otherwise the backup data will not be received. After implementation, you can adjust the time interval.
--------------------------------------------------------------------------------
  
Look, two Shell scripts with less than three lines are used with the cron timing tool. It can simplify the manual backup work that originally took more than one hour to less than 10 minutes. Make good use of your imagination and make more changes, so that you can make your life easy, abnormal, and happy.
  
  

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.