-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_cron.sh
More file actions
executable file
·77 lines (67 loc) · 2.36 KB
/
Copy pathsetup_cron.sh
File metadata and controls
executable file
·77 lines (67 loc) · 2.36 KB
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
#!/bin/bash
# Script to set up cron job for automatic repository mirroring
# This will run the mirror script daily at midnight (00:00)
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MIRROR_SCRIPT="$SCRIPT_DIR/mirror_repos.sh"
CRON_JOB="0 0 * * * $MIRROR_SCRIPT"
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${YELLOW}========================================${NC}"
echo -e "${YELLOW}GitHub Repository Mirror - Cron Setup${NC}"
echo -e "${YELLOW}========================================${NC}"
echo ""
# Check if mirror script exists and is executable
if [ ! -f "$MIRROR_SCRIPT" ]; then
echo -e "${RED}Error: mirror_repos.sh not found at $MIRROR_SCRIPT${NC}"
exit 1
fi
if [ ! -x "$MIRROR_SCRIPT" ]; then
echo -e "${YELLOW}Making mirror_repos.sh executable...${NC}"
chmod +x "$MIRROR_SCRIPT"
fi
# Check if cron job already exists
if crontab -l 2>/dev/null | grep -F "$MIRROR_SCRIPT" >/dev/null 2>&1; then
echo -e "${YELLOW}Cron job already exists for mirror_repos.sh${NC}"
echo ""
echo "Current cron entry:"
crontab -l | grep -F "$MIRROR_SCRIPT"
echo ""
read -p "Do you want to remove and re-add it? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Remove existing cron job
crontab -l | grep -v -F "$MIRROR_SCRIPT" | crontab -
echo -e "${GREEN}Removed existing cron job${NC}"
else
echo "Keeping existing cron job. Exiting."
exit 0
fi
fi
# Add new cron job
(crontab -l 2>/dev/null || true; echo "$CRON_JOB") | crontab -
echo -e "${GREEN}✓ Cron job added successfully!${NC}"
echo ""
echo "Cron schedule: Daily at midnight (00:00)"
echo "Command: $MIRROR_SCRIPT"
echo ""
echo -e "${YELLOW}Current crontab:${NC}"
crontab -l
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Setup complete!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "The backup script will now run automatically every day at midnight."
echo "You can also run it manually at any time: $MIRROR_SCRIPT"
echo ""
echo "To view cron logs, check your system logs:"
echo " - Ubuntu/Debian: grep CRON /var/log/syslog"
echo " - Other systems: journalctl -u cron"
echo ""
echo "To remove the cron job later, run:"
echo " crontab -e"
echo " (and delete the line containing mirror_repos.sh)"