Bash script for files to backup..

Need help with C, C++, perl, python, etc?

Bash script for files to backup..

Postby evandenbroecke » 2017-01-18 16:52

EDITED
A Bash script that copies files from a list to your backup location with parent directories in tack. Set configuration at top and run. Ment to be run from users home dir. but you can change location to which ever you like with "cd" in the script.

Code: Select all
#!/bin/bash

# remember the current location
c_dir=$PWD

##### CONFIG #####

bwd=/some/location  # Location of backup drive
host=mycomputer      # Machine name
bname=backup          # Name of Backup folder
# Files to backup one file
# per line, not delete quotes.
files="
.bin/
.bashrc
.profile
.bash_aliases
.bash_history
.file1
.file2
.somedir/
.some/place/to/file1
.some/other/place/to/file2
.other/place/
"

# Color warnings
bold="\033[1m"
warning="\033[33m"
error="\033[1;31m"
textoff="\033[0m"

# working location
cd $HOME

##### CONFIG #####

bdir=$bwd/$bname/$host/$USER

echo -e "$bold Backing up .dot files$textoff"
echo -e "\t\033[1mBackup location\033[0m: $bdir"

if [ -x $bwd/$bname/ ]; then
  if [ ! -d $bdir ]; then
    echo -e "\t Creating backup directories"
    mkdir -p $bdir
  fi
if [ -d $bdir ]; then
  for file in $files ; do
    if [ -f $file ]; then
      echo -e "\t\t Backing up "$bold"$file"$textoff" file..."
      cp --parent $file $bdir
      echo -e "\t\t   "$bold" "done."$textoff"
    elif [ -d $file ]; then
      echo -e "\t\t Backing up "$bold"$file"$textoff" directory..."
      cp --parent -r $file $bdir
      echo -e "\t\t   "$bold" "done."$textoff"
    else echo -e "\t\t    "$warning"Warning"$textoff": "$bold"$file"$textoff" does not exist, skipping..."
    fi ;
    done
  else
    echo -e "\n\t "$error"ERROR"$textoff": Cannot create the backup folder."
  fi
else echo -e "\n\t "$error"ERROR"$textoff": Cannot create backup location, Storage location is not accessible."
fi
cd $c_dir
unset error textoff bold files file bdir bwd bname host c_dir


Thanks,
Last edited by evandenbroecke on 2017-01-19 19:23, edited 6 times in total.
Todd Vandenbroecke
evandenbroecke@gmail.com
User avatar
evandenbroecke
 
Posts: 32
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

Re: Bashing a dot file backup script :) [help please]

Postby cpoakes » 2017-01-18 19:07

Two suggestions: 1) check out the --parent option for cp, or 2) manually populate the backup location with your directories/files and then use:

Code: Select all
rsync -a --existing --update $HOME/ /backup/folder/location

to keep them updated. With method 2, you don't need to keep a file list - simply populate the destination directory. Caveats: don't overlook the trailing slash on the source directory ($HOME); and very deep (10,000 file) home directories incur a penalty performance often alleviated by an --exclude pattern. Check the man pages for details.
User avatar
cpoakes
 
Posts: 94
Joined: 2015-03-29 04:54

Re: Bashing a dot file backup script :) [help please]

Postby evandenbroecke » 2017-01-18 19:32

Looked at them options already... execpt I just want to copy useful files, such as configuration and rc files. copying the directory they are in only helps me remember where they are located in my home directory. the --parent option is good, but wanted to remove all the extra directories...
Todd Vandenbroecke
evandenbroecke@gmail.com
User avatar
evandenbroecke
 
Posts: 32
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

Re: Bashing a dot file backup script :) [help please]

Postby evandenbroecke » 2017-01-18 19:53

what about a for loop nested inside with a directive to capture the subdirectories and make them with "mkdir -p $file"?

just dont know the syntax to get everything before the file name in my $file string.
Todd Vandenbroecke
evandenbroecke@gmail.com
User avatar
evandenbroecke
 
Posts: 32
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

Re: Bashing a dot file backup script :) [help please]

Postby evandenbroecke » 2017-01-19 03:23

How would I get the following to omit the first entry in this list? ie: a normal file not in a sub-directory.

