github.com/canonical/ubuntu-image@v0.0.0-20240430122802-2202fe98b290/tests/lib/external/snapd-testing-tools/tools/tests.backup (about) 1 #!/bin/bash -e 2 # Tool used to backup/restore a specific directory 3 # It is used by the test tool to make sure each test 4 # leaves the test directory as was initially 5 6 show_help() { 7 echo "usage: tests.backup prepare [PATH]" 8 echo " tests.backup restore [PATH]" 9 } 10 11 cmd_prepare() { 12 local BACKUP_PATH=$1 13 14 if [ ! -d "$BACKUP_PATH" ]; then 15 echo "tests.backup: cannot backup $BACKUP_PATH, not a directory" >&2 16 exit 1 17 fi 18 tar cf "${BACKUP_PATH}.tar" "$BACKUP_PATH" 19 } 20 21 cmd_restore() { 22 local BACKUP_PATH=$1 23 if [ -f "${BACKUP_PATH}.tar" ]; then 24 # Find all the files in the path $BACKUP_PATH and delete them 25 # This command deletes also the hidden files 26 find "${BACKUP_PATH}" -maxdepth 1 -mindepth 1 -exec rm -rf {} \; 27 tar -C/ -xf "${BACKUP_PATH}.tar" 28 rm "${BACKUP_PATH}.tar" 29 else 30 echo "tests.backup: cannot restore ${BACKUP_PATH}.tar, the file does not exist" >&2 31 exit 1 32 fi 33 } 34 35 main() { 36 if [ $# -eq 0 ]; then 37 show_help 38 exit 0 39 fi 40 41 while [ $# -gt 0 ]; do 42 case "$1" in 43 -h|--help) 44 show_help 45 exit 46 ;; 47 prepare) 48 local BACKUP_PATH="${2:-$(pwd)}" 49 cmd_prepare "$BACKUP_PATH" 50 exit 51 ;; 52 restore) 53 local BACKUP_PATH="${2:-$(pwd)}" 54 cmd_restore "$BACKUP_PATH" 55 exit 56 ;; 57 -*) 58 echo "tests.backup: unknown option $1" >&2 59 exit 1 60 ;; 61 *) 62 echo "tests.backup: unknown command $1" >&2 63 exit 1 64 ;; 65 esac 66 done 67 } 68 69 main "$@"