LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Ubuntu (https://www.linuxquestions.org/questions/ubuntu-63/)
-   -   how to write a backup script? (https://www.linuxquestions.org/questions/ubuntu-63/how-to-write-a-backup-script-732819/)

joseph2020 06-14-2009 04:28 AM

how to write a backup script?
 
Yet more backup questions. I use
Code:

sudo tar -cvf /media/disk/filedate.tar  /etc
sudo tar -cvf /media/disk/filedate.tar /home/joe

What I want to do is write a short script(mybackup) that will automate that for me, so when I type mybackup it executes the commands AND replaces the "filedate.tar" with "the date of that day".tar. Possibly using some variation of the "date" command, but this is way beyond my capabilities. Any help is appreciated.

Thanks in advance

Disillusionist 06-14-2009 05:08 AM

Quote:

Originally Posted by joseph2020 (Post 3573301)
Yet more backup questions. I use
Code:

sudo tar -cvf /media/disk/filedate.tar  /etc
sudo tar -cvf /media/disk/filedate.tar /home/joe

What I want to do is write a short script(mybackup) that will automate that for me, so when I type mybackup it executes the commands AND replaces the "filedate.tar" with "the date of that day".tar. Possibly using some variation of the "date" command, but this is way beyond my capabilities. Any help is appreciated.

Thanks in advance

Code:

#!/bin/bash
l_date=$(date +%Y%b%d)
tar -cvf /media/disk/file_${l_date}.tar  /etc /home/joe

Script needs to be run via sudo ;)

Telengard 06-14-2009 05:30 AM

You just want to automate those two commands, is that right?

1. open your favorite text editor (e.g. gedit, kate, emacs, or vim)
2. create a new, empty file named backup.sh in your $HOME folder
3. copy and paste those two commands into the file
4. save
5. open a terminal (or Konsole) and enable the execution bit on ~/backup.sh

Example to enable execution:
Code:

chmod 744 ~/backup.sh
Whenever you want to run the backup just open a terminal ( or Konsole) and enter ~/backup.sh

Until something is done for the date part, you'll have to first delete the old backup before running the new one. I may update this post after a while with a more complete solution, or maybe someone else already has one.

Edit: Here's the script I came up with.

Code:

dateFormat='+%F'
tar -cvf /media/disk/etc_`date "$dateFormat"`.tar /etc
tar -cvf /media/disk/home_`date "$dateFormat"`.tar /home/joe

Edit: Fixed a typo in the script. Added the tar extension to backup files.

Quote:

Originally Posted by Disillusionist
Script needs to be run via sudo

I don't see why. Care to enlighten us?

joseph2020 06-14-2009 05:11 PM

Disillusionist, thank you for your post

Telengard, Your help is much appreciated. I think you found the solution, I will try that soon.
Code:

dateFormat='+%F'
tar -cvf /media/disk/etc_`date "$dateFormat"`.tar /etc
tar -cvf /media/disk/home_`date "$dateFormat"`.tar /home/joe

At this point I am trying to learn as much as I can about Linux (sse my signature below). If it's not too much trouble would you please explain what this is doing? I don't really understand how it works. Thanks again

choogendyk 06-14-2009 05:34 PM

Quote:

Originally Posted by joseph2020 (Post 3573768)
Code:

dateFormat='+%F'
tar -cvf /media/disk/etc_`date "$dateFormat"`.tar /etc
tar -cvf /media/disk/home_`date "$dateFormat"`.tar /home/joe

At this point I am trying to learn as much as I can about Linux (sse my signature below). If it's not too much trouble would you please explain what this is doing? I don't really understand how it works.

Start by working with the man pages. `man date` will tell you how the date command works and get you into the format stuff. Note that I used back ticks around `man date`. Those indicate "execute and return the result". That is exactly what they do in the script. So, `date "$dateFormat"` executes the date command with the specified argument and returns the results. You can test those kinds of things by typing them at the command line. So, if you type "date +%F" you will see what the result will be. The text that command returns is exactly what will be inserted in the tar command as part of the name for the tar file. The rest of the tar command you already know.

Note also that scripts normally start with a #! line that tells what program will execute the script. So, the first line should be "#!/bin/bash" as indicated by Disillusionist. It also needs the execute bit set as indicated by Telengard.

Telengard 06-14-2009 10:14 PM

Quote:

Originally Posted by joseph2020 (Post 3573768)
Code:

dateFormat='+%F'
tar -cvf /media/disk/etc_`date "$dateFormat"`.tar /etc
tar -cvf /media/disk/home_`date "$dateFormat"`.tar /home/joe

If it's not too much trouble would you please explain what this is doing?

Sure, no sweat. :)

Code:

