Linux program - 11.SHELL Script (Bash) - (3) Shell script for automatic backup

xiaoxiao2021-03-06  95

A shell script for automatic backup we agreed that you can use the shell script with crones to perform regular work. To make a regular work, on UNIX is the use of crones. -------------------------------------------------- ----------------------------- First let's take to study how to back up the system. To back up the system, it is nothing more than some compression tools. On many UNIX systems, TAR and GZIP are DE FACTO data exchange criteria. We can often see some tar.gz or tgz files, these files, called Tarball. Of course, you can also compress the compression tool with BZIP2, ZIP, etc., does not have to be limited to GZIP. But TAR cooperates with GZIP is the most common and most convenient way. To compress the information we want, back up, you can carry out Tar and Gzip. There are many ways, the most commonly used instructions are the following: TAR-C File / Dir ... | Gzip -9> xxxx.tar.gz You can also do it: Tar -R file / dir ... -f xxxx.tar gzip -9 xxxx.tar or tar -r file / dir ... -f xxxx.tar gzip -9 xxxx.tar.gz ----------- -------------------------------------------------- ------------------ After the basic knowledge of the file backup of Linux, we will write a script backup of the file. #! / bin / sh # filename: Backup Dirs = "/ etc / var / your_directories_or_files" backup = "/ tmp / backup.tgz" TAR-C $ DIRS | GZIP -9> $ backup where DIRS is put down Archives and catalogs, Backup is your backup file. Don't put the / TMP in DIRS, do it, you are a backup of your backup, you may explode your hard disc. -------------------------------------------------- ------------------------------ Next test [Foxman @ Foxman Bash] # chmod 755 Backup [Foxman @ Foxman Bash] # ./backup will have a backup.tgz in / TMP after execution of execution, and your important information is stored. You can use gzip -dc /tmp/backup.tgz | tar -vt or tar vtfz /tmp/backup.tgz to see the list of files. To unlock, you can use the following instructions to complete the recovery: 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 It is an indispensable part. In addition, seeing important information in your system requires backup. In general, you don't have to back up / bin, / sbin, / usr / bin, / usr / sbin, / usr / x11r6 / bin, etc. These executive directory. As long as you back up your important file, don't back up the entire hard drive, that is a lot of motion.

-------------------------------------------------- ------------------------------ If you have many machines, you can take advantage of one of the lighter internal network hosts. , As a main backup host. Automatically perform backups all machines, then use NFS / CODA / Samba such as NFS / CODA / Samba, put the backup information in this backup machine, then charge backup data, then you will make a backup in the machine. . Here is the illustration of the entire system backup scheme. Before you do, let's take it, those in the system are backup, those who 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: Collect_Backup #! / Bin / sh nfs = "/ mnt / nfs" backup = "/ backup" MV in the backup host -f $ nfs / *. TGZ $ backup here, you cannot put all backups directly / mnt / NFS, which is dangerous. In case any machine accidentally deletes / mnt / nfs all content, then the backup will disappear. Therefore, you need to move / MNT / NFS to a directory where the backup host is accessible. -------------------------------------------------- ------------------------------ When these individual scrips are tested, we will put them in crontab. Find your crontab, its location may be on / var / spool / cron / crontabs / root, / etc / crontab, / var / cron / tabs / root. Select one of the following to join (see you regular time): slonware: / var / spool / cron / crontabs / root 01 * * * * / full_backup_script_path / backup 1> / dev / null 2> / dev / null # Hourly (too fire a little) 30 16 * * * / full_backup_script_path / backup 1> / dev / null 2> / dev / null # 1:30 per day, backup 30 16 * * 0 / full_backup_script_path / backup 1> / DEV / NULL 2> / dev / null # 6:30 on Monday 16:30 0 5 1 * * / full_backup_script_path / backup 1> / dev / null 2> / dev / null # monthly No. 5: 0 redhat / debian: / etc / crontab redhat can put backup in /etc/cron.Hourly, /etc/cron.daily, /etc/cron.monthly.

转载请注明原文地址:https://www.9cbs.com/read-122152.html

New Post(0)