Lesson 10 - Backup and Restore on Ubuntu
29/06/2024 - 3 phút
In this lesson, we will learn how to back up and restore data on a Linux system using tar
and rsync
, set up automated backups with cron
, and restore data from backups. Data backup is crucial for protecting your data from loss due to system failures or other issues.
1. Using tar and rsync for Data Backup
tar
: Creating Backups
The tar
command is used to create backups of files and directories by archiving them into a tarball file.
Command | Description | Example |
---|---|---|
tar -cvf | Create a tarball file from files and directories | tar -cvf backup.tar /home/user/Documents |
tar -czvf | Create a compressed tarball file with gzip | tar -czvf backup.tar.gz /home/user |
tar -rvf | Add files to an existing tarball file | tar -rvf backup.tar newfile.txt |
tar -xvf | Extract files from a tarball file | tar -xvf backup.tar |
tar -xzvf | Extract files from a compressed tarball file with gzip | tar -xzvf backup.tar.gz |
Detailed Examples:
Create a backup of the
/home/user/Documents
directory:tar -cvf backup.tar /home/user/Documents
This command creates a
backup.tar
file containing all files and directories in/home/user/Documents
.Create a compressed backup of the
/home/user
directory:tar -czvf backup.tar.gz /home/user
This command creates a compressed
backup.tar.gz
file containing all files and directories in/home/user
.Extract files from a tarball:
tar -xvf backup.tar
This command extracts all files and directories from
backup.tar
to the current directory.
rsync
: Synchronizing Backups
The rsync
command is used to back up and synchronize files and directories between different locations.
Command | Description | Example |
---|---|---|
rsync -av | Backup and synchronize files and directories | rsync -av /home/user/Documents /backup |
rsync -avz | Backup and synchronize files and directories over SSH | rsync -avz /home/user/Documents user@remote:/backup |
Detailed Examples:
Backup the
/home/user/Documents
directory to the/backup
directory:rsync -av /home/user/Documents /backup
This command copies all files and directories from
/home/user/Documents
to the/backup
directory, preserving permissions and directory structure.Backup the directory over SSH:
rsync -avz /home/user/Documents user@remote:/backup
This command copies all files and directories from
/home/user/Documents
to the/backup
directory on the remote serverremote
, using SSH for secure transfer.
2. Setting Up Automated Backups with cron
cron
is a powerful tool that allows you to automate tasks on a Linux system.
Creating an Automated Backup Task with cron
Open the crontab file:
crontab -e
Add the following line to the crontab file to set up a daily backup at 2 AM:
0 2 * * * tar -czvf /backup/backup_$(date +\%F).tar.gz /home/user
This command creates a compressed backup of the
/home/user
directory and saves it to the/backup
directory with the current date in the filename.
Detailed Example:
Set up a daily backup:
0 2 * * * rsync -av /home/user/Documents /backup
This line runs the
rsync
command to back up the/home/user/Documents
directory to the/backup
directory every day at 2 AM.
3. Restoring Data from Backups
Extracting and Restoring from a tarball
Extract files from a tarball:
tar -xzvf /backup/backup_2024-06-28.tar.gz -C /home/user
This command extracts the
backup_2024-06-28.tar.gz
file to the/home/user
directory.
Detailed Example:
Restore from a tarball:
tar -xzvf /backup/backup_2024-06-28.tar.gz -C /home/user
This command extracts the
backup_2024-06-28.tar.gz
file to the/home/user
directory, restoring all backed-up files and directories.
Using rsync to Restore Data
Restore data from the backup directory:
rsync -av /backup /home/user/Documents
This command copies all files and directories from the
/backup
directory to the/home/user/Documents
directory.
Detailed Example:
Restore using rsync:
rsync -av /backup /home/user/Documents
This command copies all files and directories from the
/backup
directory to the/home/user/Documents
directory, restoring the backed-up data.
Conclusion
In this lesson, you have learned how to use tar
and rsync
to back up and restore data, as well as how to set up automated backups with cron
. These skills are essential for ensuring that your data is always protected and can be quickly restored when needed. Practice these commands to become proficient in managing backups on your Linux system.