Skip to content

Commit b8e2e08

Browse files
committed
updated tar.md
1 parent d2890f7 commit b8e2e08

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tar.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,63 @@ Replace 'DEVICE' with whichever device is applicable to you, but note that it mu
1616
```bash
1717
tar -czvpf "$(printf '/media/%s/%s/%d:%d_%(%F)T.tgz' "$USER" 'DEVICE' ${UID:-`id -u`} ${GID:-`id -g`} -1)" "$HOME"
1818
```
19+
---
20+
## How to Backup a specific user's home directory?
21+
22+
Use the following command to take a backup of a user's home directory:
23+
24+
In this example, we will backup our test user – **`'2daygeek`**'s home directory, and the output file will be saved in the ** `'/backup'`** directory.
25+
26+
```
27+
# tar -zcvpf /backup/2daygeek-backup-$(date +%d-%m-%Y).tar.gz /home/2daygeek
28+
```
29+
Due to some reason, if you would like to exclude some folders from backing up, then use the following format:
30+
31+
Note: The below example will exclude the entire ** `'demo'`** directory and archive the rest of the files and folders.
32+
33+
```
34+
tar --exclude='/home/2daygeek/demo' -zcvpf /backup/2daygeek-backup-$(date +%d-%m-%Y).tar.gz /home/2daygeek
35+
```
36+
37+
Similarly, if you would like to exclude some of the pattern files or group of files, then use the following format:
38+
39+
Note: The below example will exclude the ** `'.mp3 & .avi'`** files from ** `'demo'`** directory and archive the rest of the files.
40+
41+
```
42+
tar -- **exclude**={'*.mp3','*.avi'} '/home/2daygeek/ **demo**/' -zcvpf /backup/2daygeek-backup-$(date +%d-%m-%Y).tar.gz /home/2daygeek
43+
```
44+
45+
## How to Backup a single user's home directory using shell script?
46+
47+
This shell script allows you to backup the given user's home directory.
48+
49+
To do so, add the following shell script in a file:
50+
51+
```
52+
#!/bin/bash
53+
DATE=$(date +%d-%m-%Y)
54+
BACKUP_DIR="/backup"
55+
56+
#To backup 2daygeek's home directory
57+
tar -zcvpf $BACKUP_DIR/$USER-$DATE.tar.gz /home/$USER
58+
59+
#To delete files older than 10 days
60+
find $BACKUP_DIR/* -mtime +10 -exec rm {} \;
61+
```
62+
1963
---
2064
## `tar` commands
2165

66+
```bash
67+
tar -zcvpf /[Backup_File_Location]/[Backup_Filename] /[User's_Home_Directory_Location]
68+
69+
z : Compress the backup file with ‘gzip’ to make it small size.
70+
c : Create a new backup archive.
71+
v : verbosely list files which are processed.
72+
p : Preserves the permissions of the files put in the archive for later restoration.
73+
f : use archive file or device ARCHIVE.
74+
```
75+
2276
To extract an uncompressed archive:
2377
`tar -xvf /path/to/foo.tar`
2478

0 commit comments

Comments
 (0)