简介
下面是备份的脚本
脚本
简介
这个脚本将指定的目录备份打包成 tar.gz
文件,备份到家目录 下的 backup/application 文件夹下,然后使用 rclone 上传到指定的远程存储
创建
1
2
3
4
|
mkdir -p $HOME/bin
touch $HOME/bin/backup_and_upload.sh
chmod +x $HOME/bin/backup_and_upload.sh
vim $HOME/bin/backup_and_upload.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
|
#!/bin/bash
#
source_dir=(
# 你的备份目录,一行一个,例如
/opt/container/server1
/opt/container/server2
)
date_format=$(TZ="UTC" date +%Y%m%d)
# 备份文件的存放位置(可选)
backup_base_dir="$HOME/backup/application"
force_repack=0 # Default: do not force repack
# Define backup functions
# 函数名为你的rclone配置的远程存储名,仿照下面的添加即可
b2() {
remote_dir="rclone远程存储名:远程备份路径"
backup_to_remote "$remote_dir" "$1"
}
# Generic backup function
backup_to_remote() {
local remote_dir="$1"
local source_full_path="$2"
local source_name=$(basename "$source_full_path")
# check output directory
target_dir="$backup_base_dir/$source_name"
if [ ! -d "$target_dir" ]; then
echo "$target_dir directory not exists, creating..."
mkdir -p "$target_dir"
fi
# archive directory
target_full_path="$target_dir/${source_name}_backup_${date_format}.tar.gz"
if [ ! -f "$target_full_path" ] || [ "$force_repack" -eq 1 ]; then
if [ "$force_repack" -eq 1 ]; then
echo "Force repack is enabled. Recreating archive: $target_full_path"
else
echo "Creating archive: $target_full_path"
fi
tar -zcf "$target_full_path" "$source_full_path"
fi
# backup archive file
echo "Starting backup to $remote_dir: ${target_full_path}"
rclone copy "$target_full_path" "$remote_dir/${source_name}" -P
echo ""
}
# Process each source directory
process_directory() {
local dir="$1"
local backup_targets=("${@:2}") # Get all arguments after the directory
if [ -z "$backup_targets" ]; then
echo "No backup targets specified for $dir. Skipping."
return
fi
for target in "${backup_targets[@]}"; do
if type "$target" > /dev/null 2>&1; then
echo "Backing up $dir to $target"
"$target" "$dir" # Call the backup function with the directory
else
echo "Error: Backup target '$target' not found."
fi
done
}
# Main script logic
# Parse command line arguments
while getopts "f" opt; do
case $opt in
f)
force_repack=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $((OPTIND -1)) # Shift off the options, leaving only backup targets
if [ $# -eq 0 ]; then
echo "Usage: $0 [-f] <backup_target> [backup_target2 ...] "
echo " -f: Force re-packaging the local archive file."
echo "Available backup targets: b2"
exit 1
fi
# Loop through each source directory and apply the specified backups
for source_dir_item in "${source_dir[@]}"; do
process_directory "$source_dir_item" "$@"
done
echo "Backup process completed."
|
添加到环境变量(可选)
1
2
|
echo 'export PATH=$PATH:$HOME/bin' >> $HOME/.bashrc
source $HOME/.bashrc
|
使用
运行
1
|
backup_and_upload.sh rclone配置的远程存储名
|
参数
示例
备份到 b2
1
|
backup_and_upload.sh b2
|
执行后将会自动打包备份到家目录的 backup/application
下,然后上传到 b2
定时任务
为了实现定期备份,可以添加定时任务
这个表示每天 0 点 0 分备份上传
1
|
echo "0 0 * * * /root/bin/backup_and_upload.sh b2 tebi >> /root/logs/backup_and_upload.log" >> /var/spool/cron/crontabs/root
|