How to backup multiple folders & files using Backup.sh script
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
Solution 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"