Backup directories in home with tar - linux

I want to make a backup of each directory in /home separately and each directory tar (backup) files to enter into a specified directory. Under linux ubuntu.

You can do something like this in bash:
for d in */; do short=${d%/}; tar -cvf ${short}.tar $short ;done
Or, more verbosely:
cd # go home
for d in */ # for all directories
do
short=${d%/} # strip off trailing slash
tar -cvf ${short}.tar $short # tar up directory into file with same name but ".tar" extension
done
So, if you have doirectories $HOME/Documents and $HOME/Music, you will get 2 tarfiles in your home directory called Documents.tar and Music.tar

To clarify ... I want to make a backup of all directories, for example to /home/user file is named backup-2014.02.02.tar and is located in the directory /home/user /backups. I'm doing a backup of the entire /home directory with the following script:
#!/bin/bash
today=$(date '+%Y.%m.%d')
tar czf /var/backup/backup_"$today".tar.gz /home
Yes, but I want to go to backups in the following way ... If the directory was /home/user file batskup-user-2014.02.04.tar.gz to go to the directory /home/backups

Related

Loop through directory and zip specific folder without parent path

I'm trying to make a bash script to loop through all folder in directories, and individually zip just the folder I want without all path and choose where to zip theme.
#!/bin/bash
for dir in /MyPersonalFolder/*/*/WhatIWantFolder
do
folder_number=$(basename ${dir%/*}) ### basename get the name of this folder [*] 'folder have numbers' /MyPersonalFolder/*/[*]/WhatIWantFolder
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "$dir"
mkdir -p ./backup-theme/ && sudo mv "${folder_number}-theme".tar.gz $_ ### I use this to move zipped folder to specific directory if i can choose where to zip file in the zip command line it's better
done
I can zip the folder I want, but the output zip file comes with this content:
/MyPersonalFolder/0001/0001/WhatIWantFolder
But what I need is to output the file with this path:
0001/
|___WhatIWantFolder/
I tried to change "$dir" in this line
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "$dir"
with basename ${dir%/*}
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "basename ${dir%/*}"
tar not found the folder it's come with errors
tar: e0001: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
You can cd to another directory while running tar using the --cd option.
#!/bin/bash
for dir in MyPersonalFolder/*/*/WhatIWantFolder; do
parent="${dir%/*/*}"
subdir="${dir#*/*/*}"
outfile="backup-theme/${subdir/\//-}.tar.gz"
tar --cd "$parent" -cvzf "$outfile" "$subdir"
done
The directory structure of the tar file would look something like this:
tar -tf backup-theme/0001-WhatIWantFolder.tar.gz
0001/WhatIWantFolder/
0001/WhatIWantFolder/f1

Listing log files in some directory and create tar file

i want to write a shell script in bash syntacs can do
listing all .log files in var/log directory and ask with y/n question for each file to create a tar from that file.
can anyone help me?
#!/bin/bash
log='find /var/log -name *.log'
If you want to get only files in /var/log (without subdirectories) you can do it on this way:
tar cvf tarfile.tar /var/log/*.log
if you want also the files in subdirectories you can do it on this way:
find /var/log -name *.log|tar cvf tarfile.tar
(this may not work if you have space or special symbols in filenames)

Can tar extraction erase brother directory ?

I made several backups on different directories with Backup Manager. Eg: /home/user1 /home/user2...
It gives me some tar files. The content of a tar file looks like :
home/user1/
home/user1/.profile
home/user1/.bash_history
home/user1/.bash_logout
...
I tried to test the restoration with something like :
tar -xvzf home.user1.tar.gz -C home/user1
But the command above recreate all the structure inside the choosen directory. That gives /home/user1/home/user1/filname1.
So I guess I should use the command specifying the home directory (/home) instead of the user directory. But is there any risk to erase other user's directories in /home ?
Thks for your time.
Actually tar does not erase data as a default. But any files that are contained within the tar archive will overwrite files of the same name if they are already present. Likewise a sub-directory's contents will not be overwritten if the tar archive does not contain files matching them.
mkdir -p foo/bar/
touch foo/file1 foo/bar/file1
tar -cf foo.tar foo/
rm -rf foo
mkdir -p foo/bar/
touch foo/file2 foo/bar/file2
tar -xf foo.tar
ls foo foo/bar/
As once can see both file1 and file2 are present and the newly unarchived directory did not overwrite the old. Here is the output of ls from my system:
foo:
bar file1 file2
foo/bar/:
file1 file2

Backing up directories with Bash

I have a function in my .bashrc that I use to backup files:
backup() {
filename=`date +F-%H-%M`-"$#"
/bin/cp -fr "$#" ~/backup/$filename
}
and I would like to make an alias to easily backup a project in the folder foxhound:
alias backfox="backup /home/projects/Foxhound"
I get the error
/bin/cp: cannot create directory `/home/username/backup/2012-01-23-15-03-/home/projects/Foxhound`: No such file or directory
I usually tar/zip the directory with something like this
tar -czf backup-$(date +-%Y-%m%d-%H%Mh%S).tar.gz $filename
then just mv the tar to the backups directory/file server etc.
It makes it easier than dealing a bunch of directories
You may want to add:
mkdir -p $filename
before the cp line.

how to use relative path in .sh in order to make my tar files include only the folder tar is created from instead of its fullpath

In a mono exe I am creating .sh scripts file containing codes as below
tarcreator.sh:
tar -cvf /data/folder1/folder2/.../xyz.tar /data/...../tarHomeFolder/myTarFolder > /data/logs/tarLogs.txt
....
and the codes continue.
this .sh file is located in /data/...../tarHomeFolder and in my .exe I call it using "bash 'full path of .sh file'"
When I run this code in this way, it creates the tar but when I open the tar I see folders from the root directory.. (data/..../tarHomeFolder/myTarFolder/.....) instead of this I want my tar contain only directory structure of its folder (myTarFolder).
Then I changed the code to :
tar -cvf /data/folder1/folder2/.../xyz.tar myTarFolder > /data/logs/tarLogs.txt
knowing that .sh file is in tarHomeFolder but then my script tells no such file or directory for myTarFolder ..
when I use tar command right in command prompt, using the command
tar -cvf /data/folder1/folder2/.../xyz.tar myTarFolder > /data/logs/tarLogs.txt
it creates the tar directory structure as I want, so, how can I make it do the same in my bash file created and executed by mono .exe ?
All you need to do is change your directory before running tar. Something like this:
# If your full path to the directory is in DIR = "/data/...../tarHomeFolder/myTarFolder"
cd $(dirname $DIR)
tar -cvf /data/folder1/folder2/.../xyz.tar $(basename $DIR) > /data/logs/tarLogs.txt
This will leave you with relative paths, starting with myTarFolder, in your tar file.
Useful references:
basename
dirname
Something like this:
tar -cvf /data/folder1/folder2/.../xyz.tar -C /data/...../tarHomeFolder/ myTarFolder > /data/logs/tarLogs.txt

Resources