Code: Select all
bdir=/some/place

file="
.bashrc
.config/testfile.ini
.config/vlc/here/somefilerc
"

for i in $file ; do
        echo $i | sed -e 's_/[^/]*$__' ;
done



I'm just using this as a test, going to replace "echo $i" with "mkdir -p $file" in my script above...
Todd Vandenbroecke
evandenbroecke@gmail.com
User avatar
evandenbroecke
 
Posts: 32
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

Re: Bashing a dot file backup script :) [help please]

Postby pylkko » 2017-01-19 09:53

What about:
http://search.cpan.org/~dmuey/File-Copy-Recursive-0.38/Recursive.pm#fcopy() wrote:
fcopy()
This function uses File::Copy's copy() function to copy a file but not a directory. Any directories are recursively created if need be. One difference to File::Copy::copy() is that fcopy attempts to preserve the mode (see Preserving Mode below) The optional $buf in the synopsis if the same as File::Copy::copy()'s 3rd argument returns the same as File::Copy::copy() in scalar context and 1,0,0 in list context to accomidate rcopy()'s list context on regular files.
Haven't used it, but it appears to me that it would 1) copy file and keep permissions etc 2) make path if not existsing but 3) not copy all the files only the one that you indicate.
User avatar
pylkko
 
Posts: 1006
Joined: 2014-11-06 19:02

Re: Bashing a dot file backup script :) [help please]

Postby dasein » 2017-01-19 11:30

I'm starting to suspect homework
User avatar
dasein
 
Posts: 7774
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Bashing a dot file backup script :) [help please]

Postby evandenbroecke » 2017-01-19 13:26

pylkko wrote:What about:
http://search.cpan.org/~dmuey/File-Copy-Recursive-0.38/Recursive.pm#fcopy() wrote:
fcopy()
This function uses File::Copy's copy() function to copy a file but not a directory. Any directories are recursively created if need be. One difference to File::Copy::copy() is that fcopy attempts to preserve the mode (see Preserving Mode below) The optional $buf in the synopsis if the same as File::Copy::copy()'s 3rd argument returns the same as File::Copy::copy() in scalar context and 1,0,0 in list context to accomidate rcopy()'s list context on regular files.
Haven't used it, but it appears to me that it would 1) copy file and keep permissions etc 2) make path if not existsing but 3) not copy all the files only the one that you indicate.


Yes, that's what it's suppose to do.. Just can't figure out the syntax when I want to make the directory that doesnt exist already, having trouble with ignoring the files in the base directory and moving on to creating the folders that dont exist before copying the selected file.

Re: Bashing a dot file backup script :) [help please]
 by dasein » 19 Jan 2017, 03:30

I'm starting to suspect homework


Nope, not in school, but yeah I am teaching myself, so I guess it is homework, in a way... ;)
Todd Vandenbroecke
evandenbroecke@gmail.com
User avatar
evandenbroecke
 
Posts: 32
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

Re: Bashing a dot file backup script :) [help please]

Postby evandenbroecke » 2017-01-19 14:42

fcopy()


I dont believe this is a bash function... looks like perl...
Todd Vandenbroecke
evandenbroecke@gmail.com
User avatar
evandenbroecke
 
Posts: 32
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

Re: Bash script for files to backup..

Postby evandenbroecke » 2017-01-19 15:50

I got this figured out now. Thanks to any that helped out. Turns out I was making way more complicated then I had too, as you probably well can see from the new script above.

Cheers,
Todd Vandenbroecke
evandenbroecke@gmail.com
User avatar
evandenbroecke
 
Posts: 32
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

Re: Bashing a dot file backup script :) [help please]

Postby pylkko » 2017-01-20 12:26

evandenbroecke wrote:
fcopy()


I dont believe this is a bash function... looks like perl...
Uhm... yeah but you were already using sed and there is no reason why you cannot use perl inside a shell script. Also, it seems to me that some things are just easy to do in perl or let's say awk, like stream editing, data selection/rearrangement/summarization. I guess I support a kind of "use what you can find"-type of attitude to these things. But yeah, what you have now apparently works and does not seem to be anyhow convoluted.
User avatar
pylkko
 
Posts: 1006
Joined: 2014-11-06 19:02


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable