WE CODE NOW
  • Home 
  • Blog 
  • Guides 
Guides
  1. Home
  2. Guides
  3. Linux Command Series
  4. Backup and Restore in Linux

Backup and Restore in Linux

Posted on June 1, 2024  (Last modified on June 8, 2024) • 1 min read • 207 words
Linux
 
Backup
 
Restore
 
Tar
 
Rsync
 
Cron
 
Linux
 
Backup
 
Restore
 
Tar
 
Rsync
 
Cron
 
Share via

Learn how to perform backups and restores in Linux using tools like tar, rsync, and cron for automated backups.

On this page
  • Using tar for Backups
    • Creating a tar Archive
    • Extracting a tar Archive
  • Using rsync for Backups
    • Basic rsync Usage
    • Remote Backups with rsync
  • Automating Backups with cron
    • Creating a cron Job
    • Example Backup Script
  • Conclusion

Backup and Restore in Linux  

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.

Using tar for Backups  

Creating a tar Archive  

Use tar to create a compressed archive.

tar -czvf backup.tar.gz /path/to/directory

Extracting a tar Archive  

Use tar to extract an archive.

tar -xzvf backup.tar.gz

Using rsync for Backups  

Basic rsync Usage  

Use rsync for efficient file synchronization.

rsync -avh /source/directory /destination/directory

Remote Backups with rsync  

Use rsync to perform remote backups over SSH.

rsync -avh -e ssh /source/directory user@remote:/destination/directory

Automating Backups with cron  

Creating a cron Job  

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

Example Backup Script  

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

Conclusion  

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.

 Linux Security Best Practices
Shell Scripting in Linux 
On this page:
  • Using tar for Backups
    • Creating a tar Archive
    • Extracting a tar Archive
  • Using rsync for Backups
    • Basic rsync Usage
    • Remote Backups with rsync
  • Automating Backups with cron
    • Creating a cron Job
    • Example Backup Script
  • Conclusion
Copyright © 2024 WE CODE NOW All rights reserved.
WE CODE NOW
Link copied to clipboard
WE CODE NOW
Code copied to clipboard