LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 08: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, 08:45 PM   #2
Adol
Member
 
Registered: Feb 2011
Location: Osaka, Japan
Distribution: Gentoo, Opensuse
Posts: 271

Rep: Reputation: 6
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 08:47 PM.
 
Old 10-19-2012, 09:45 PM   #3
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 18.04-14.04, Scientific Linux 6.3-6.4, Android-x86, Pretty much all distros at one point...
Posts: 1,802

Rep: Reputation: 157Reputation: 157
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-19-2012, 11:23 PM   #4
Adol
Member
 
Registered: Feb 2011
Location: Osaka, Japan
Distribution: Gentoo, Opensuse
Posts: 271

Rep: Reputation: 6
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-19-2012, 11:54 PM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

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

Rep: Reputation: 286Reputation: 286Reputation: 286
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, 03:50 PM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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, 07:17 AM   #8
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
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, 07: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, 09:55 AM   #10
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

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



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 05:20 AM
LXer: Back In Time, Automatic Back-Up Tool For Linux LXer Syndicated Linux News 0 12-12-2011 09:21 AM
How To : File Transfers from Linux to Windows and Back mavv Linux - Software 2 08-14-2006 02:23 AM
Get back a file from a crashed linux system ust Linux - Software 8 05-26-2005 01:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:36 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
Open Source Consulting | Domain Registration