Rabu, 01 November 2023

SCRIPT-backup linux

 

How to compress files using tar command on Ubuntu Linux

See all Ubuntu Linux related FAQ
How do I compress and backup files using tar command to another directory on same computer or another Linux computer under Ubuntu Linux?

You need to use the tar command to compress files and folder using tar command on Ubuntu Linux.
Tutorial details
Difficulty levelEasy
Root privilegesYes
RequirementsLinux terminal
CategoryBackup Management
Prerequisitestar command on Ubuntu Linux
OS compatibilityDebian  Linux  Mint  Pop!_OS  Ubuntu
Est. reading time3 minutes

nixCraft: Privacy First, Reader Supported
  • nixCraft is a one-person operation. I create all the content myself, with no help from AI or ML. I keep the content accurate and up-to-date.
  • Your privacy is my top priority. I don’t track you, show you ads, or spam you with emails. Just pure content in the true spirit of Linux and FLOSS.
  • Fast and clean browsing experience. nixCraft is designed to be fast and easy to use. You won’t have to deal with pop-ups, ads, cookie banners, or other distractions.
  • Support independent content creators. nixCraft is a labor of love, and it’s only possible thanks to the support of our readers. If you enjoy the content, please support us on Patreon or share this page on social media or your blog. Every bit helps.

Join Patreon ➔

Compressing files using tar command on Ubuntu Linux

First, open up a terminal under Ubuntu Linux. Some directories such as /etc or /usr require root level permissions to read and write for backup. So you may need to run tar command using the sudo command. In this example backup, /etc and your home directory called /home/vivek to /backup directory, enter:
sudo mkdir /backup
sudo tar -zcvpf /backup/files.backup.Nov_6_2009.tar.gz /etc/ /home/vivek/

Where,

  • z : Compress the backup file with ‘gzip’ to make it smaller.
  • c : Create a new backup archive called /backup/files.backup.Nov_6_2009.tar.gz.
  • v : Verbose mode, the tar command will display what it’s doing to the screen.
  • p : Preserves the permissions of the files put in the archive for restoration later.
  • f /backup/files.backup.Nov_6_2009.tar.gz: Specifies where to store the backup, /backup/files.backup.Nov_6_2009.tar.gz is the filename used in this example.
How to compress files using tar command on Ubuntu Linux

How Do I Exclude Certain Files or Directories?

You can exclude all *.mp3 stored in /home/vivek/music directory with –exclude option:
sudo tar --exclude='/home/vivek/music/' -zcvpf /backup/files.backup.Nov_6_2009.tar.gz /etc/ /home/vivek
Another example (exclude ~/music/ and ~/Downloads/*.avi files):
sudo tar --exclude='/home/vivek/music/' --exclude='/home/vivek/Downloads/*.avi' -zcvpf /backup/files.backup.Nov_6_2009.tar.gz /etc/ /home/vivek

How Do I See a List Of Files Stored In Tar Ball or an Archive?

Type the following command:
sudo tar -ztvf /backup/files.backup.Nov_6_2009.tar.gz
Where,

  • t: List the contents of an archive.

How Do I Restore Files?

You can use the command as follows to restore everything in / directory:
sudo tar -xvpzf /backup/files.backup.Nov_6_2009.tar.gz -C /
OR
cd /
sudo tar -xvpzf /backup/files.backup.Nov_6_2009.tar.gz

Another option is to type the following command to extract ubuntu-archive.tar in the current directory:
tar -xvf ubuntu-archive.tar
Where,

  • -x: Extract the files.
  • -C / : Extract the files in / directory.

In this example, you are restoring to the /delta directory.
sudo tar -xvpzf /backup/files.backup.Nov_6_2009.tar.gz -C /delta

How Do I Backup Files To a Remote Server Called backup.example.com?

Type the command as follows to backup /etc/ and /home/vivek directories to a remote system called backup.example.com:
sudo -s
ssh user@backup.example.com "mkdir /backup"
tar zcvpf - /etc/ /home/vivek | ssh user@backup.example.com "cat > /backup/files.backup.Nov_6_2009.tar.gz"

How Do I Restore Backup From a Remote System to Local Ubuntu Box?

Type the command
sudo -s
cd /
ssh user@backup.example.com "cat /backup/files.backup.Nov_6_2009.tar.gz" | tar zxpvf -

Summing up

You learned how to compress and extract files using the tar command on Ubuntu Linux.

Ubuntu backup with tar command

Table 1: How to Compress and Extract Files Using the tar Command On Ubuntu Linux
tar commandDescription
tar -zcvf /backup/archive.tar.gz /home/vivek/
Backup /home/vivek/ directory on Ubuntu Linux with compress option saves disk space.
sudo tar -zcvf /backup/09-Jun-2009.tar.gz\
/home/ /etc/ /var/spool/
Backup all home dirs, including system-level dirs such as /etc/ and /var/spool under Ubuntu Linux using tar command.
sudo tar -xvf /backup/09-Jun-2009.tar.gz -C /
Extract compressed backup on Ubuntu Linux using tar command.
tar zcvf - /home/vivek \
| ssh -t vivek@backup.cyberciti.biz \
"cat > /home/vivek/backups/09-Jun-2009.tar.gz"
Backup your /home/vivek/ directory to backup server named backup.cyberciti.biz (IP 192.168.1.201) host over ssh session
ssh vivek@backup.cyberciti.biz \
"cat /home/vivek/backups/09-Jun-2009.tar.gz" \
| tar zxvf -
Extract and resotre compressed backup from the remote server over using the ssh.

For more info see the following manual page using the man command or by passing the --help option to the tar as follows:
man tar
tar --help

Tidak ada komentar:

Posting Komentar