In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use rsync to back up the Linux system". In the daily operation, I believe many people have doubts about how to use rsync to back up the Linux system. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubts of "how to use rsync to back up the Linux system". Next, please follow the editor to study!
Rsync command
Andrew Tridgell and Paul Mackerras wrote rsync, which was released in 1996. Its goal is to synchronize files to another computer. Did you notice why they chose that name (remotely synchronize)? It is open source software available in most distributions.
Rsync can be used to synchronize two directories or directory trees, whether they are on the same computer or on different computers, and not only that, it can do more. It creates or updates exactly the same directory as the source directory. The new directories are not packaged and stored in tar or zip, but ordinary directories and files that can be easily accessed by common Linux tools, which is exactly what I need.
One of the most important features of rsync is the way it handles existing files where the source directory has been modified. Instead of copying the entire file from the source, it uses chunk checking to compare the source file with the target file. If all blocks of the two files have the same checksum, there is no need to transfer data. Otherwise, only the changed blocks are transmitted. This saves a lot of time and bandwidth consumed by remote synchronization. For example, it takes three hours for me to use the rsync script to back up all my hosts to a large external usb hard drive because all the data needs to be transferred. The next backup takes only 3 to 8 minutes, depending on how many files have been created and changed since the last backup. I use the time command to record the actual time spent. Last night, it only took me three minutes to back up about 750 Gb data from six remote systems and local workstations. In fact, only a few hundred Mb data that changes during the day need to be backed up.
The following command can be used to synchronize the contents of two directories and any of their subdirectories. That is, after the contents of the new directory are synchronized with the source directory, their contents are exactly the same.
Rsync-aH sourcedir targetdir
The-an option represents the archive mode, which maintains permissions, all relationships, and symbolic (soft) links. The-H option is used to maintain hard links. Note that both the source and destination directories can be on the remote host.
Suppose we synchronized two directories using rsync yesterday. We want to synchronize again today, but we deleted some files from the source directory. Rsync only copies new and changed files to the new directory by default, without changing the files we deleted in the new directory, but if you want those files that have been deleted in the source directory to be deleted in the new directory, you can add the-delete option to delete.
Another interesting option, and my personal favorite, is link-dest, because it greatly increases the power and flexibility of rsync. -- link-dest makes daily backups cost very little extra space and very short time.
Use this option to specify the previous day's backup directory and today's backup directory, and then rsync will create today's new backup directory and create a hard link to each file in the yesterday's backup directory. Now we have a bunch of hard links to yesterday's backup in today's backup directory. The file was not created repeatedly, but some hard links were created. For hard links, there is a very detailed description in Wikipedia. After creating today's backup with the hard link of yesterday's backup directory file, rsync backs up as usual, if a change is detected in the file, it will not make a hard link, but copy a copy of a file from yesterday's backup directory, and then copy the changed part of the source file. (LCTT translation note: the original text is suspected to be unclear here. See the try_dests_reg function of generator.c. Copy or hard link is selected according to match_level first, instead of judging match_level after creating hard link.)
Now our orders look like this.
Rsync-aH-delete-link-dest=yesterdaystargetdir sourcedir todaystargetdir
You may also want to exclude directories or files that you don't want to back up. Then you can use the-- exclude option. Use this option plus the mode in which you want to exclude files or directories. You can use the following new command to exclude the browser's cache.
Rsync-aH-delete-exclude Cache-link-dest=yesterdaystargetdir sourcedir todaystargetdir
Note: the mode of each file you want to exclude needs to be preceded by the-- exclude option.
Rsync can synchronize remote hosts, either as the source or destination of synchronization. As another example, let's assume that you want to synchronize the directory of a remote host named remote1 to your local location. Because ssh is the default protocol for exchanging data with remote hosts, I always use the ssh option. Now the command looks like this.
Rsync-aH-e ssh-delete-exclude Cache-link-dest=yesterdaystargetdir remote1:sourcedir todaystargetdir
This is the final version of my rsync backup command.
You can customize your synchronization process by relying on a number of options available in rsync. For the most part, the simple commands I have just described are sufficient for my personal needs. You can read rsync's rich documentation to learn about its other capabilities.
Deploy backup
My backup runs automatically because-everything can be automated. I wrote a BASH script that uses rsync to create daily backups. This includes ensuring that the backup media is mounted, generating the name of the daily backup directory, and creating an appropriate directory structure in the backup media, * performing a real backup and then uninstalling the backup media.
I use cron to execute scripts every morning to make sure I never forget to back up.
My script rsbu and configuration file rsbu.conf are available on GitHub.
Recovery test
All backup plans that have not been tested are incomplete. You can test to restore a file or an entire directory to ensure that the backup works as usual and can be used to recover after all data is lost. I have seen too many backups fail for a variety of reasons, and valuable data is lost due to problems ignored by lack of testing.
Select a file to restore to a test directory such as / tmp, so that you will not overwrite any files that have been updated after the backup. Verify that the contents of the file are what you expected. Restoring a file backed up with rsync is as simple as finding your backup file and copying it to where you want to restore it.
I have had to recover my personal files several times, occasionally the entire directory. Most of them accidentally deleted files or directories. Several times it was because the hard drive crashed. These backups will come in handy sooner or later.
* one step
But simply creating backups won't save your business. You need to create backups regularly so that the most recent backup is stored on another remote machine, if possible, in another building or a few miles away. This ensures that a large-scale disaster does not destroy all your backups.
A reasonable choice for small businesses is to do daily backups on removable media, bring the backups home at night, and bring earlier backups to the office the next morning. You'll have several copies in turn. You can even take the backup to the bank and put it in your safe deposit box, and then bring back the previous backup.
At this point, the study on "how to use rsync to back up the Linux system" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.