-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackup.sh
executable file
·75 lines (62 loc) · 1.88 KB
/
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
#!/bin/bash
# Internal variable for the path to the root of the git repository
backups_repo_root="/home/ilya/dev/backups" # Replace with your actual repository path
# Function to display usage information
usage() {
echo "Usage: $0 backup_name source_path dest_path [--delete] [--no-commit]"
echo " backup_name: Name of the backup"
echo " source_path: Path to the source directory"
echo " dest_path: Path to the destination directory"
echo " --delete: Pass this option to delete files in dest_path not present in source_path"
echo " --no-commit: Pass this option to skip git commit"
exit 1
}
# Check for --help option
if [ "$1" == "--help" ]; then
usage
fi
# Validate the number of arguments
if [ "$#" -lt 3 ]; then
echo "Error: Missing arguments."
usage
fi
# Assign command line arguments
backup_name="$1"
source_path="$2"
dest_path="$3"
delete_flag=""
commit_flag=true
# Check for optional arguments
for arg in "$@"; do
if [ "$arg" == "--delete" ]; then
delete_flag="--delete"
elif [ "$arg" == "--no-commit" ]; then
commit_flag=false
fi
done
# Run rsync command
rsync_cmd="rsync -rtvu $delete_flag \"$source_path\" \"$dest_path\""
eval $rsync_cmd
# Check if rsync was successful
if [ $? -ne 0 ]; then
echo "Error: Rsync command failed."
exit 1
fi
# Proceed with git operations if commit_flag is true
if $commit_flag; then
# Navigate to the git repository
cd "$backups_repo_root"
# Stash any changes, checkout master, and pull latest changes
git stash
git checkout master
git pull
# Log the backup
current_date=$(date)
echo "$current_date :: $rsync_cmd" >> "$backups_repo_root/backup_drives/last_backup.log"
# Commit and push the changes
git add --all .
git commit -m "$current_date Backup : $backup_name"
git push
fi
# Print completion message
echo "Backup completed successfully."