How to backup multiple folders & files using Backup.sh script

18,663

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"
Share:
18,663

Related videos on Youtube

Wyzer
Author by

Wyzer

Updated on September 18, 2022

Comments

  • Wyzer
    Wyzer over 1 year

    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
    
    • Wyzer
      Wyzer about 7 years
      My new code from my understanding from Dorian dest="/home/wyzer/Downloads/Scripts/Test_Folder" folders=( "/home/wyzer/.config/google-chrome/Default/Local*/kbmfpngjjg‌​dllneeigpgjifpgocmfg‌​mb" "/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
  • Delorean
    Delorean about 7 years
    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.
  • Wyzer
    Wyzer about 7 years
    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?
  • Delorean
    Delorean about 7 years
    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.
  • Wyzer
    Wyzer about 7 years
    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.
  • Delorean
    Delorean about 7 years
    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.
  • Delorean
    Delorean about 7 years
    I added comments to the code to try to explain it better.
  • Wyzer
    Wyzer about 7 years
    Dorian, maybe due to my lack of understanding, should the files=( "/folder1" ...) be my actual file/folders location? Like this `files=( "/home/wyzer/.config/google-chrome/Default/Local*/kbmfpngjjg‌​‌​dllneeigpgjifpgocm‌​fg‌​mb" ... ) Correct?
  • Delorean
    Delorean about 7 years
    @Wyzer Yes that's correct. My code was just an example.
  • Delorean
    Delorean about 7 years
    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.