0

I am trying to create a script that runs at log-in or boot that backups some specific folders & files. I can backup only the last line of the "# What to Backup." folder and file locations.

I can move any one of those locations to the last line under # What to Backup., and that is all that will backup to the archive. Is there another command that I am missing to be able complete multiple backups of different folder & files, or will I have to create a backup.sh for each and every file/folder I want to backup?

If I do have to create independent backup(NAME).sh scripts will I have to add some type of delay between each running script?

#!/bin/bash
####################################
#
# Backup script.
#
####################################

# What to backup. 
backup_files="/home/wyzer/.config/google-chrome/Default/Local*/kbmfpngjjgdllneeigpgjifpgocmfgmb"
backup_files="/home/wyzer/.config/google-chrome/Default/Favicons*"
backup_files="/home/wyzer/.config/google-chrome/Default/Favicons-journal*"
backup_files="/home/wyzer/.config/google-chrome/Default/Google*.png"


# Where to backup to.
dest="/home/wyzer/Downloads/Scripts/Test_Folder"

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-TEST-$day.tgz"

# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest

Thanks to Dorian for providing some very good information I was able to figure out how to backup the files I wanted. Turns out I just needed to inform TAR what folder needed to be backed up. Here is my Script as it stands now.

#!/bin/bash
####################################
#
# Backup script.
#
####################################

# What to backup. 
backup_files1="/home/wyzer/.config/google-chrome/Default/Local*/kbmfpngjjgdllneeigpgjifpgocmfgmb"
backup_files2="/home/wyzer/.config/google-chrome/Default/Favicons"
backup_files3="/home/wyzer/.config/google-chrome/Default/Google*.png"
backup_files4="/home/wyzer/.config/google-chrome/Default/Favicons-journal"

# Where to backup to.
dest="/home/wyzer/Downloads/Scripts/Test_Folder"

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"

# Print start status message.
echo "Backing up $backup_files1 to $dest/$archive_file"
echo "Backing up $backup_files2 to $dest/$archive_file"
echo "Backing up $backup_files3 to $dest/$archive_file"
echo "Backing up $backup_files4 to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files1 $backup_files2 $backup_files3 $backup_files4

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest
  • My new code from my understanding from Dorian dest="/home/wyzer/Downloads/Scripts/Test_Folder" folders=( "/home/wyzer/.config/google-chrome/Default/Local*/kbmfpngjjgdllneeigpgjifpgocmfgmb" "/home/wyzer/.config/google-chrome/Default/Favicons" ) for i in "${folders[@]}" do day=$(date +%A) hostname=$(hostname -s) archive_file="$hostname-TEST-$day.tgz" # Print start status message. echo "Backing up $i to $dest/$archive_file" date echo # Backup the files using tar. tar czf $dest/$archive_file $i done – Wyzer Mar 22 '17 at 18:00
1

This is because you're continually overwriting the value of backup_files, so only the last one is what is stored in the variable.

You need to make it an array, and then loop through each item in the array.

dest="/home/wyzer/Downloads/Scripts/Test_Folder"

#The line below will store multiple folders in an array (files)
files=( "/folder1" "/folder2" "/home/username/anotherfolder" )
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-TEST-$day.tgz"


for i in "${files[@]}"  # For every line in FILES
do # Do the following, starting with the first one:
    # Print start status message.
    echo "Backing up $i to $dest/$archive_file"  # Here, $i will be the first directory
    date
    echo

    # Backup the files using tar.
    tar czf $dest/$archive_file $i # Again, $i is the directory    
done # Stop here and do the loop again with the next item in FILES

# Print end status message.
echo
echo "Backup finished"
  • Upon looking again, it might make more sense to name files to folders, but that is up to you what you want to call it. I'm commenting because I don't think it was worth an edit. – Dorian Mar 22 '17 at 16:46
  • Dorian, thank you so much for the reply and pointing out my error. I am not familiar with Scripting nor this syntax, and very much appreciate your input. Yet it appears I need some kind of "loop" for the archiving, as it appears once the archive is created, it only holds the last "File" and not the others. Maybe I am missing something from your code? – Wyzer Mar 22 '17 at 17:51
  • Not a problem! I'm glad to help. If you found my answer to be of help to you, please don't forget to accept it by clicking on the checkmark next to my answer. – Dorian Mar 22 '17 at 17:53
  • Dorian, sorry did not know that "enter" would end my comment and you replied prior to the edit I was working. As I stated earlier, maybe I am missing something in your code, not familiar with scripting or the syntax, yet it appears I need some kind of "loop" for creating the archive folders, as it appears the "next" overwrites the "next" so to speak. – Wyzer Mar 22 '17 at 17:59
  • Yes, the loop is in the code I posted. The for command will go through each item in files (each directory) and will loop through the code between the do and done lines. Every time it loops through, the $i variable will be next directory in the files array. – Dorian Mar 22 '17 at 18:02
0

Here is how the code looks for multiple file/folders, the key from the Script I provided was informing TAR how to "parse" the folders it is backing up.

Just add more backup_filesX= and don't forget to add the echo "Backing up $backup_filesX to.. if you want to see how it was backed up.

Of course another critical is the TAR line make sure to add to the # Backup the files using tar.1 with additional $backup_filesX commands.

Thanks again to Dorian for helping me. Much Appreciated.

#!/bin/bash
####################################
#
# Backup script.
#
####################################

# What to backup. 
backup_files1="/home/wyzer/.config/google-chrome/Default/Local*/kbmfpngjjgdllneeigpgjifpgocmfgmb"
backup_files2="/home/wyzer/.config/google-chrome/Default/Favicons"
backup_files3="/home/wyzer/.config/google-chrome/Default/Google*.png"
backup_files4="/home/wyzer/.config/google-chrome/Default/Favicons-journal"

# Where to backup to.
dest="/home/wyzer/Downloads/Scripts/Test_Folder"

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"

# Print start status message.
echo "Backing up $backup_files1 to $dest/$archive_file"
echo "Backing up $backup_files2 to $dest/$archive_file"
echo "Backing up $backup_files3 to $dest/$archive_file"
echo "Backing up $backup_files4 to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files1 $backup_files2 $backup_files3 $backup_files4

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest
  • This is another way to do it, but as you can see, it makes for longer code. If you want to add or remove location, you need to add or remove code in several locations. The difference with the code I posted was that you only need to add or remove a folder in the file=( "/folder") line, and nothing else. It can be one location, or 50, and the loop will process them all. – Dorian Mar 22 '17 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.