-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaiida-backup.sh
executable file
·118 lines (101 loc) · 3.96 KB
/
aiida-backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
# CLI OPTIONS (IGNORE!) #################################################################
show_help() {
echo
echo "Usage: ./aiida-backup.sh [-h] [-e env-type] [-c conda-env] [-v venv-path] [-n project] [-p profiles]"
echo
echo "Examples:"
echo " conda: ./aiida-backup.sh -e conda -c <conda-env> -n <project>"
echo " venv: ./aiida-backup.sh -e venv -v aiida2.5 -n <project>"
echo " aiida-project: ./aiida-backup.sh -e aiida-project -n <project>"
echo
echo "Options:"
echo " -h Show this help message"
echo " -e env-type The type of environment to activate (conda, venv, aiida-project)"
echo " -c conda-env The name of the Conda environment to activate"
echo " -v venv-path The path to the virtual environment to activate"
echo " -n project The name of the AiiDA project (required)"
echo " -p profiles The names of AiiDA profiles to backup (optional) - if not specified, all profiles will be backed up"
echo
echo "NOTE: "
echo " The project argument is required for backups, which assume a ROOT -> PROJECT -> PROFILE structure in the backup folder."
echo " If you do not already have a project name, we recommend using 'default', in which case, you would use (for example):"
echo
echo " ./aiida-backup.sh -e conda -c <conda-env> -n default"
echo
}
if [ "$#" -eq 0 ] || [ "${1:0:1}" != "-" ]; then
show_help
exit 0
fi
while getopts "he:c:v:a:n:p:" opt; do
case "${opt}" in
h) show_help && exit 0 ;;
e) env=$OPTARG ;;
c) conda_env=$OPTARG ;;
v) venv_path=$OPTARG ;;
n) project=$OPTARG ;;
p) profiles=$OPTARG ;;
*) echo "Invalid argument. Run with -h for help." && exit 1 ;;
esac
done
if [ ! "$project" ]; then
echo "Project name not specified. Use -n <name>" && exit 1
fi
home=$HOME # default home directory
case "${env}" in
conda)
if [ ! "$conda_env" ]; then
echo "Conda environment not specified. Use -c <name>" && exit 1
fi
source "$(conda info --base)/etc/profile.d/conda.sh"
conda activate "$conda_env"
;;
venv)
if [ ! "$venv_path" ]; then
echo "Virtual environment path not specified. Use -v <path>"
exit 1
fi
source "$venv_path/bin/activate"
;;
aiida-project)
if [ ! -f "$HOME/.aiida_project.env" ]; then
echo ".aiida_project.env not found in user home directory. Is aiida-project initialized?" && exit 1
fi
export "$(grep -v '^#' "$HOME/.aiida_project.env" | xargs)"
source "$aiida_venv_dir/$project/bin/activate"
home="$aiida_home_dir/$project" # point to project home directory
;;
*)
echo "Python environment type not specified. Use -e <conda|venv|aiida-project>" && exit 1
;;
esac
repodir="$home/.aiida/repository" # used to find profiles to backup
# ROOT DIRECTORY FOR ALL BACKUPS ########################################################
ROOT="/home/edanb/PSI/group/aiida-backup-setup/backups/aiida"
# BACKUP UTILITY ########################################################################
backup() {
local project=$1
local profiles=$2
local log_file="$ROOT/$project/backup.log"
# Overwrite log file, if exists
mkdir -p "$ROOT/$project"
echo -e "\nBacking up \"$project\" project" 2>&1 | tee "$log_file"
# Find all profiles if none specified
if [ ! "$profiles" ]; then
if [ -d "$repodir" ]; then
profiles=$(ls "$repodir")
else
echo "No profiles found!" | tee -a "$log_file" && exit 1
fi
fi
# Backup each profile
for profile in $profiles; do
path="$ROOT/$project/$profile"
mkdir -p "$path"
echo -e "\nBacking up \"$profile\" profile to $path \n" 2>&1 | tee -a "$log_file"
(verdi -p "$profile" storage backup "$path") 2>&1 | tee -a "$log_file"
done
}
# RUN BACKUP UTILITY ####################################################################
backup "$project" "$profiles"