Table of Contents
镜像(本机备份系统,还原到新主机上)
rsync命令
dd命令
Home Operation and Maintenance Linux Operation and Maintenance Commonly used Linux system backup and recovery commands

Commonly used Linux system backup and recovery commands

Aug 03, 2023 pm 04:23 PM
linux System backup


Commonly used Linux system backup and recovery commands
##Delete the database and run away I often hear about this, but this can only be a joke. You can't do this in real work. Otherwise, the library will be deleted and the road will no longer be able to run.

So, backup is very important! ! ! ! !

tar command

Copy (backup the entire system locally, restore to the local machine later)

Note that there must be sufficient free space in the root directory for backup.

cd /
#tar.gz格式
tar cvpzf system_backup.tar.gz / --exclude=/proc --exclude=/lost+found --exclude=/system_backup.tar.gz --exclude=/mnt --exclude=/sys

#tar.bz2格式
tar cvpjf system_backup.tar.bz2 / --exclude=/proc --exclude=/lost+found --exclude=/system_backup.tar.bz2 --exclude=/mnt --exclude=/sys


# 恢复系统
cd /
#上传文件到根目录下
tar xvpfz system_backup.tar.gz -C /
或
tar xvpfj system_backup.tar.bz2 -C /

#创建备份时排除的目录
mkdir proc
mkdir lost+found
mkdir mnt
mkdir sys
Copy after login

  • /proc Permissions: File Owner: root Group: root Owner: Read Execution Group: Read Execution Other: Read Execution

  • /lost found Permissions: File Owner: root Group: root Owner: Read Write Execution Group: Read Execute Others: Read Execute

  • /mnt Permissions: File Owner: root Group: root Owner: Read Write Execution Group: Read Execute Others: Read Execute

  • /sys 权限:文件所有者:root群组:root 所有者:读取 写入 执行 群组:读取 执行 其它:读取 执行

  • 搜索公众号Linux中文社区后台回复“私房菜”,获取一份惊喜礼包。

恢复完成重启以后,所以的事情都会和你备份的时候一模一样。

镜像(本机备份系统,还原到新主机上)

1,检查系统版本,在目标机上安装一样版本的系统(最简安装即可),分区格式,类型也一样(我没试过不一样的情况,不知道能否成功)

lsb_release -a
uname -a
df -Th
free -h
Copy after login

2,备份源系统

# 因为目标机和源主机硬件配置不同,所以排除dev,tmp;再适当增加你要排除的文件,如:--exclude=/root/*.bz2
# 这里再mnt下有充足空间,所以保存到mnt下。
cd /
tar cvpzf /mnt/system_backup.tar.gz / --exclude=/mnt/system_backup.tar.gz \
--exclude=/proc --exclude=/lost+found --exclude=/mnt --exclude=/sys --exclude=/dev \
--exclude=/tmp --exclude=/media

# 上传到目标主机
scp /mnt/system_backup.tar.gz root@192.168.0.166:/mnt
Copy after login

3,在目标机上用ISO、LiveCD等启动,挂载磁盘(一般会自动挂载到/media文件夹)

sudo -s  
cd /media/<对应的uuid号>
# 备份重要配置文件/boot/gurb/gurb.cfg /etc/fstab
记录里面的UUID,

# 删除重复文件
# 除了上面备份系统时排除的一些文件夹外,比如说dev mnt media sys这些文件夹,其他全部删除。
rm -rf root home usr lib lib64 etc var bin sbin opt boot run selinux vmlinuz initrd.img

# 还原备份
mount /dev/vda1 /mnt/1
# 这里注意千万不要写/目录,会把现有的系统搞挂!!!应该是挂载的目录
tar xvpfz system_backup.tar.gz -C /mnt/1
cd /mnt/1       #此时你可以看到根目录的结构,但是编辑fstab文件发现是现有系统的fstab
chroot ./       #执行chroot后会以./目录为根目录,这时编辑的文件就是真正的目标源文件了。
Copy after login

还原后修改/etc/fstab里的UUID为刚刚备份的文件里面的信息,注意分区格式也要对应。

修改/boot/gurb/gurb.cfg里的UUID为刚刚备份的文件里面的信息。修改网卡、IP配置文件,以防无法分配IP。(如果是虚拟机记得添加网卡,配置中等性能的显卡)

如果有依赖于原有平台的服务,如内建NTP,Agent等监控程序;关闭服务,关闭开机自启;

Ubuntu:在命令行输入runleve可以查看当前运行级别,一般默认是2

查看/etc/rc2.d目录中的S开头的服务都是会开机自动运行的;里面是软链接,想添加的话自己建一个链接文件就可以,S代表start,后面数字是启动顺序,删除软链接。同时删除/etc/init.d/下对应的脚本。

vim /etc/init.d/rc.local
Centos:用systemctl
Copy after login

完成上述步骤后

exit      #退出chroot
cd ~
umount /mnt/1

# 一切完成后就可以重启了,不出意外就正常启动系统了(启动后原来安装系统时设置的账户等全部消失;账户和源主机一致)。
若开机Grub提示“boot error 15 :Error 15 file not found”
解决方法:请检查GRUB相关文件的内核文件所在位置。通常与/boot分区有关。
 
若开机Grub提示“dracut:dono&#39;t how to hand root=f078”
解决方法:将root=UUID改成root=/dev/sdaX这种格式。
 
若开机系统提示/usr/libexec/gconf-sanity-check-2退出状态256的解决
解决方法:chmod 777 /tmp
Copy after login

rsync命令

注意目标分区的格式最好是NTFS、FAT、EXT之类的格式,避免遇到大于4G的文件无法备份的问题。

#最好有其他分区或外接存储设备,挂载好,df -lh看挂载点。
#备份
rsync -Pa / /media/usb/backup_20170410 --exclude=/media/* --exclude=/sys/* --exclude=/proc/* --exclude=/mnt/* --exclude=/tmp/*

#恢复

rsync -Pa /media/usb/backup_20170410 /
Copy after login

dd命令

dd命令属于扇区克隆,目标分区要比备份分区要大,即使没有使用的空间也会被原样克隆下来,会比较慢。

#备份
df -h   #查看系统所在分区
dd if=/dev/sda1 of=/dev/sdb3     #备份sda1到sdb3中

#恢复
dd if=/dev/sdb3 of=/dev/sda1     #恢复sdb3到sdb1中
Copy after login

The above is the detailed content of Commonly used Linux system backup and recovery commands. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

What to do if the docker image fails What to do if the docker image fails Apr 15, 2025 am 11:21 AM

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

See all articles