在Linux(Linux)中备份数据有多种选择。您可以使用一些免费提供的软件程序,例如fwbackups和Sbackup。但是,有一种无需安装任何额外软件即可备份目录的简单方法。
我们将使用变量、tar命令和date命令创建一个 shell 脚本,以创建一个目录及其子目录的日期备份文件。
shell 脚本本质上是一个文件,其中包含按顺序运行的命令列表。如果您有一系列定期按顺序运行的命令,那么创建一个包含这些命令的 shell 脚本会很有帮助。然后,您只需运行脚本文件即可运行命令。
创建 Shell 脚本文件
对于此示例,我们将创建一个 shell 脚本来备份包含用户指南文件的目录。我们在Ubuntu中使用(Ubuntu)Gnome环境。
首先,通过从Places菜单中选择Home Folder来访问您的主目录。(Home Folder)文件浏览器(File Browser)打开您的主目录。
我们将创建一个新的空文件,我们将在其中输入执行备份的命令。在右窗格中右键单击(Right-click)并选择Create Document | Empty File 弹出菜单中的空文件。
一个文件被添加到列表中并准备好被重命名。输入(Type)文件的名称,为文件提供扩展名.sh。
对于此示例,我们将文件命名为user_guide_backups.sh。
现在我们需要将命令添加到文件中。右键单击文件名,然后从弹出菜单中选择使用 gedit 打开。(Open with gedit)
该文件在gedit(gedit)中打开。在文件中输入以下行,然后单击Save。下面列出了每行的用途。
注意:(NOTE:)您还可以复制以下文本并将其粘贴到gedit中。请务必将<username>更改为您的用户名。
#!/bin/bash
SRCDIR="/home/<username>/Documents/my_work/"
DESTDIR="/home/<username>/Backups/"
FILENAME=ug-$(date +%-Y%-m%-d)-$(date +%-T).tgz
tar – create – gzip – file=$DESTDIR$FILENAME $SRCDIR
逐行说明
下表描述了 shell 脚本文件中的每一行。
Line # |
Description |
1 |
This line must be the first line in a bash shell script, which is the default type of script. |
2 |
This line sets up a variable called SRCDIR and sets the value of it to the directory to be backed up.
NOTE: Be sure to replace <username> with your username. |
3 |
This line sets up a variable called DESTDIR and sets the value of it to the directory into which the backup file will be written.
NOTE: Be sure to replace <username> with your username. |
4 |
This line sets up a variable called FILENAME and sets the value using text and variables containing the date command to add the current date and time to the filename. For example, the filename might be ug-20100212-13:03:45.tgz.
NOTE: When using a variable, always start it with the dollar sign ($). If you use a command as part of a variable, enclose the command and the options for the command in parentheses. |
5 |
This line is the tar command with the following function and options added.
–create |
This function creates a new archive (or truncates an old one if the filename specified already exists) and writes the named files or directory to it. |
–gzip |
This option tells tar to compress the files in the archive using the gzip utility. |
–file |
This option gives tar the filename to use. In this case, we assembled the filename using the DESTDIR, FILENAME, and SRCDIR variables. |
|
编辑Shell 脚本文件(Shell Script File)的权限(Permissions)
在运行脚本之前,您需要确保文件具有正确的权限。为此,如上所述再次打开您的主文件夹,然后右键单击 shell 脚本文件。(Home Folder)从弹出菜单中选择属性。(Properties)
显示属性(Properties)对话框。确保(Make)选中执行(Execute)复选框。
单击关闭(Close)。
运行 Shell 脚本
Accessories | Terminal打开终端窗口。应用程序(Applications)菜单中的终端。
当终端(Terminal)窗口打开时,默认情况下您应该在您的主文件夹中。(Home Folder)在命令行上键入pwd并按 Enter 即可确认这一事实。在提示符下,键入./user_guide_backups.sh并按Enter。
您的Home Folder(Home Folder)的Backups文件夹中应该有一个.tgz文件。如果您右键单击文件名,您会看到几个选项,用于在可用的存档程序之一中打开存档,或使用“在此处提取”命令将文件直接提取到“(Extract Here )备份(Backups)”文件夹。
更多信息
下面的链接提供了有关 shell 脚本、tar和date命令以及其他Linux命令的更多信息。
脚本
使用 bash shell 编写脚本的快速指南(A quick guide to writing scripts using the bash shell)
Bash Shell Scripting – 10 Seconds Guide | All about Linux
Bash 参考手册(Bash Reference Manual)
Linux 命令
tar 手册页(tar MAN Page)
日期手册页(date MAN Page)
bash 命令——Linux 手册页(bash commands – Linux MAN Pages)
浏览这些页面将帮助您构建自己有用的 bash shell 脚本。
Back Up a Directory in Linux using a Shell Script
There are several орtions for backing up your data in Linux. You can use somе freely аvailable software programs, such аs fwbackups and Sbackup. However, there is a simple method of backing up a directory without installing any extra software.
We will create a shell script using variables, the tar command and the date command to create a dated backup file of a directory, with its subdirectories.
A shell script is essentially a file containing a list of commands that are run in sequence. If you have a series of commands you regularly run in order, it is helpful to create a shell script containing these commands. Then, you only have to run the script file to run the commands.
Creating the Shell Script File
For this example, we are going to create a shell script to backup a directory containing files for a user guide. We are using the Gnome environment in Ubuntu.
First, access your home directory, by selecting Home Folder from the Places menu. The File Browser opens to your home directory.
We are going to create a new empty file in which we will enter the commands for performing the backup. Right-click in the right pane and select Create Document | Empty File from the pop-up menu.
A file is added to the list and is ready to be renamed. Type in a name for the file, giving the file an extension of .sh.
For this example, we named our file user_guide_backups.sh.
Now we need to add the commands to the file. Right-click on the name of the file and select Open with gedit from the pop-up menu.
The file opens in gedit. Enter the following lines into the file and click Save. The purpose of each line is listed below.
NOTE: You can also copy the following text and paste it into gedit. Be sure to change <username> to your username.
#!/bin/bash
SRCDIR="/home/<username>/Documents/my_work/"
DESTDIR="/home/<username>/Backups/"
FILENAME=ug-$(date +%-Y%-m%-d)-$(date +%-T).tgz
tar – create – gzip – file=$DESTDIR$FILENAME $SRCDIR
Line-by-Line Description
The following table describes what each line is in the shell script file.
Line # |
Description |
1 |
This line must be the first line in a bash shell script, which is the default type of script. |
2 |
This line sets up a variable called SRCDIR and sets the value of it to the directory to be backed up.
NOTE: Be sure to replace <username> with your username. |
3 |
This line sets up a variable called DESTDIR and sets the value of it to the directory into which the backup file will be written.
NOTE: Be sure to replace <username> with your username. |
4 |
This line sets up a variable called FILENAME and sets the value using text and variables containing the date command to add the current date and time to the filename. For example, the filename might be ug-20100212-13:03:45.tgz.
NOTE: When using a variable, always start it with the dollar sign ($). If you use a command as part of a variable, enclose the command and the options for the command in parentheses. |
5 |
This line is the tar command with the following function and options added.
–create |
This function creates a new archive (or truncates an old one if the filename specified already exists) and writes the named files or directory to it. |
–gzip |
This option tells tar to compress the files in the archive using the gzip utility. |
–file |
This option gives tar the filename to use. In this case, we assembled the filename using the DESTDIR, FILENAME, and SRCDIR variables. |
|
Editing the Permissions on the Shell Script File
Before running your script, you need to make sure the file has the correct permissions. To do this, open your Home Folder again as mentioned above and right-click on the shell script file. Select Properties from the pop-up menu.
The Properties dialog box displays. Make sure the Execute check box is selected.
Click Close.
Running the Shell Script
To run the shell script, open a terminal window by selecting Accessories | Terminal from the Applications menu.
When the Terminal window opens, you should be in your Home Folder by default. Typing pwd on the command line and pressing enter confirms this fact. At the prompt, type ./user_guide_backups.sh and press Enter.
You should have a .tgz file in the Backups folder in your Home Folder. If you right-click on the filename, you see several options for opening the archive in one of the available archiving programs, or extracting the files directly to the Backups folder using the Extract Here command.
More information
The links below provide more information about shell scripts, the tar and date commands, and other Linux commands.
Scripting
A quick guide to writing scripts using the bash shell
Bash Shell Scripting – 10 Seconds Guide | All about Linux
Bash Reference Manual
Linux Commands
tar MAN Page
date MAN Page
bash commands – Linux MAN Pages
Exploring these pages will help you to construct your own useful bash shell scripts.