Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Manage backups using SHA256 values. (Inspired by hash tables, but far poorer by design.)
- # Syntax: mkbackup.sh [filename]
- # WIP: Migrate to python3.
- # WIP: Remove table and tarball entries based on backup_dir contents.
- # Create a csv table: filename,sha256sum_of_filename
- BackUp()
- {
- # Set backup directory.
- backup_dir="$1"
- # Send filename to a tar archive, if it's new.
- toArchive()
- {
- if [[ $1 == "pass" ]]; then
- return 0
- else
- filename="$2"
- if [[ $1 == "append" ]]; then
- mode="-rf"
- # If a second parameter is passed, use it as the tarball filename.
- else
- mode="-uf"
- if [[ "$3" ]]; then
- tarball="$3"
- fi
- # Append to the tarball directory.
- tar $mode $tarball "$filename"
- echo "$filename added to $tarball.\n "
- fi
- fi
- }
- # Test existing entries.
- testEntry()
- {
- filename="$1"
- tarball="$USER-backup.tar"
- sha="$2"
- if [[ $3 ]]; then
- backup_table="$3"
- else
- backup_table="backup_table"
- fi
- # Test if the filename is there, but the SHA value changed.
- if [[ $(grep "$filename," $backup_table) ]] && [[ ! $(grep "$sha" $backup_table) ]]; then
- # Remove trailing characters
- filename_sed="${filename//\//\\/}"
- sed -i "s/$filename_sed,.*/$filename_sed,$sha/g" $backup_table && \
- echo "Entry updated."
- mode="append"
- unset filename_sed
- # Check if the file isn't in the tarball.
- elif [[ ! $(tar -tf $tarball 2> /dev/null | grep -w "$filename") ]]; then
- # If the entry does not exist at all, add it.
- if [[ ! $(grep -w "$filename,$sha" $backup_table) ]]; then
- # If it is not there exactly, append it to the table.
- echo "$filename,$sha" >> $backup_table && \
- echo "Added entry to table."
- fi
- mode="update"
- # If it's there and unchanged, a null value will stop toArchive() from running.
- else
- filename=""
- mode="pass"
- fi
- # If something should be sent to a tar archive, send it.
- toArchive "$mode" "$filename"
- }
- # Loop through everything in the backup directory.
- loopDir()
- {
- for i in "$1"/*; do
- if [ -d "$i" ]; then
- echo "dir: $i" && \
- loopDir "$i"
- elif [ -f "$i" ]; then
- filename="$i"
- echo "file: $filename"
- sha=$(sha256sum "$filename" | sed 's/ .*//g')
- testEntry "$filename" "$sha"
- fi
- done
- }
- # Init.
- loopDir $backup_dir
- # You could even build on it:
- #gzip2 $tarball
- #gpg2 --cipher-algo AES-256 $tarball
- #rsync -v $encrypted_file user@server:/backup/storage/space
- }
- BackUp "$1"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement