It is currently Fri May 24, 2013 10:42 pm

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Backup script
PostPosted: Fri Apr 06, 2007 8:12 am 
Master Zenwalker
Master Zenwalker
User avatar

Joined: Fri Feb 09, 2007 8:40 pm
Posts: 529
Location: Romania
Hi,
just made a simple backup script and I want to know how stable it is or if it could be better. This is a beta so, use it at you own risk. It works nice on my PC. Also keep in mind that, even thow I have a solid base in programming, this is my first "serious" bash script, and it took a few hours to search syntax related stuff (don't throw stones at me if something goes wrong, even thow if you don't enter abnormal configuration stuff it shouldn't)

Please feel free to post any personal requirements or if you see any problems and/or ways to make it better.

hbackup v0.1b2
How to use:
- open it with your favorite text editor and modify the settings for your needs
- copy it where you want. For easy acces, put it somewhere in /usr/local/bin or /usr/bin
- make sure it is executable: chmod +x hbackup
- run it as root or regular user (it will ask you for password ans runs itself as root)
- write "y" to begin, "n" to exit, "d" to output the settings
- to use in a script, run as "hbackup --supress" ; this way it will not ask you anything and will overwrite if backup file already exists. You must be root to use this.
- to restore, use
Code:
tar xvjf backup_name -C /

if you used compression=1, or
Code:
tar xvf backup_name -C /

for compression=0
I haven't yet tested it on any vital directory, like /etc

About:
- it can compress in tar.bz2 to save space
- can be runned from script or more "intuitive"
- if not runned with "--supress",  askes before overwriting
- if not runned with "--supress", enters "root-mode" without the need to start the script as root. It will ask you for the password and continue
- run "hbackup ?" for more options

Download: here


Last edited by energiya on Fri Apr 06, 2007 8:35 am, edited 1 time in total.

 Profile Send private message  
 
 Post subject: Re: Backup script
PostPosted: Fri Apr 06, 2007 5:38 pm 

I am using such a kind script for my daily backups.
There is one extra thing in the script I am using; it is checking if already a daily backup is made.
This way you can add the script to /etc/rc.d/rc.local ;)

Here is my script, simple but working nicely already here for some time:

Code:
#!/bin/sh

bu_path=/data/backups/diversen          # path for backup
bu_name=$(date '+%y%m%d')_backup_PC1    # name of backup
archive=gz                              # empty, bz2 or gz. archive is made and the backup directory will be removed

# Define the files which should be copied into backup
backupfiles=(
/etc/fonts/fonts.conf
/etc/fstab
/etc/group
/etc/hosts
/etc/locales.gen
/etc/rc.conf
/etc/network-profiles/eth0
/etc/network-profiles/wlan0
/etc/pacman.conf
/etc/pacman.d
/etc/rc.d
/etc/rc.local
/etc/rc.local.shutdown
/etc/resolv.conf
/etc/sane.d/plustek.conf
/etc/udev/rules.d/sane.rules
/etc/wpa_supplicant.conf
/etc/X11
/home/jan
/usr/share/blackbox
)

######################################################

# Check if everything is ok and set internal parameters

org=$(pwd)
backuproot=$bu_path/$bu_name

if [ "$archive" = "bz2" ]; then
  compprog=bzip2
elif [ "$archive" = "gz" ]; then
  compprog=gzip
else
  archive=""
fi

if [ "$bu_path" = "" ]; then
  echo "ERROR: The backup root directory name must be filled in before using this script."
  echo "Please edit the script parameters before using this script."
  exit 1
fi

if [ "$bu_name" = "" ]; then
  echo "ERROR: The backup name must be filled in before using this script."
  echo "Please edit the script parameters before using this script."
  exit 1
fi
 
if [ -d $backuproot ]; then
  echo "Backup directory $backuproot does already exist."
  if [ "$1" = "--force" ]; then
    rm -rf $backuproot
    echo "Old directory $backuproot is removed because of using -force"
  else
    echo "Please rename to make a new backup, or use --force to overwrite"
    exit 1
  fi
fi

if [ -f $backuproot.tar.$archive ]; then
  echo "Backup file $backuproot.tar.$archive does already exist."
  if [ "$1" = "--force" ]; then
    rm -rf $backuproot.tar.$archive
    echo "Old backup file $backuproot.tar.$archive is removed because of using --force"
  else
    echo "Please rename to make a new backup, or use --force to overwrite"
    exit 1
  fi
fi

# Make new backup directory
mkdir -p $backuproot

# Make the backup
echo -n "Copy files into backup directory..."

cnt=${#backupfiles[@]}
for (( i=0 ; i < cnt ; i++ ))
do
  if [ -d ${backupfiles[$i]} ]; then
    cp -afr --parent ${backupfiles[$i]} $backuproot
  elif [ -f ${backupfiles[$i]} ]; then
    cp -af --parent ${backupfiles[$i]} $backuproot
  fi
done

echo "OK"

# Make archive if wanted
if [ "$archive" != "" ]; then
  cd $bu_path
  echo -n "Making archive..."
  tar -cvf $bu_name.tar $bu_name &> /dev/null
  $compprog $bu_name.tar
  sleep 0.5
  rm -rf $bu_path/$bu_name
  rm -rf $bu_path/$bu_name.tar
  cd $org
  echo "OK"
fi


  
 
 Post subject: Re: Backup script
PostPosted: Fri Apr 06, 2007 6:34 pm 

Code:
/etc/pacman.conf
/etc/pacman.d

Please, tell to the users that those files are arch linux related ;)

++


  
 
 Post subject: Re: Backup script
PostPosted: Fri Apr 06, 2007 6:53 pm 

You are right.
But the idea is to make your own file choices there ;)

Jan


  
 
 Post subject: Re: Backup script
PostPosted: Fri Apr 06, 2007 6:53 pm 
Master Zenwalker
Master Zenwalker
User avatar

Joined: Fri Feb 09, 2007 8:40 pm
Posts: 529
Location: Romania
Well... I prefer mine because it saves a full path so I can read easily any file in the archive, and it restores them to the exact path. And I have a lot of files, because I use it to backup my home folder and /etc, and the resulting archive has more than 700MB, due to the fact that I keep all my projects there... more than 10.000 files. I wouldn't want to erase any of them by accident...  ;)


 Profile Send private message  
 
 Post subject: Re: Backup script
PostPosted: Fri Apr 06, 2007 6:56 pm 

Mine is doing the same energiya; for example my complete userdirectory is stored by adding /home/jan

Here is the part doing the work:
Code:
cnt=${#backupfiles[@]}
for (( i=0 ; i < cnt ; i++ ))
do
  if [ -d ${backupfiles[$i]} ]; then
    cp -afr --parent ${backupfiles[$i]} $backuproot
  elif [ -f ${backupfiles[$i]} ]; then
    cp -af --parent ${backupfiles[$i]} $backuproot
  fi
done


Btw, file rights, ownerships etc... are kept ;)

But ofcourse you are free to use your own script.
I added mine because it is using the date in the name of the archive and is checking if a backup is already there. Only two lines of script, I know, but in my case very usefull ;)

Jan


  
 
 Post subject: Re: Backup script
PostPosted: Fri Apr 06, 2007 7:10 pm 
Master Zenwalker
Master Zenwalker
User avatar

Joined: Fri Feb 09, 2007 8:40 pm
Posts: 529
Location: Romania
Yes, you are right. Well, as posted in the initial post, this is my first "serious" bash script, so I'm still not used to this scripting language. It seems... I don't know... weird, not to use functions and other similar structures, but hey! we learn something new enery day ;)

I'll still try and modify my script and make it better (even thow I will probably be the only one using it), just as a way to learn this scripting language  ::). It is usefull, as a linux user...

Btw, Lontronics, I hope you don't mind if I use some things from your script  ;D

edit: does my script look safe to use? Long term speaking... I'll probably add incremental backing-up, just to see how it works


Last edited by energiya on Fri Apr 06, 2007 7:13 pm, edited 1 time in total.

 Profile Send private message  
 
 Post subject: Re: Backup script
PostPosted: Fri Apr 06, 2007 8:03 pm 

Your script is nice and I think it is safe to use.

Ofcourse you can use what you want, no problem at all ;)

Jan


  
 
 Post subject: Re: Backup script
PostPosted: Fri Apr 06, 2007 8:17 pm 
Master Zenwalker
Master Zenwalker
User avatar

Joined: Fri Feb 09, 2007 8:40 pm
Posts: 529
Location: Romania
Lontronics wrote:
Your script is nice and I think it is safe to use.

Ofcourse you can use what you want, no problem at all ;)

Jan

Thanks! I'll try making this script a project on my web page. I'm struggling to make it live again, after my usual host had a fatal hardware failure, and they didn't have a backup script :D.


 Profile Send private message  
 
 Post subject: Re: Backup script
PostPosted: Thu Apr 12, 2007 12:42 pm 
Experienced Zenwalker
Experienced Zenwalker

Joined: Wed Mar 07, 2007 4:13 pm
Posts: 122
I use this kind of solution:

http://www.mikerubel.org/computers/rsync_snapshots/

It's very clever because it minimizes the need for diskspace completely transparent to the user :) I'm able to keep one month worth of old file versions in case I accidently break something and don't notice it immediately. It doesn't really require a remote server even though it uses rsync. To be of any use it does require two hard-disks though.


Last edited by alienDog on Thu Apr 12, 2007 12:44 pm, edited 1 time in total.

 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


 Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: