Backup and Restore in Linux
Posted on June 1, 2024 (Last modified on June 8, 2024) • 1 min read • 207 wordsLearn how to perform backups and restores in Linux using tools like tar, rsync, and cron for automated backups.
Regular backups are essential for data protection. This guide covers performing backups and restores in Linux using tools like tar
, rsync
, and cron
for automated backups.
Use tar
to create a compressed archive.
tar -czvf backup.tar.gz /path/to/directory
Use tar
to extract an archive.
tar -xzvf backup.tar.gz
Use rsync
for efficient file synchronization.
rsync -avh /source/directory /destination/directory
Use rsync
to perform remote backups over SSH.
rsync -avh -e ssh /source/directory user@remote:/destination/directory
Edit the crontab to schedule automated backups.
crontab -e
Add a cron job to run a backup script daily at midnight.
0 0 * * * /path/to/backup_script.sh
Create a simple backup script using tar
and rsync
.
#!/bin/bash
# Backup script
tar -czvf /backups/backup_$(date +%F).tar.gz /path/to/directory
rsync -avh /path/to/directory /backups/
Make the script executable.
chmod +x /path/to/backup_script.sh
Performing regular backups and being prepared to restore data is crucial for data protection. Practice using tar
, rsync
, and cron
to create and automate backups, ensuring your data is safe and recoverable.