Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Bash script for files to backup..

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
evandenbroecke
Posts: 39
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

Bash script for files to backup..

#1 Post by evandenbroecke »

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[at]gmail.com

User avatar
cpoakes
Posts: 99
Joined: 2015-03-29 04:54

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

#2 Post by cpoakes »

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
evandenbroecke
Posts: 39
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

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

#3 Post by evandenbroecke »

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[at]gmail.com

User avatar
evandenbroecke
Posts: 39
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

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

#4 Post by evandenbroecke »

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[at]gmail.com

User avatar
evandenbroecke
Posts: 39
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

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

#5 Post by evandenbroecke »

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[at]gmail.com

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

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

#6 Post by pylkko »

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
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

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

#7 Post by dasein »

I'm starting to suspect homework

User avatar
evandenbroecke
Posts: 39
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

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

#8 Post by evandenbroecke »

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[at]gmail.com

User avatar
evandenbroecke
Posts: 39
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

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

#9 Post by evandenbroecke »

fcopy()
I dont believe this is a bash function... looks like perl...
Todd Vandenbroecke
evandenbroecke[at]gmail.com

User avatar
evandenbroecke
Posts: 39
Joined: 2017-01-06 21:31
Location: Abbotsford, BC Canada

Re: Bash script for files to backup..

#10 Post by evandenbroecke »

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[at]gmail.com

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

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

#11 Post by pylkko »

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.

Post Reply