How to Backup Necessary Linux Directories in 5 Ways?

In this article, you will know directories in Linux that should be backed up and 5 main ways to do this.

download-icon
Free Download
for VM, OS, DB, File, NAS, etc.
dan-zeng

Updated by Dan Zeng on 2024/01/24

Table of contents
  • What Directories Should You Backup?

  • Way 1: Backup Linux Directories with Cp

  • Way 2: Backup Linux Directories with Tar

  • Way 3: Backup Linux Directories with Rsync

  • Way 4: Backup Linux Directories with Scp

  • Way 5: Backup and Restore Linux Files/Folders/Servers

  • Conclusion

In Linux, directories play a crucial role in organizing and managing files and data on the system. The directory structure forms the foundation of the Linux file system, providing a hierarchical organization of files and directories.

Additionally, many Linux applications and services rely on specific directories and files within the Linux directory structure. For example, configuration files for applications are often stored in specific directories, and system logs are typically stored in a centralized location. And sometimes, you may need it to overhaul the primary filesystem structure. 

linux.png

Although you can backup Linux to multiple destinations, it's necessary to backup certain important Linux directories other than the whole server.

But hold on, you must know what directories are worth trying. In this article, you will know directories in Linux that should be backed up and the 5 main ways to do this.

What Directories Should You Backup?

/etcStores important configuration files for a system, including user and group information, network settings, application configurations, and startup files. Without it, recovering from an outage or failure would be difficult and time-consuming. Having a backup of /etc is crucial to avoid having to reinstall the system and applications from scratch.
/home User data, files, documents, pictures, and downloads, are stored in the /home directory under the user's name. Backing up this directory is essential as it's the most important one for users.
/root Backing up the root user's home directory is crucial. It contains important and unique information, such as downloads, configurations, scripts, and notes, that is essential for the system administrator.
/var Stores files like databases, web pages, logs, crontabs, and more. It grows daily and requires its own separate storage and disk.
/usr/local/binMay be empty or contain non-standard scripts and applications. If empty, an empty file (/usr/local/bin/empty) is suggested to be created to avoid backup confusion.

/usr/local/sbin

Likewise, /usr/local/sbin is also important and often empty. An empty file (/usr/local/sbin/empty) can be added in the directory to indicate its status.
/srv Used for internet-related service files like WWW uploads, FTP files, and CVS, but it may remain empty. Its use is random and uncommon.
/optFor third-party or separate software. It's useful for testing new software. An empty file (opt/empty) is added if empty.
Shared directoriesMay be affiliated with /finance, /data, or /hr that have special permissions for a specific group. They must be backed up as they may contain unique corporate data. Only create these directories if needed.

Way 1: Backup Linux Directories with Cp

Cp (copy) command copies a file or a directory with 3 primary operation modes to copy a file to others, numerous files to a directory, and copy the directory to others.

Options:

--archive, -aPreserves the attributes and permissions of files and directories during the copy process.
-attributes-onlyOnly copies the file attributes.
--backup[=CONTROL] Backup all current destination files.
--copy-contents Copies special file contents recursively.
--force, -f  Deletes and retries opening an existing destination file, unless -n flag is used.
--interactive, -i Confirms before overwriting the previous -n flag.
--preserve[=ATTR_LIST]Preserves the included attributes.
--remove-destination Removes all current destination files before opening them.
--symbolic-link, -s  Makes symbolic links.
--suffix=SUFFIX, -SOverrides the backup suffix.
--helpDisplays the help menu and then terminates.

1.     Copy the directory with all content included to another directory. 

cp -r <sourceDirectory> <destinationDirectory> 

2.     Backup the existing file in the destination directory.

cp --backup <filename> <destinationDirectory> 

3.     Copy multiple directories on Linux.

$ cp -R <source_folder_1> <source_folder_2> ... <source_folder_n>  <destination_folder>

4.     Copy the /etc and home directory.

$ cp -R /etc/* /home/* /backup_folder

5.     Copy the directory content recursively.

$ cp -R <source_folder>/* <destination_folder>

Way 2: Backup Linux Directories with Tar

Tar (tape archive) compresses files and directories to a tarball and creates backups with tar, gzip, and bzip formats.

Options:

-cCreates a tar archive.
-xExtracts from the tar archive.
-tDisplays a list of files inside the tar archive.
-rAdds an additional file in the tar archive.
-WVerifies a tar archive.
-zCreate a tar archive using gzip.
-jCreate a tar archive using bzip.
-vDisplays verbose information.
-fSpecifies archive file name.

1.     Create a tar archive of a directory.

tar -cf archive_name.tar source­­­_dir

2.     Create a bz2 archive from the /etc directory.

tar -cjf archive_name.bz2 /etc

3.     Create a tar.gz archive from the /user directory.

tar -czf archive.name.tar.gz /use

4.     Extract a tar archive.

tar -tvf archive_name.tar

5.     Extract a tar gzip archive.

tar -xzvf archive.name.tar.gz

6.     Extract a tar bz2 archive.

tar -xjvf archive.name.tar.bz2

7.     Extract a tar archie to a different directory.

tar -xvf archive_name_tar -C / destination_directory

Way 3: Backup Linux Directories with Rsync

Rsync is a tool for copying files remotely, which can be used either on its own or integrated within bash scripts. It is utilized by a variety of graphical user interfaces (GUIs) and provides Linux administrators with ample flexibility to perform almost any kind of backup required.

You can backup the whole system, individual files, or directories using this Linux-based utility.

Options:


-a

Ensures that the copying process is done recursively and that the attributes, such as file/directory permissions and ownership, are preserved.
-v The verbose mode increases the number of logs printed in the terminal.
-xLimits rsync to sync within file system boundaries, excluding any mounts in the home directory.
-pDisplays the progress of each sync operation and retains partially transferred files if the transfer is interrupted.
--deleteDeletes any directories or files in the destination directory that do not exist in the source directory. Use with caution.
--exclude-from=FileExcludes files and directories listed.

Go to the Rsync website for the full options.

1.     Install Rsync.

sudo apt-get install rsync -y

Start and enable Rsync.

sudo systemctl start rsync

sudo systemctl enable rsync

2.     Backup $HOME directory to the external drive.

$ rsync -avxP --delete --exclude-from=/files/to/be/ignored_list.txt /home/external/drive/location

Note: add -n behind the -avxP to do a dry run for simulation if you are using Rsync for the first time. Remove it if the command runs as expected and run the command without -n to backup.

3.     Copy Linux directories to remote hosts.

$ rsync -ar <source_folder> <destination_user>@<destination_host>:<path>

4.     Copy the /etc/ directory to a backup server.

$ rsync -ar /etcusername@xxx.xxx.xxx.xx:/etc_backup

5.     Indicate the current data when backup a directory with bash.

$ rsync -ar /etc/* username@xxx.xxx.xxx.xx:/etc_backup /etc_$(date "+%F")

Way 4: Backup Linux Directories with Scp

Scp (secure copy) copes files across an ssh connection or via an encryption connection. You can use it to copy files from and to remote computers to your system, and from one remote computer to another on Linux, Mac, and Windows.

1.     Copy the Linux directory to a remote location.

$ scp -r <source_folder> <destination_user>@<destination_host>:<path>

2.     Copy all files in the home folder to the remote one. Add -r to copy files recursively.

scp /home/user/html/* username@server:/path/to/folder/

3.     Copy the /etc directory to a backup server.

$ scp -r /etc username@xxx.xxx.xxx.xx:/etc_backup

Way 5: Backup and Restore Linux Files/Folders/Servers

You can backup and restore files, directories, and server for Linux & Windows using the versatile Vinchin Backup & Recovery that also supports 12 virtualizations like VMware, Hyper-V, Xen, 6 databases, and NAS, which has the following features:

  • Simplified backup and restore procedures with automatic schedules.

  • Select one or more files/folders for backup.

  • Exclude or include a type of file with a wildcard.

  • Backup files/folders/servers to all NAS under CIFS and NFS protocols.

  • Enable data encryption with the AES-256 algorithm.

  • Restore data easily to NAS Share/File Server under encrypted transfer.

It is compatible with:

File Backup

  • Debian Linux 7 to 10

  • Ubuntu Linux 12 to 20

  • CentOS Linux: 6/7/8

  • Windows XP/7/8/10 and Server 2003 ~ 2019

Physical Server Backup

  • Windows 8 ~ 11

  • Windows XP/7/8/10/11 and Server 2003 ~ 2022

  • RedHat Enterprise Linux 6(6.0/6.8), 7(6.2/7.6), 8(8.0/8.1)

  • CentOS Linux 6(6.5/6.9), 7(7.2/7.6/7.7/7.9), 8(8.0/8.3)

Download the 60-day free trial of Vinchin Backup & Recovery for complete Linux backup and recovery now.

Easy Backup for Linux Director

1.     Go to Physical Backup> File Backup> Backup, and select the installed backup agent and source files/folders. You can add a wildcard policy here to filter certain files and apply the policy to other agents. 

b9d62874029d4ebb5b6c9e5939a2442.png

2.     Select a backup node and storage for the job. You can choose the added NAS here.

image.png

3.     Customize your desired backup strategies.

image.png

image.png

image.png

4.     View and submit. The job will run immediately or as scheduled based on your settings.

Easy Recovery for Linux Directory

1.     Go to Physical Backup> File Backup> Restore and choose a restore point from the backups.

image.png

2.     Choose to restore to File Server/NAS Share, select the backup agent/share and choose the restore path manually or the original path.

image.png

image.png

3.     Configure recovery strategies as needed.

image.png

image.png

4.     View and submit.

Conclusion

It is important to create backups of specific important folders or directories in Linux. This article provides details about the necessary Linux directories to be backed up and 5 ways to do so. The former 4 ways are related to command lines and the users can invoke them either within the system or after the installation. Or you can use Vinchin Backup & Recovery to backup and restore all important files/folders/servers in Linux & Windows.

Share on:

Categories: Linux Backup
Free Trial Now close-icon