dateFormat='+%F'
A simple variable assignment, like in almost any programming language you may think of. In traditional Unix, all shell variables are treated as strings (purely text replacement) unless you pass them to a command which tries to interpret them otherwise. (BASH has some weird ideas of integers but that's offtopic here.) The single quote marks simply protect whatever is in the string from interpretation by the shell. Use them when you want the string passed completely intact to the command. For more info:

Code:

sudo apt-get install bash-doc
(enter your password when prompted)
info '(Bashref)Quoting'


Code:

tar -cvf
You're already familiar with this command, so I'll be brief. tar just takes all the files you give it and copies them together into one big file. Option "c" means create, for making a new archive. Option "v" means verbose, for printing the name of every file added. Option "f" means file, and must be followed by the name of the archive. For more info:

Code:

man tar
and

Code:

sudo apt-get install tar-doc
(enter your password when prompted)
info tar

Code:

date
Prints the date you specify (or the current date if you don't give one) in the format you specify. Dates can be represented in many custom formats thanks to format specifiers which work similar to (but not the same as) those of printf() function in C programming. For more info:

Code:

man date
and

Code:

info '(Coreutils)date invocation'
Here's the part you probably want to understand most.

Code:

`date "$dateFormat"`
It's the date command, and we're passing it the format string we stored in variable "dateFormat" in the first line of the script. The magic is in the "`" (back-tic) marks. It is made by holding the Shift key while typing the "~" (tilde) key. Surrounding any command with a pair of "`" (back-tic) marks, causes the text output (STDOUT I think it is called) to become part of the rest of the command line just as if you had typed it in yourself. In this case, the output of the date command becomes part of the file name of our archive. For more info:

Code:

sudo apt-get install bash-doc
(enter your password when prompted)
info '(Bashref)Command Substitution'

Edit: So why did I use +%F as the date format specifier? Enter the command below to find out :)

Code:

date --help | grep '%F'

Telengard 06-14-2009 10:21 PM

Quote:

Originally Posted by choogendyk (Post 3573782)
Note also that scripts normally start with a #! line that tells what program will execute the script. So, the first line should be "#!/bin/bash" as indicated by Disillusionist. Telengard.

Quite correct. But Bash (or whatever /bin/bash is linked to) is the default command interpreter on the OP's distro, and will execute the script even if not specified. Either way, it works as expected in Ubuntu right now.

The only reason this would matter is if some future release of Ubuntu used a command interpreter incompatible with Bash as the default command interpreter. I don't see that happening anytime soon, but who knows eh?

Disillusionist 06-15-2009 02:00 AM

OK, the reason that I suggested the script should be run as sudo, was that the original commands were run as sudo.

Basically, I didn't think further than assuming there were permissions issues which lead to /etc not being backed up correctly without sudo.

There is no reason to run the script with sudo if this was not a correct assumption.

I cannot see your reasoning behind:
Code:

dateFormat='+%F'
tar -cvf /media/disk/etc_`date "$dateFormat"`.tar /etc
tar -cvf /media/disk/home_`date "$dateFormat"`.tar /home/joe

surely date +%F would be shorter to type than date "$dateFormat"

If both folders are to go to the same backup file, which is what I concluded from the original post, then the simplest thing would be:
Code:

#!/bin/bash
tar cvf /media/disk/backup_$(date +%F).tar /etc /home/joe


Disillusionist 06-15-2009 02:08 AM

Run the backup script without sudo, then run:
Code:

tar tvf /media/disk/backup_$(date +%F).tar etc/shadow etc/hosts
Run the backup script with sudo, then run:
Code:

tar tvf /media/disk/backup_$(date +%F).tar etc/shadow etc/hosts

joseph2020 06-15-2009 02:29 AM

Telengard, Thank you so much for that great explanation. I did know some of that, but I also learned a lot. I admire your ability to explain things in a clear and easily digestible format. I will be saving that as one of my "linux lesson" pages.

As far as I can tell your solution will work, so this will be my last post on this topic, as I consider the problem resolved. Please feel free to answer any of my future questions in the same robust manner. Thanks again!

Telengard 06-15-2009 01:00 PM

Quote:

Originally Posted by Disillusionist (Post 3574047)
OK, the reason that I suggested the script should be run as sudo, was that the original commands were run as sudo. Basically, I didn't think further than assuming there were permissions issues which lead to /etc not being backed up correctly without sudo.

That makes good sense, and I don't say you're wrong. It is true that there are permissions problems when trying to archive /etc with non-root user.

IMHO, it is not necessary, or even desirable, to archive every file and folder in /etc. Most of those configuration files will be generated automatically when the software is re-installed. IMHO, a better practice is to archive only configurations for programs that have complicated settings or if they are configured by hand in an editor. If you have spent hours and days working on the perfect smb.conf then you certainly want to keep it archived!

Quote:

Originally Posted by Disillusionist (Post 3574047)
surely date +%F would be shorter to type than date "$dateFormat"

Yes it would be shorter in your script, but only because your script only uses a single target backup file. In my script, I use multiple target backup files. If I were writing the script for my own use, that's what I would want.

If future revisions of my script used a different date format, then I would only have to change the date format on one line.

Telengard 06-15-2009 01:21 PM

Quote:

Originally Posted by joseph2020 (Post 3574075)
Telengard, Thank you so much for that great explanation. I did know some of that, but I also learned a lot. I admire your ability to explain things in a clear and easily digestible format. I will be saving that as one of my "linux lesson" pages.

You are welcome, of course. But I really don't feel worthy of such praise. :redface:

It has taken a long time and a great deal of study for me to learn the little bit that I know. Read those manuals I showed you, and keep practicing, and you'll be writing scripts like a pro before you know it. :study:

Quote:

Originally Posted by joseph2020 (Post 3574075)
As far as I can tell your solution will work, so this will be my last post on this topic, as I consider the problem resolved. Please feel free to answer any of my future questions in the same robust manner. Thanks again!

The script I gave you can not be considered a complete solution! It is your responsibility as system administrator to understand how the script works, and tailor it to fit your own needs. Especially, I strongly encourage you to carefully read and understand the valid criticisms already pointed out in this thread by choogendyk and Disillusionist.

Telengard 06-15-2009 01:36 PM

Quote:

Originally Posted by Disillusionist (Post 3574059)
Run the backup script without sudo, then run:
Code:

tar tvf /media/disk/backup_$(date +%F).tar etc/shadow etc/hosts
Run the backup script with sudo, then run:
Code:

tar tvf /media/disk/backup_$(date +%F).tar etc/shadow etc/hosts

Is it really necessary to include those in the backup file? Why or why not?


All times are GMT -5. The time now is 02:52 PM.