How to back up and restore a CentOS system
Use rsync for full system backups by mounting a target drive and running rsync with archive, ACL, and extended attribute preservation while excluding virtual filesystems, then unmount after completion. 2. Create compressed system backups with tar by excluding temporary directories and compressing the root filesystem into a .tar.gz file, and restore by extracting it in a chroot environment after reinstalling GRUB. 3. Perform selective backups of critical directories like /home, /etc, /var/www, and /var/lib/mysql using tar, automate with a script, and schedule via cron for routine protection. 4. Restore full backups by booting from live media, partitioning the target disk, mounting the root partition, restoring files, reinstalling the bootloader, and updating /etc/fstab if needed, or restore selective data by extracting specific archives and restarting services. 5. Ensure reliability by regularly testing restores, storing backups offsite, verifying disk layouts with lsblk and blkid, maintaining a package list for quick reinstallation, and considering LVM snapshots for consistent live backups, with rsync or tar being optimal for most CentOS users due to simplicity and control.
Backing up and restoring a CentOS system is essential for disaster recovery, hardware migration, or system upgrades. A solid backup strategy ensures you can recover your data, configurations, and operating system quickly. Below are practical methods to back up and restore a CentOS system effectively.

1. Full System Backup Using rsync
rsync
is a reliable and widely used tool for creating complete system backups. It copies files incrementally and preserves permissions, ownership, and symlinks.
Steps to Back Up with rsunk:
-
Boot into a live environment (optional but recommended for consistency).
-
Mount your target backup drive (e.g., external disk or network location):
mkdir /mnt/backup mount /dev/sdb1 /mnt/backup
Run rsync to copy the entire system (exclude temporary and virtual filesystems):
rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost found"} / /mnt/backup/
-a
: Archive mode (preserves permissions, symlinks, etc.)-A
: Preserve ACLs-X
: Preserve extended attributes-v
: Verbose output
After backup completes, unmount:
umount /mnt/backup
This method works well for full system images and supports incremental backups.
2. Using tar for System Backup
tar
is another powerful tool for creating compressed system backups.
Create a Full Backup:
tar --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/run --exclude=/mnt --exclude=/media --exclude=/tmp -czf /backup/centos-backup.tar.gz /
-c
: Create archive-z
: Compress with gzip-f
: Specify filename
Store the .tar.gz
file on an external drive or remote server.
Restore from tar Backup:
Boot from a CentOS live USB/CD.
Mount your root partition:
mount /dev/sda1 /mnt
Extract the backup:
tar -xzf /backup/centos-backup.tar.gz -C /mnt/
Reinstall GRUB (if needed):
mount --bind /dev /mnt/dev mount --bind /proc /mnt/proc mount --bind /sys /mnt/sys chroot /mnt grub2-install /dev/sda grub2-mkconfig -o /boot/grub2/grub.cfg exit
Reboot.
3. Backup Important Data and Configurations (Selective)
For routine backups, focus on critical directories:
/home
– user data/etc
– system configuration/var/www
– web content/var/lib/mysql
– databases/opt
– third-party software
Example backup script:
#!/bin/bash BACKUP_DIR="/backup/$(date %F)" mkdir -p $BACKUP_DIR tar -czf $BACKUP_DIR/home.tar.gz /home tar -czf $BACKUP_DIR/etc.tar.gz /etc tar -czf $BACKUP_DIR/mysql.tar.gz /var/lib/mysql # Optional: Copy to remote server scp $BACKUP_DIR/*.tar.gz user@remote:/backup/
Schedule it via cron:
0 2 * * * /usr/local/bin/backup.sh
4. Restore Process Overview
Restoration depends on your backup method:
rsync or tar full system backup:
- Boot from live media.
- Partition and format the target disk.
- Mount the root partition and restore files.
- Reinstall bootloader (GRUB).
- Update
/etc/fstab
if UUIDs changed.
Selective data restore:
- Extract needed
.tar.gz
files to appropriate locations. - Restart services (e.g.,
systemctl restart httpd mysqld
).
- Extract needed
5. Tips for Reliable Backups
Test your restore process regularly.
Store backups offsite or on separate hardware.
Use
lsblk
andblkid
to verify disk layouts before restoring.Keep a list of installed packages:
rpm -qa > package-list.txt
Restore with:
yum install $(cat package-list.txt)
Consider using LVM snapshots for consistent backups on running systems.
Final Notes
There are also advanced tools like Bacula, Amanda, or Clonezilla for enterprise or disk-image-based backups. But for most CentOS users, rsync
or tar
offers simplicity, control, and reliability.
Choose the method that fits your environment—full system image for disaster recovery, selective backups for daily use.
Basically, back up early, back up often, and always verify you can restore.
The above is the detailed content of How to back up and restore a CentOS system. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

