LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   back up a file in linux (http://www.linuxquestions.org/questions/linux-newbie-8/back-up-a-file-in-linux-4175433148/)

demet8 10-19-2012 08:32 PM

back up a file in linux
 
What is the best way to write a small bash script that will make a backup of a file giving it a .bak’ extension? I am getting a lot of examples but most of them are extremely long and I believe not really what I am looking for.

Adol 10-19-2012 08:45 PM

Quote:

Originally Posted by demet8 (Post 4810394)
What is the best way to write a small bash script that will make a backup of a file giving it a .bak’ extension? I am getting a lot of examples but most of them are extremely long and I believe not really what I am looking for.

Here is an example from the xargs page( using find and xargs to to what you want:

Code:

$ find . -name "(file you want to change)" -print0 | xargs -0 -I {} mv {} ~/(what you want the file to be)
Basically you find the file your looking for with the find command and then piping the result into xargs to move it. If you want to copy change:
Code:

mv
to
Code:

cp
For actually making a bash script just Google it. There are many resources for creating bash scripts.

Also, if you want to have it automated based on time take a look at cron scripts. I think you can also have it backup whenever there is a change to the file but Im not sure how that would be done.

JaseP 10-19-2012 09:45 PM

I really don't understand the question... what can be that much harder than???;

Code:

cp filename.ext filename.ext.bak
You can use wildcards/regular expressions to make it generic...
Is the goal to make it automatic? In that case you just create a cron job...
Also, for backups, you have rsync and other utilities.

Adol 10-19-2012 11:23 PM

Quote:

Originally Posted by JaseP (Post 4810429)
I really don't understand the question... what can be that much harder than???;

Code:

cp filename.ext filename.ext.bak
You can use wildcards/regular expressions to make it generic...
Is the goal to make it automatic? In that case you just create a cron job...
Also, for backups, you have rsync and other utilities.

Thats much better. Not sure why I even suggested the find command if you know exactly what file you want to backup.

shivaa 10-19-2012 11:54 PM

Here's a simple bash script:
Code:

#!/bin/bash
savepath="/home/jack"                        ## path where you want to save the backup file
echo -n "Enter full path of file: "          ## path where file is residing
read filepath
echo -n "Enter filename to be backup: "      ## filename to be backup
read file
cd $filepath
if [ -f $file ];                            ## checking existance of file
then
cp -p $file $savepath/$file.`date +%Y%m%d`  ## Save the file in date of backup format, i.e. file.20121020
cp -p $file $savepath/$file.bck              ## Save the file in .bck format under the path $savepath
echo "Backup done for $file successfully"
else
echo "File $file not found"
fi


shivaa 10-20-2012 12:01 AM

If you want regularly backup of the file, then better save it in date format, not in .bck format, else it will keep the file overriding. But there will be no such pb with date format.

schneidz 10-20-2012 03:50 PM

Quote:

Originally Posted by demet8 (Post 4810394)
What is the best way to write a small bash script that will make a backup of a file giving it a .bak’ extension? I am getting a lot of examples but most of them are extremely long and I believe not really what I am looking for.

um,
Code:

cp file.txt file.bak
:confused:

arizonagroovejet 10-21-2012 07:17 AM

To expand on what JaseP said
Code:

case:foo mike$ ls -l
total 0
-rw-r--r--  1 mike  wheel  0 21 Oct 13:14 blah.txt
case:foo mike$ cp blah.txt{,.bak}
case:foo mike$ ls -l
total 0
-rw-r--r--  1 mike  wheel  0 21 Oct 13:14 blah.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 13:14 blah.txt.bak
case:foo mike$

Code:

case:moo mike$ ls -l
total 0
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 four.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 one.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 three.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 two.txt
case:moo mike$ for i in *;do cp ${i}{,.bak};done
case:moo mike$ ls -l
total 0
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 four.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 four.txt.bak
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 one.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 one.txt.bak
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 three.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 three.txt.bak
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 two.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 13:16 two.txt.bak
case:moo mike$

I don't think I really understand what you're asking either.

Mr liu 10-21-2012 07:40 AM

I think "cp" is better . mv *.ext (what you want).ext.txt.

arizonagroovejet 10-21-2012 09:55 AM

Come to think of it that second example of mine fails if there's spaces in the names of the files. And some people will keep creating files with spaces in the names ;)
Code:

case:boo mike$ ls -l
total 0
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 four file.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 one file.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 three file.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 two file.txt
case:boo mike$ for i in *;do cp "${i}"{,.bak};done
case:boo mike$ ls -l
total 0
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 four file.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 four file.txt.bak
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 one file.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 one file.txt.bak
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 three file.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 three file.txt.bak
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 two file.txt
-rw-r--r--  1 mike  wheel  0 21 Oct 15:53 two file.txt.bak



All times are GMT -5. The time now is 08:29 AM.