Backing up directories with Bash - linux

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.

Related

Bash script to backup modified file in a particular folder structure

I have a folder structure from there i am backing up files which are modified
Below is folder structure
/home/aaditya/customer/jiva/foo/bar/File1.txt
Using below command i want to backup File1.txt file
cd /home/aaditya/customer/jiva/foo/bar
tar -zcvf archive_backup_folder.tar.gz File1.txt
But my problem statement is that when i unzip the tar archive_backup_folder, File1.txt should be there inside customer/jiva/foo/bar/File1.txt not only File1.txt
Can anybody help me how to do that.
Just do:
cd /home/aaditya/
tar -zcvf archive_backup_folder.tar.gz customer/jiva/foo/bar/File1.txt
That is, include the path structure in the archive.
You can try something like this
TIME=`date +%b-%d-%y`
FILENAME=backup-$TIME.tar.gz
SRCDIR= directory or file that has to be backed up
DESDIR= place where you need to store it
tar -cpzf $DESDIR/$FILENAME $SRCDIR
cd /home/aaditya/
find customer -mtime -1 -exec tar -rvf test.tar {} \;
the "find" command will find out all files modified within 1 day and for each of them, run "tar -rvf test.tar the_file" to add it to test.tar (you way want to "rm -f test.tar" before running the "find" command

Script create tar of a Parent directory which will have individual tar's of its sub directories

I want to write a script which will take two parameters
1. Name of Parent directory (ex. mainFolder)
2. The name of the tar to be created
The parent directory will have subfolers (folder1,folder2,folder3 etc).
I need a script which will create "mainFolder.tar" which will consists of "folder1.tar","folder2.tar","folder3.tar" ...etc...
tar cvf mainFolder.jar mainFolder
this will create tar of parent directly "mainFolder" only...Can anybody tell me how can I make mainFolder.tar to create & contain tar's of its sub directories...Need a Shell Script (ex. createTar.sh)which will do it..
while creating tar, only "directories" inside the parent folder "mainFolder" should be considered for tar..and not ".txt " files..
Here is a POSIX compliant solution for your problem. There may be a simpler way (not using any temporary files) but it would involve special and likely non-portable arguments to tar.
#!/bin/sh
startdir="$(pwd)"
mkdir -p parent/horse/frog parent/dog/frog parent/fish/frog
mkdir newparent
(
cd parent
for i in *
do if [ -d "$i" ]
then tar -cf "$startdir/newparent/${i}.tar" ${i}
fi
if [ -f "$i" ]
then cp "$i" "$startdir/newparent/"
fi
done
)
tar -cf newparent.tar newparent

What is the linux command to move the files of subdirecties into one level up respectively

The path structure of the files on my server is similar to shown below,
/home/sun/sdir1/mp4/file.mp4
/home/sun/collection/sdir2/mp4/file.mp4
I would like to move the files of "mp4" into one level up(into sdir1 and sdir2 respectively)
So the output should be,
/home/sun/sdir1/file.mp4
/home/sun/collection/sdir2/file.mp4
I have no idea to do this, so not tried yet anything...
There are different ways to solve your problem
If you just want to move those specific files, run these commands:
cd /home/sun/
mv sdir1/mp4/file.mp4 sdir1/
mv sdir2/mp4/file.mp4 sdir2/
If you want to move all mp4 files on those directories (sdir1 and sdir2), run these commands:
cd /home/sun/
mv sdir1/mp4/*.mp4 sdir1/
mv sdir2/mp4/*.mp4 sdir2/
Edit:
Make a script that iterates all the directories:
Create a script and name it and edit it with your favorite editor (nano, vim, gedit, ...):
gedit folderIterator.sh
The script file content is:
#/bin/bash
# Go to the desired directory
cd /home/sun/
# Do an action over all the subdirectories in the folder
for dir in /home/sun/*/
do
dir=${dir%*/}
mv "$dir"/mp4/*.mp4 "$dir"/
# If you want to remove the subdirectory after moving the files, uncomment the following line
# rm -rf "$dir"
done
Save the file and give it execute permissions:
chmod +x folderIterator.sh
And execute it:
./folderIterator.sh
You can do this:
# move all .mp4 files from sdir1/mp4 to sdir1 directory
user#host:~/home/sun$ mv sdir1/mp4/*.mp4 sdir/
# move all .mp4 files from collection/sdir2/mp4 to collection/sdir2 directory
user#host:~/home/sun$ mv collection/sdir2/mp4/*.mp4 collection/sdir2/
# move only 1 file
user#host:~/home/sun$ mv sdir1/mp4/file.mp4 sdir/
user#host:~/home/sun$ mv collection/sdir2/mp4/file.mp4 collection/sdir2/
I suggest you use find and something like
cd /home/sun/sdir1/mp4/
find . -name "*" -exec mv {} /home/sun/sdir1/ \;
cd /home/sun/collection/sdir2/mp4/
find . -name "*" -exec mv {} /home/sun/collection/sdir2/ \;
Alternatively, you could use tar and something like
cd /home/sun/sdir1/mp4/
tar cfp - * | (cd ../ ; tar xvvf -)
# Make sure everything looks good
rm -rf mp4
cd /home/sun/collection/sdir2/mp4/
tar cfp - * | (cd ../ ; tar xvvf -)
# Make sure everything looks good
rm -rf mp4
The command to move a file (or directory) up one level is:
mv /home/sun/sdir1/mp4/file.mp4 ..
Wildcards can be used to select more files & directories, you can also provide more than one directory at a time.
mv /home/sun/sdir1/mp4/*.mp4 /home/sun/collection/sdir2/mp4/*.mp4 ..

Backup directories in home with tar

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

How to have the cp command create any necessary folders for copying a file to a destination [duplicate]

This question already has answers here:
Linux: copy and create destination dir if it does not exist
(27 answers)
Closed 7 years ago.
When copying a file using cp to a folder that may or may not exist, how do I get cp to create the folder if necessary? Here is what I have tried:
[root#file nutch-0.9]# cp -f urls-resume /nosuchdirectory/hi.txt
cp: cannot create regular file `/nosuchdirectory/hi.txt': No such file or directory
To expand upon Christian's answer, the only reliable way to do this would be to combine mkdir and cp:
mkdir -p /foo/bar && cp myfile "$_"
As an aside, when you only need to create a single directory in an existing hierarchy, rsync can do it in one operation. I'm quite a fan of rsync as a much more versatile cp replacement, in fact:
rsync -a myfile /foo/bar/ # works if /foo exists but /foo/bar doesn't. bar is created.
I didn't know you could do that with cp.
You can do it with mkdir ..
mkdir -p /var/path/to/your/dir
EDIT
See lhunath's answer for incorporating cp.
One can also use the command find:
find ./ -depth -print | cpio -pvd newdirpathname
mkdir -p `dirname /nosuchdirectory/hi.txt` && cp -r urls-resume /nosuchdirectory/hi.txt
There is no such option. What you can do is to run mkdir -p before copying the file
I made a very cool script you can use to copy files in locations that doesn't exist
#!/bin/bash
if [ ! -d "$2" ]; then
mkdir -p "$2"
fi
cp -R "$1" "$2"
Now just save it, give it permissions and run it using
./cp-improved SOURCE DEST
I put -R option but it's just a draft, I know it can be and you will improve it in many ways. Hope it helps you
rsync is work!
#file:
rsync -aqz _vimrc ~/.vimrc
#directory:
rsync -aqz _vim/ ~/.vim
cp -Rvn /source/path/* /destination/path/
cp: /destination/path/any.zip: No such file or directory
It will create no existing paths in destination, if path have a source file inside.
This dont create empty directories.
A moment ago i've seen xxxxxxxx: No such file or directory, because i run out of free space. without error message.
with ditto:
ditto -V /source/path/* /destination/path
ditto: /destination/path/any.zip: No space left on device
once freed space cp -Rvn /source/path/* /destination/path/ works as expected

Resources