When the CentOS server cannot be connected to the network, you can follow the following steps to check: 1. Check the status of the network interface, use iplinkshow to confirm whether the interface is enabled, if not enabled, use sudoiplinksetup to start, and use ipaddrshow to check the IP allocation status; 2. If it is in DHCP mode, run sudodhclient to obtain the IP. If it is static configuration, check the IP, gateway and DNS settings in /etc/sysconfig/network-scripts/ifcfg- and restart the network service; 3. Check the routing table iprouteshow to ensure that there is a default gateway. If there is no, add it temporarily or modify GATEWAY in the configuration file.

To update all software packages on the CentOS system, you can use yum (CentOS7) or dnf (CentOS8 and above). The specific steps are as follows: 1. Check for available updates and use "sudoyumcheck-update" or "sudodnfcheck-update" to list the packages to be updated; 2. Execute the system-wide update, and use "sudoyumupdate-y" or "sudodnfupgrade--allowerasing" commands to upgrade, where the -y parameter is automatically confirmed, and --allowerasing allows the deletion of conflicting packages; 3. If the update involves a new kernel, the system needs to be restarted to take effect, and "unam can be used to use "

Adding new users to the CentOS system requires attention to security settings and permission management. The most basic method is to use the useradd command. Execute sudouseraddnewusername and add the -m parameter to create the user and home directory; use the -G parameter to specify the user group such as wheel to give sudo permissions. Then use sudopasswd to set the password. To configure sudo permissions, it is recommended to add the user to the wheel group: sudousermod-aGwheelnewusername and verify the sudo permissions. If you need SSH login, 1. Check the AllowUsers settings in /etc/ssh/sshd_config and add users

Identifythenewdiskusinglsblkorfdisk-ltolocatethecorrectdevicelike/dev/sdb.2.Optionallypartitionthediskwithfdisk/dev/sdb,createaprimarypartitionusingdefaultsettings,andwritechangeswithw,thenrunpartprobetoupdatethekernel.3.Createafilesystemusingmkfs-tx

The key to updating the CentOS kernel is to use the ELRepo repository and set up the startup items correctly. 1. First run uname-r to view the current kernel version; 2. Install the ELRepo repository and import the key; 3. Use yum to install kernel-lt (long-term support version) or kernel-ml (main version); 4. After the installation is completed, check the available kernels through the awk command and use grub2-set-default to set the default startup item; 5. Generate a new GRUB configuration file grub2-mkconfig-o/boot/grub2/grub.cfg; 6. Finally restart the system and run uname-r again to confirm whether the kernel version is effective. The whole process requires

To configure the PHP environment to support MongoDB, the core step is to install and enable the PHP driver of MongoDB to enable the PHP application to communicate with the MongoDB database. 1. Install MongoDBPHP driver, it is recommended to use PECL to install. If there is no PECL, you need to first install the PHP development package and related compilation tools; 2. Edit the php.ini file and add extension=mongodb.so (or .dll) to enable the extension; 3. Restart the web server or PHP-FPM service to make the configuration take effect; 4. Verify whether the extension is loaded successfully through phpinfo() or php-m. Frequently asked questions include missing PECL commands, compilation errors, php.ini

Installing and configuring fail2ban on CentOS is not complicated, it mainly includes the following steps: 1. Install fail2ban using yum; 2. Manually enable and start the service; 3. Create a jail.local file for custom configuration; 4. Set SSH defense rules, including enabling sshd, specifying the blocking time and retry times; 5. Configure firewalld as an action actuator; 6. Regularly check the blocking IP and logs. Fail2ban detects abnormal login behavior through monitoring logs and automatically blocks suspicious IPs. Its core mechanism relies on key parameters such as bantime (banned time), findtime (statistic window time) and maxretry (maximum failure number).

The recommended method to modify the CentOS host name is to use the hostnamectl command. The specific steps are: 1. Execute sudohostnamectlset-hostnamenew-hostname to set the new host name; 2. Check whether the cloud server or container environment needs to add the command to the initialization script to prevent the original name from being restored after restart; 3. Manually edit the /etc/hostname file and update the hostname resolution in /etc/hosts to ensure compatibility and normal service operation; 4. Verify whether the hostname is effective through the hostname or hostnamectl command. The entire process requires attention to configuration file adaptation and environment restrictions to avoid host name resolution problems.
