LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 10-19-2012, 09:32 PM   #1
demet8
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Rep: Reputation: Disabled
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.
 
Old 10-19-2012, 09:45 PM   #2
Adol
Member
 
Registered: Feb 2011
Location: Osaka, Japan
Distribution: Gentoo, Opensuse
Posts: 248

Rep: Reputation: 4
Quote:
Originally Posted by demet8 View Post
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.

Last edited by Adol; 10-19-2012 at 09:47 PM.
 
Old 10-19-2012, 10:45 PM   #3
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 10.04/12.04, Scientific Linux 6.3, Android-x86, Maemo
Posts: 1,658

Rep: Reputation: 138Reputation: 138
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.
 
Old 10-20-2012, 12:23 AM   #4
Adol
Member
 
Registered: Feb 2011
Location: Osaka, Japan
Distribution: Gentoo, Opensuse
Posts: 248

Rep: Reputation: 4
Quote:
Originally Posted by JaseP View Post
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.
 
Old 10-20-2012, 12:54 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
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
 
Old 10-20-2012, 01:01 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
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.
 
Old 10-20-2012, 04:50 PM   #7
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,217

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Quote:
Originally Posted by demet8 View Post
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
 
Old 10-21-2012, 08:17 AM   #8
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,032

Rep: Reputation: 174Reputation: 174
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.
 
Old 10-21-2012, 08:40 AM   #9
Mr liu
LQ Newbie
 
Registered: Sep 2012
Location: China
Posts: 2

Rep: Reputation: Disabled
I think "cp" is better . mv *.ext (what you want).ext.txt.
 
Old 10-21-2012, 10:55 AM   #10
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,032

Rep: Reputation: 174Reputation: 174
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
 
  


Reply

Tags
bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Back Up File Storage: Free Nas or a Linux Distro and Samba ? brian2 Linux - Newbie 5 01-04-2012 06:20 AM
LXer: Back In Time, Automatic Back-Up Tool For Linux LXer Syndicated Linux News 0 12-12-2011 10:21 AM
How To : File Transfers from Linux to Windows and Back mavv Linux - Software 2 08-14-2006 03:23 AM
Get back a file from a crashed linux system ust Linux - Software 8 05-26-2005 02:27 AM


All times are GMT -5. The time now is 10:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration