Simple & Easy Back-Up Scripts

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
Fred

Simple & Easy Back-Up Scripts

Post by Fred »

Greetings & Salutations,

I am sure many of you are beginning to think about the problems you may have transferring your data and hidden config files to your new upgrade install. Others may just need to be able to back up their system so if it breaks they can put it back like it was with out loosing data and programs you have installed and configured.

What I am going to show you is how to make several very simple shell scripts that you can use to back up your system. They aren't intended to be fancy or take the place of programs that make live cd's. In the Unix/Linux style, they do only one thing and they do it well.

For the programmers out there these are very basic "Hello World" type one line scripts, so don't look for elegant, flash of genius scripts. You won't find them here. They were kept as simple as possible for a reason. :-)

A shell script is nothing more than one or more commands in a text file. In this case we will be using rsync commands. rsync is a very powerful and flexible command line tool to make back ups.

First let's look at how we would make a simple text file into an executable that we can call directly from the command line just by typing the filename and hitting enter.

Let's say we have a text file called bakconfig which we have saved into a hidden folder called .ShellScripts, which we have created in our /home directory to save our shell scripts in. We need to do two things to this file to magically make it work like a program. First we need to set the permissions of the file so we can execute it. Open a terminal and type:

chmod 755 /home/fred/.ShellScripts/bakconfig

The correct permissions are now set. Next we need to put a copy of the file in our default path so the computer can find it. To do this we would copy our text file into the /bin directory, which is by default in out path. We can do this easily from the command line. Type in a terminal:

sudo cp -a /home/fred/.ShellScripts/bakconfig /bin

That's all there is to it. We have turned our simple text file into a executable script that can be run from the command line by typing bakconfig and pressing enter.

OK.. let's write a simple script to back up all our hidden configuration files and directories from our /home/fred folder. These files hold all the configurations for the programs we have installed, including our desktop settings, our bookmarks, and our e-mail. We are going to make a mirror image of these files and directories in a directory called BackUpConfig on sdb5 partition which we will assume is on another drive on our system or possibly an external drive. We could compress these files so they wouldn't take up as much space but we won't because they will be easier to access piecemeal when we upgrade if left uncompressed. Open a text editor and off we go.

#
# Backs up hidden config files/folders in /home/fred to /media/sdb5/BackUpConfig/BackUpConfig directory
#
# Save as /home/fred/.ShellScripts/bakconfig
#
# Set permissions with: chmod 755 /home/fred/.ShellScripts/bakconfig
#
# Copy to /bin with: sudo cp -a /home/fred/.ShellScripts/bakconfig /bin
#
# Execute by typing in a terminal: bakconfig
#
sudo rsync -ar --progress --delete --timeout=240 --include ".*" --include ".*/**" --exclude "*" /home/fred/ /media/sdb5/BackUpConfig/BackUpConfig
#
printf "
Fred, your config files have been backed up successfully!

To Restore your config files type:
rsync -ar --progress --include "*" --include "*/**" /media/sdb5/BackUpConfig/BackUpConfig/ /home/fred

"
#
exit
# End

We are done. You will need to create a folder on your backup partition called, in this case, BackUpConfig or whatever you decide to call it. The same is true for all the scripts that follow also. Be careful when changing the file/folder names to apply to you. Every space, dash, quote, etc. in the uncommented lines is important. Close only counts in horse shoes and hand grenades. :-)

This script is good for saving your config files to a data partition or somewhere out of your install so you will have them available to you at upgrade time. This script makes a mirror image. When something is added or deleted from your /home config files the changes will be made in your backup the next time you run the script. If you wanted, you could set it up on a crontab to do it automatically, but that, we will leave for a another day. Remember, running the restore command will replace all the config files with the ones backed up.


The next script backs up all the data files/folders in your /home directory except the hidden files. This is nice because it will extract them from your install, even if you have it all in one partition. It can be used on a /home partition or on a combined / partition. It is a quick easy way to back up your data if you don't have it in data partitions.

CAUTION!!! If you have data partitions mounted in your /home it will copy all that data and put it into your back up too. This is generally not good. If you have some data partitions in your /home/fred directory that you don't want pulled into your back up be sure to fix the script to unmount them before you run this script.

#
# Backs up data files/folders in /home/fred to /media/sdb5/BackUpHome/BackUpHome directory
#
# Save as /home/fred/.ShellScripts/bakhome
#
# Set permissions with: chmod 755 /home/fred/.ShellScripts/bakhome
#
# Copy to /bin with: sudo cp -a /home/fred/.ShellScripts/bakhome /bin
#
# Execute by typing in a terminal: bakhome
#
# Uncomment and correct any unmount commands that might
# apply to you. Examples:
#
# sudo umount /home/fred/My_Data
# sudo umount /home/fred/Multimedia
# sudo umount /home/fred/Pictures
#
sudo rsync -ar --delete --progress --timeout=240 --exclude ".*" --exclude ".*/**" --include "*" --include "*/**" /home/fred/ /media/sdb5/BackUpHome/BackUpHome
#
# Remount any partitions that were unmounted above. Example:
# sudo mount -a /dev/sda6 /home/fred/My_Data
#
printf "
Fred, your /home/fred data files have been backed up successfully!

To Restore your /home data type:
sudo rsync -ar --progress --include "*" --include "*/**" /media/sdb5/BackUpHome/BackUpHome/ /home/fred

"
#
exit
# End


The last script for today will back up your Windows install to /media/sdb5/BackUpWin/BackUpWin.

#
# Backs up files/folders in Windows on sda1 to /media/sdb5/BackUpWin/BackUpWin directory
#
# Save as /home/fred/.ShellScripts/bakwin
#
# Set permissions with: chmod 755 /home/fred/.ShellScripts/bakwin
#
# Copy to /bin with: sudo cp -a /home/fred/.ShellScripts/bakwin /bin
#
# Execute by typing in a terminal: bakwin
#
sudo rsync -ar --progress --delete --timeout=240 --include "*" --include "*/**" /media/sda1/ /media/sdb5/BackUpWin/BackUpWin
#
printf "
Fred, your Windows files/folders have been backed up successfully!

To Restore your Windows type:
sudo rsync -ar --progress --delete --include "*" --include "*/**" /media/sdb5/BackUpWin/BackUpWin/ /media/sda1

"
#
exit
# End

When you do back ups make sure the partition you are backing up to has enough space in it to hold the amount of data you expect to save. Also remember that both the source and the destination of the file transfers must be mounted when you run these scripts or their restore commands.

To remind you again, you must change the partition/file/folder designations in the above scripts to work with your system. When you do this be very careful not to make mistakes. A missing space or an extra dash, for example, is the difference between working as expected and not working. Don't mistake word wrap in your editor with returns.

It is best to run your back up scripts on a regular basis to keep your back ups, up to date.

Enjoy, :-)

Fred

PS: There is a lot of material here. If anyone spots a mistake or typo, please call my attention to it so I can correct it. We wouldn't want to mislead anybody. Thanks!
Last edited by Fred on Tue May 20, 2008 5:14 am, edited 7 times in total.
Fred

Re: Simple & Easy Back-Up Scripts

Post by Fred »

Ed,

Thanks for reading and commenting.
I usually prefer to set up a wheattab :lol: (just after the 1st script).
lol... I like whole wheat better also. It is healthier too.
I have been meaning to learn to spell, just haven't gotten around to it yet. :-)
isn't it more practical to modify the PATH variable to add '~/.Shellscripts/'
Yes, and also a lot more orthodox. The reason I didn't was because it would be another concept I would have to explain and show how to implement. These scripts are already, at best, unorthodox. I was trying to be as simple as possible and still work, at the expense of good technique and practices. Understandable was a key goal. There are a number of things I would have done differently if I were trying to show off my skills, such as they are. :-)
what happens if /media/sdb5 is an external disk and a modification is made in /home while it is unplugged ?
It shouldn't matter, as long as the external drive is mounted when the script is run.
In the second script, maybe add the corresponding 'mount' commands at the end so that it does it automatically ?
Excellent catch and a gross oversight on my part. Done... :-)

Thanks again for your comments. They were very much appreciated.

Fred
Fred

Re: Simple & Easy Back-Up Scripts

Post by Fred »

Either these scripts are so good and well explained that nobody has had a problem with them, which I doubt, or no one has tried any of them them yet. :-)

Don't let the fact that they are command line scripts scare you away. Once you get into it, you will find them quite simple.

Fred
idiotkiwi
Level 2
Level 2
Posts: 57
Joined: Fri Feb 02, 2007 2:44 am
Location: Christchurch New Zealand

Re: Simple & Easy Back-Up Scripts

Post by idiotkiwi »

Fred
Much appreciated!!

I tried the original backup home version with the delete activated and not only backed up, but managed to clean out some of the residual files which were annoying me. Wine seems particularly hard to get rid of.

I am looking forward to trying a number of your other "How To's" now the back up is safe.

Thanks & regards for all the work of yours & others.

Euan
Fred

Re: Simple & Easy Back-Up Scripts

Post by Fred »

idiotkiwi,

You are quite welcome. I am glad you found it helpful.

As you noticed, I removed the delete command from the restore for fear it might have some unintended consequences for some users. Just remember, when using the delete command that it removes anything in the target directory that isn't in the source directory.

You might want to backup your config files too. That is where your bookmarks and e-mail files are, as well as your desktop configuration. Be sure and up date your first script to add the extra directory. This is important if you are trying to put your backups in the same parent folder. You don't want one script to delete the other's data. Unintended consequence of the delete command. :-)

Fred
User avatar
T J Tulley
Level 5
Level 5
Posts: 558
Joined: Wed Jul 18, 2007 10:18 am
Location: Hull, England

Re: Simple & Easy Back-Up Scripts

Post by T J Tulley »

This is a late attempt to start scheduled backups, using cron and rscript. Up to now I've been making simple copies "by hand" to a separate internal disk - not at all regularly.
Long ago I read (and printed) Fred's Howto describing Bakclean v. 1.4b - now I fear that I need to write my own scripts - and the manual of rscript is opaque to me!

In the simple scripts in this topic, the key command line appears to be:
sudo rsync -ar --progress --delete --timeout=240 --include ".*" --include ".*/**" --exclude "*" /home/fred/ /media/sdb5/BackUpConfig/BackUpConfig

or
sudo rsync -ar --delete --progress --timeout=240 --exclude ".*" --exclude ".*/**" --include "*" --include "*/**" /home/fred/ /media/sdb5/BackUpHome/BackUpHome

The files I need to back up are "all over the place" and I guess I need a separate command for each. A major exercise will be to move them, which would simplify backup procedure - but for the time being can I use, for example:

sudo rsync -ar --delete --progress --timeout=240 /media/sda1/Documents*and*Settings/Theo*Tulley/My*Pictures /media/sdb1/BkpJ6NSch

Can I expect this to back up "My Pictures" (which is a folder containing many sub- and sub-sub-folders, totalling 2 GiB) to a folder BkpJ6NSch in sdb1? The destination filename includes the coded date of commencement; I expect to use the same file for all backups for a period (a year?), and then start again.
Will it subsequently back up only those items which are new or changed since the previous backup?
Will it delete items which it replaces in the backup folder? (I used scheduled backups in Windows, and the files continually grew!).

I shall be most grateful for advice.

The partition sda1 is labelled "(E)DI8:H7R" and sdb1 is labelled "Backups", these are also the names of their mount points and presumably will need to appear in the command? I know the brackets cause problems, but I've learned to cope with this in /etc/fstab.
Yours hopefully -

Theo Tulley.
Using a PC with 2GB RAM, 3 hdds and a 1.7 GHz Celeron cpu.
Post Reply

Return to “Tutorials”