SHARE
TWEET

mkbackup.sh

a guest Jun 21st, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Manage backups using SHA256 values. (Inspired by hash tables, but far poorer by design.)
  2. # Syntax: mkbackup.sh [filename]
  3. # WIP: Migrate to python3.
  4. # WIP: Remove table and tarball entries based on backup_dir contents.
  5.  
  6.  
  7. # Create a csv table: filename,sha256sum_of_filename
  8. BackUp()
  9. {
  10.  
  11.   # Set backup directory.
  12.   backup_dir="$1"
  13.  
  14.   # Send filename to a tar archive, if it's new.
  15.   toArchive()
  16.   {
  17.     if [[ $1 == "pass" ]]; then
  18.       return 0
  19.     else
  20.         filename="$2"
  21.         if [[ $1 == "append" ]]; then
  22.           mode="-rf"
  23.         # If a second parameter is passed, use it as the tarball filename.
  24.         else
  25.           mode="-uf"
  26.         if [[ "$3" ]]; then
  27.           tarball="$3"
  28.         fi
  29.         # Append to the tarball directory.
  30.         tar $mode $tarball "$filename"
  31.         echo "$filename added to $tarball.\n "
  32.       fi
  33.     fi
  34.   }
  35.  
  36.   # Test existing entries.
  37.   testEntry()
  38.   {
  39.     filename="$1"
  40.     tarball="$USER-backup.tar"
  41.     sha="$2"
  42.     if [[ $3 ]]; then
  43.       backup_table="$3"
  44.     else
  45.       backup_table="backup_table"
  46.     fi
  47.     # Test if the filename is there, but the SHA value changed.
  48.     if [[ $(grep "$filename," $backup_table) ]] && [[ ! $(grep "$sha" $backup_table) ]]; then
  49.         # Remove trailing characters
  50.       filename_sed="${filename//\//\\/}"
  51.         sed -i "s/$filename_sed,.*/$filename_sed,$sha/g" $backup_table && \
  52.         echo "Entry updated."
  53.       mode="append"
  54.       unset filename_sed
  55.     # Check if the file isn't in the tarball.
  56.     elif [[ ! $(tar -tf $tarball 2> /dev/null | grep -w "$filename") ]]; then
  57.       # If the entry does not exist at all, add it.
  58.       if [[ ! $(grep -w "$filename,$sha" $backup_table) ]]; then
  59.         # If it is not there exactly, append it to the table.
  60.         echo "$filename,$sha" >> $backup_table && \
  61.         echo "Added entry to table."
  62.       fi
  63.       mode="update"
  64.     # If it's there and unchanged, a null value will stop toArchive() from running.
  65.     else
  66.       filename=""
  67.       mode="pass"
  68.     fi
  69.     # If something should be sent to a tar archive, send it.
  70.     toArchive "$mode" "$filename"
  71.   }
  72.  
  73.   # Loop through everything in the backup directory.
  74.   loopDir()
  75.   {
  76.     for i in "$1"/*; do
  77.       if [ -d "$i" ]; then
  78.         echo "dir: $i" && \
  79.         loopDir "$i"
  80.       elif [ -f "$i" ]; then
  81.         filename="$i"
  82.         echo "file: $filename"
  83.         sha=$(sha256sum "$filename" | sed 's/ .*//g')
  84.         testEntry "$filename" "$sha"
  85.       fi
  86.     done
  87.   }
  88.  
  89.   # Init.
  90.   loopDir $backup_dir
  91.   # You could even build on it:
  92.   #gzip2 $tarball
  93.   #gpg2 --cipher-algo AES-256 $tarball
  94.   #rsync -v $encrypted_file user@server:/backup/storage/space
  95.  
  96. }
  97.  
  98.  
  99. BackUp "$1"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top