-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_nginx_symlinks.py
More file actions
140 lines (125 loc) · 4.16 KB
/
backup_nginx_symlinks.py
File metadata and controls
140 lines (125 loc) · 4.16 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
backup_nginx_symlinks
---------------------
Create or overwrite a file called `/opt/restore-nginx-symlinks.sh` if
the file is writable. Change the directory by setting the LP_BACKUPS
environment variable to something other than /opt.
Usually you must run this as root.
Running the generated `sh` file will remove all symlinks (not files) in
/etc/nginx/sites-enabled and re-add symlinks that were there on the
last run of `backup-nginx-symlinks`.
'''
from __future__ import print_function
import sys
import locale
import os
# from datetime import datetime
import time
if __name__ == "__main__":
SUBMODULE_DIR = os.path.dirname(os.path.realpath(__file__))
MODULE_DIR = os.path.dirname(os.path.dirname(SUBMODULE_DIR))
sys.path.insert(0, os.path.dirname(MODULE_DIR))
from linuxpreinstall.lplogging import (
echo0,
)
from linuxpreinstall.logging2 import getLogger
logger = getLogger(__name__)
LC_ALL = os.environ.get('LC_ALL')
if LC_ALL is not None:
locale.setlocale(locale.LC_ALL, LC_ALL)
else:
# datetime.now().strftime("%c")
# BEFORE: 'Fri May 27 07:20:46 2022'
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
# AFTER: 'Fri 27 May 2022 07:20:54 AM '
# dt_fmt = "%c" # based on locale (see `else` case above)
dt_fmt = "%a %d %b %Y %H:%M:%S %p %Z"
# ^ match linux 'date' command output in US locale, like
# 'Fri 27 May 2022 07:14:49 AM EDT'
# ^ BUT instead of "EDT" it is blank for datetime.now(),
# even after setlocale!
# date_s = datetime.now().strftime(dt_fmt)
date_s = time.strftime("%c", time.localtime(time.time()))
EUID = os.geteuid()
if EUID != 0:
logger.error(
"You must be root to run {} but EUID is {}."
.format(sys.argv[0], EUID))
sys.exit(1)
LP_BACKUPS = os.environ.get("/opt")
if LP_BACKUPS is None:
LP_BACKUPS = "/opt"
restore_path = os.path.join(LP_BACKUPS, "restore-nginx-symlinks.sh")
enabled_path = "/etc/nginx/sites-enabled"
heading = '''#!/bin/bash
# DO NOT EDIT THIS FILE DIRECTLY:
# It is generated and will be overwritten
# by the next run of "{}".
'''.format(sys.argv[0])
pre_restore_deletes_or_errors = '''echo "* removing symlinks in {ep}:"
for site in `ls {ep}`
do
DEST="`readlink {ep}/$site`"
if [ "$DEST" != "" ]; then
echo "* removing symlink. Undo via: ln -s $DEST {ep}/$site"
rm "{ep}/$site"
else
echo "# skipping 'rm' for real file \\"{ep}/$site\\""
fi
done
'''
restore_or_error = '''DEST="{dest}"
if [ ! -f "$DEST" ]; then
echo "Error: Restore files before running $0 to try to fix missing:"
echo "# ln -s $DEST {ep}/{site}"
code=1
else
echo "* running 'ln -s \\"$DEST\\" \\"{ep}/{site}\\"'"
ln -s "$DEST" "{ep}/{site}"
fi
'''
def main():
with open(restore_path, 'w') as outs:
outs.write(heading)
outs.write(pre_restore_deletes_or_errors.format(
ep=enabled_path,
))
outs.write("code=0\n")
subs = os.listdir(enabled_path)
count = 0
for site in subs:
subPath = os.path.join(enabled_path, site)
if os.path.islink(subPath):
count += 1
if count < 1:
outs.write(
'echo "There are no symlink(s)'
' to restore for sites-enabled"\n'
)
else:
outs.write(
'echo "{} symlink(s) to restore in sites-enabled:"\n'
''.format(count)
)
for site in subs:
subPath = os.path.join(enabled_path, site)
if os.path.islink(subPath):
dest = os.readlink(subPath)
outs.write(restore_or_error.format(
dest=dest,
ep=enabled_path,
site=site,
))
# if [ $? -ne 0 ]; then exit 1; fi
else:
outs.write('# skipping "ln -s" for real file "{}"\n'
''.format(subPath))
echo0("{} is complete {}.".format(restore_path, date_s))
outs.write("echo \"got nginx symlinks from {}\"\n"
"".format(date_s))
outs.write("exit $code\n")
return 0
if __name__ == "__main__":
sys.exit(main())