backup-server 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. #!/bin/bash
  2. #/###################################################################\
  3. #| Make backup of: |
  4. #| -Web Pages (/var/www) |
  5. #| -SQL Databases (MySQL or MariaDB) |
  6. #| -HTTP Server Configuration (/etc/nginx) |
  7. #| -Let's Encrypt Certificates (/etc/letsencrypt) |
  8. #| -Mail Mailboxes (/var/mail) |
  9. #| -Home Directory (/home) |
  10. #| -GOGS Git Repository (/opt/gogs) |
  11. #| Compressed with: |
  12. #| -Gzip |
  13. #| -Bzip2 |
  14. #| -XZ |
  15. #| -7zip |
  16. #| and encrypted (or not). |
  17. #| |
  18. #| USAGE: backup-server [options] -z|-j|-J|-7 backupdir |
  19. #| HELP: backup-server -h |
  20. #| |
  21. #| Guzmán Castanedo (guzman@castanedo.es) |
  22. #| Version 2.0 (March 2018) |
  23. #| Licence: GPL v3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
  24. #\###################################################################/
  25. function usage {
  26. printf "%s Version 2.0\n" $(basename $0)
  27. printf "Usage: %s [options]... -z|-j|-J|-7 [backupdir]\n" $(basename $0)
  28. printf "Make a encrypted backup of a server compressed with: \n"
  29. printf "\t·Gzip (tar.gz or tar.gz.gpg)\n"
  30. printf "\t·Bzip2 (tar.bz2 or tar.gz.gpg)\n"
  31. printf "\t·XZ (tar.xz or tar.xz.gpg)\n"
  32. printf "\t·7Z (7z)\n"
  33. printf "\n"
  34. printf "Backup Directory:\n"
  35. printf "\tbackupdir: argument to set to directory to save the backup\n"
  36. printf "\t\tDefault: %s\n" $backupDir
  37. printf "Backup Options:\n"
  38. printf "\t-p, --prefix name: prefix for the name of the backup\n"
  39. printf "\t\tDefault: %s\n" $backupPrefix
  40. printf "\t--no-postfix: disable the postfi: the full name will be the prefix\n"
  41. printf "\t\tDefault: prefix-yyyy-mm-dd(date)-XXXXXX(random)\n"
  42. printf "\t--no-remove: no remove old files\n"
  43. printf "\t\tDefault: false\n"
  44. printf "\t--remove-days number: set number of days to considere a backup old\n"
  45. printf "\t\tDefault: %s\n" $deleteDays
  46. printf "\t-u, --user uid: name of the user owner of the backup\n"
  47. printf "\t\tDefault: %s\n" $user
  48. printf "\t-g, --group gid: name of the group owner of the backup\n"
  49. printf "\t\tDefault: %s\n" $group
  50. printf "\t--permision-mask number: octal mask to set accesss permision of the backup\n"
  51. printf "\t\tDefault: %s\n" $permisionMask
  52. printf "\n"
  53. printf "Compression Options:\n"
  54. printf "\t-z, --gzip: compress using gzip (tar.gz or tar.gz.gpg)\n"
  55. printf "\t-j, --bzip2: compress using bzip2 (tar.bz2 or tar.bz2.gpg)\n"
  56. printf "\t-J, --xz: compress using xz (tar.xz or tar.xz.gpg)\n"
  57. printf "\t-7, --7zip: compress using 7z (7z)\n"
  58. printf "\n"
  59. printf "Encryption Options:\n"
  60. printf "\t-k, --key-id ID: set gpg2 public key-id used for encryption\n"
  61. printf "\t\tUse with Gzip, Bzip2 or XZ compression\n"
  62. printf "\t\tDefault: %s\n" $keyID
  63. printf "\t--no-encryption: disable the encryption of the backup\n"
  64. printf "\t\tDefault: %s\n" $noEncryption
  65. printf "\t--7z-password password: set password (ONLY for 7z) (INSECURE)\n"
  66. printf "\t\tDefault: %s\n" $pass7z
  67. printf "\n"
  68. printf "Web Options:\n"
  69. printf "\t--web-dir dir: set web pages directory to backup\n"
  70. printf "\t\tDefault: %s\n" $webDir
  71. printf "\t--no-web: disable backup of web pages\n"
  72. printf "\t\tDefault: %s\n" $noWeb
  73. printf "\n"
  74. printf "SQL Options:\n"
  75. printf "\t--no-sql: disable MySQL/MariaDB backup\n"
  76. printf "\t\tDefault: %s\n" $noSql
  77. printf "\t--sql-user username: set MySQL/MariaDB username\n"
  78. printf "\t\tDefault: %s\n" $mysqluser
  79. printf "\t--sql-password password: set MySQL/MariaDB password (INSECURE)\n"
  80. printf "\t\tDefault: %s\n" $mysqlpass
  81. printf "\n"
  82. printf "Nginx Options:\n"
  83. printf "\t--nginx-dir dir: set nginx configuration directory to backup\n"
  84. printf "\t\tDefault: %s\n" $nginxDir
  85. printf "\t--no-nginx: disable Nginx backup\n"
  86. printf "\t\tDefault: %s\n" $noNginx
  87. printf "\n"
  88. printf "Let's Encrypt Options:\n"
  89. printf "\t--letsencrypt-dir dir: set Let's Encrypt configuration directory to backup\n"
  90. printf "\t\tDefault: %s\n" $letsencryptDir
  91. printf "\t--no-letsencrypt: disable Let's Encrypt backup\n"
  92. printf "\t\tDefault: %s\n" $noLetsencrypt
  93. printf "\n"
  94. printf "Mail Options:\n"
  95. printf "\t--mail-dir dir: set Mailboxes directory to backup\n"
  96. printf "\t\tDefault: %s\n" $mailDir
  97. printf "\t--no-mail: disable mailboxes backup\n"
  98. printf "\t\tDefault: %s\n" $noMail
  99. printf "\n"
  100. printf "Home Options:\n"
  101. printf "\t--home-dir dir: set home directories to backup\n"
  102. printf "\t\tDefault: %s\n" $homeDir
  103. printf "\t--no-home: disable home directory backup\n"
  104. printf "\t\tDefault: %s\n" $noHome
  105. printf "\n"
  106. printf "GOGS Options:\n"
  107. printf "\t--gogs-dir dir: set GOGS Git Repository to backup\n"
  108. printf "\t\tDefault: %s\n" $gogsDir
  109. printf "\t--no-gogs: disable gogs backup\n"
  110. printf "\t\tDefault: %s\n" $noGogs
  111. printf "\n"
  112. printf "Other Options:\n"
  113. printf "\t-h, --help: shows this message and exit\n"
  114. }
  115. function makep7zip {
  116. #Destination file
  117. encrypt7z=""
  118. if [ ! -z $pass7z ];then
  119. encrypt7z="-p$pass7z -mhe"
  120. fi
  121. if [ $noEncryption = true ];then
  122. encrypt7z=""
  123. fi
  124. printf "Backup File (7z):\t%s\n" $backupOutput
  125. #Copy webpages code (except backup and main/public)
  126. if [ $noWeb = false ] && [ -d $webDir ];then
  127. printf "Compressing:\t%s\n" $webDir
  128. tempfile=$(mktemp -t exclude-XXX)
  129. echo "www/backup" > $tempfile
  130. echo "www/main/public" >> $tempfile
  131. 7z a -t7z -mx=9 $encrypt7z $backupOutput $webDir -x@$tempfile > /dev/null
  132. if [ $? != 0 ];then
  133. printf "WARNING:\tError copying web pages (Continue).\n"
  134. fi
  135. rm $tempfile
  136. fi
  137. #Copy MySQL databases (mysqldump)
  138. if [ $noSql = false ] && [ ! -z $mysqluser ] && [ ! -z $mysqlpass ] && [ -x "$(which mysql)" ] && [ -x "$(which mysqldump)" ]; then
  139. list=$(mysql -u $mysqluser -p$mysqlpass -e "show DATABASES;")
  140. #Parse databases expect information_schema & performance_schema
  141. for database in $list; do
  142. valid=true
  143. for excep in Database information_schema performance_schema; do
  144. if [ $database = $excep ]; then
  145. valid=false
  146. break
  147. fi
  148. done
  149. if [ $valid = true ]; then
  150. printf "Compressing MySQL database:\t%s\n" $database.sql
  151. mysqldump -u $mysqluser -p$mysqlpass $database | 7z a -t7z -mx=9 $encrypt7z $backupOutput -simysql/$database.sql > /dev/null 2>&1
  152. if [ $? != 0 ];then
  153. printf "WARNING:\tError compressing database (%s) (Continue).\n" $database
  154. fi
  155. fi
  156. done
  157. fi
  158. #Copy nginx configuration (sites-available)
  159. if [ $noNginx = false ] && [ -d $nginxDir ];then
  160. printf "Compressing:\t%s\n" $nginxDir
  161. 7z a -t7z -mx=9 $encrypt7z $backupOutput $nginxDir > /dev/null
  162. if [ $? != 0 ];then
  163. printf "WARNING:\tError copying nginx configuration (Continue)\n"
  164. fi
  165. fi
  166. #Copy Email (this could be heavy in the future)
  167. if [ $noMail = false ] && [ -d $mailDir ];then
  168. printf "Compressing:\t%s\n" $mailDir
  169. 7z a -t7z -mx=9 $encrypt7z $backupOutput $mailDir > /dev/null
  170. #tar -c -zf - $mailDir | 7z a -t7z -mx=9 -p$pass7z -mhe $backupOutput -simail.tar.gz > /dev/null
  171. if [ $? != 0 ];then
  172. printf "WARNING:\tError copying mailboxes (Continue)\n"
  173. fi
  174. fi
  175. #Copy Certificates (LetsEncrypt)
  176. if [ $noLetsencrypt = false ] && [ -d $letsencryptDir ];then
  177. printf "Compressing:\t%s\n" $letsencryptDir
  178. 7z a -t7z -mx=9 $encrypt7z $backupOutput $letsEncryptDir > /dev/null
  179. if [ $? != 0 ];then
  180. printf "WARNING:\tError copying Let's Encrypt certificates (Continue)\n"
  181. fi
  182. fi
  183. #Copy /home
  184. if [ $noHome = false ] && [ -d $homeDir ];then
  185. printf "Compressing:\t%s\n" $homeDir
  186. 7z a -t7z -mx=9 $encrypt7z $backupOutput $homeDir > /dev/null
  187. if [ $? != 0 ];then
  188. printf "WARNING:\tError copying home dir (Continue)\n"
  189. fi
  190. fi
  191. #Copy GOGS
  192. if [ $noGogs = false ] && [ -d $gogsDir ];then
  193. printf "Compressing:\t%s\n" $gogsDir
  194. 7z a -t7z -mx=9 $encrypt7z $backupOutput $gogsDir > /dev/null
  195. if [ $? != 0 ];then
  196. printf "WARNING:\tError copying GOGS Repository (Continue)\n"
  197. fi
  198. fi
  199. }
  200. function makeTar {
  201. #Destination file
  202. printf "Backup File:\t%s\n" $backupOutput
  203. #Copy webpages code (except backup and main/public)
  204. if [ $noWeb = false ] && [ -d $webDir ];then
  205. printf "Adding:\t%s\n" $webDir
  206. tar -rf $tempOutput -C $(dirname $webDir) --exclude=www/backup --exclude=www/main/public $(basename $webDir) > /dev/null 2>&1
  207. if [ $? != 0 ]; then
  208. printf "WARNING:\tError copying web pages (Continue).\n"
  209. fi
  210. fi
  211. #Copy MySQL databases (mysqldump)
  212. if [ $noSql = false ] && [ ! -z $mysqluser ] && [ ! -z $mysqlpass ] && [ -x "$(which mysql)" ] && [ -x "$(which mysqldump)" ]; then
  213. list=$(mysql -u $mysqluser -p$mysqlpass -e "show DATABASES;" 2> /dev/null)
  214. mkdir /tmp/mysql
  215. #Parse databases expect information_schema & performance_schema
  216. for database in $list; do
  217. valid=true
  218. for excep in Database information_schema performance_schema; do
  219. if [ $database = $excep ]; then
  220. valid=false
  221. break
  222. fi
  223. done
  224. if [ $valid = true ]; then
  225. printf "Adding MySQL database:\t%s\n" $database.sql
  226. mysqldump -u $mysqluser -p$mysqlpass $database > /tmp/mysql/$database.sql 2> /dev/null
  227. if [ $? != 0 ];then
  228. printf "WARNING:\tError extracting database (%s) (Continue).\n" $database
  229. continue
  230. fi
  231. tar -rf $tempOutput -C /tmp mysql/$database.sql > /dev/null 2>&1
  232. if [ $? != 0 ];then
  233. printf "WARNING:\tError adding to tar (%s) (Continue)\n"
  234. fi
  235. rm /tmp/mysql/$database.sql
  236. fi
  237. done
  238. rm -R /tmp/mysql
  239. fi
  240. #Copy nginx configuration (sites-available)
  241. if [ $noNginx = false ] && [ -d $nginxDir ];then
  242. printf "Adding:\t%s\n" $nginxDir
  243. tar -rf $tempOutput -C $(dirname $nginxDir) $(basename $nginxDir) > /dev/null 2>&1
  244. if [ $? != 0 ];then
  245. printf "WARNING:\tError copying nginx configuration (Continue)\n"
  246. fi
  247. fi
  248. #Copy Email (this could be heavy in the future)
  249. if [ $noMail = false ] && [ -d $mailDir ];then
  250. printf "Adding:\t%s\n" $mailDir
  251. tar -rf $tempOutput -C $(dirname $mailDir) $(basename $mailDir) > /dev/null 2>&1
  252. if [ $? != 0 ];then
  253. printf "WARNING:\tError copying mailboxes (Continue)\n"
  254. fi
  255. fi
  256. #Copy Certificates (LetsEncrypt)
  257. if [ $noLetsencrypt = false ] && [ -d $letsencryptDir ];then
  258. printf "Adding:\t%s\n" $letsencryptDir
  259. tar -rf $tempOutput -C $(dirname $letsencryptDir) $(basename $letsencryptDir) > /dev/null 2>&1
  260. if [ $? != 0 ];then
  261. printf "WARNING:\tError copying Let's Encrypt certificates (Continue)\n"
  262. fi
  263. fi
  264. #Copy /home
  265. if [ $noHome = false ] && [ -d $homeDir ];then
  266. printf "Adding:\t%s\n" $homeDir
  267. tar -rf $tempOutput -C $(dirname $homeDir) $(basename $homeDir) > /dev/null 2>&1
  268. if [ $? != 0 ];then
  269. printf "WARNING:\tError copying home dir (Continue)\n"
  270. fi
  271. fi
  272. #Copy GOGS
  273. if [ $noGogs = false ] && [ -d $gogsDir ];then
  274. printf "Adding:\t/opt/gogs\n"
  275. tar -rf $tempOutput -C $(dirname $gogsDir) $(basename $gogsDir) > /dev/null 2>&1
  276. if [ $? != 0 ];then
  277. printf "WARNING:\tError copying GOGS Repository (Continue)\n"
  278. fi
  279. fi
  280. }
  281. function gzUnencrypted {
  282. printf "Compressing (GZIP)..."
  283. gzip -9 --stdout $tempOutput > $backupOutput
  284. if [ $? != 0 ]; then
  285. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  286. exit 1
  287. fi
  288. rm $tempOutput
  289. }
  290. function gzEncrypted {
  291. printf "Compressing (GZIP) and encrypting..."
  292. gzip -9 --stdout $tempOutput | gpg2 --no-batch --output $backupOutput --encrypt -r $keyID -
  293. if [ $? != 0 ]; then
  294. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  295. exit 1
  296. fi
  297. rm $tempOutput
  298. }
  299. function bz2Unencrypted {
  300. printf "Compressing (BZIP2)..."
  301. bzip2 -9 --stdout $tempOutput > $backupOutput
  302. if [ $? != 0 ]; then
  303. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  304. exit 1
  305. fi
  306. rm $tempOutput
  307. }
  308. function bz2Encrypted {
  309. printf "Compressing (BZIP2) and encrypting..."
  310. bzip2 -9 --stdout $tempOutput | gpg2 --no-batch --output $backupOutput --encrypt -r $keyID -
  311. if [ $? != 0 ]; then
  312. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  313. exit 1
  314. fi
  315. rm $tempOutput
  316. }
  317. function xzUnencrypted {
  318. printf "Compressing (XZ)..."
  319. xz -9 --stdout $tempOutput > $backupOutput
  320. if [ $? != 0 ]; then
  321. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  322. exit 1
  323. fi
  324. rm $tempOutput
  325. }
  326. function xzEncrypted {
  327. printf "Compressing (XZ) and encrypting..."
  328. xz -9 --stdout $tempOutput | gpg2 --no-batch --output $backupOutput --encrypt -r $keyID -
  329. if [ $? != 0 ]; then
  330. printf "\nERROR:\tImpossible to compress (%s)\n" $backupOutput$ext
  331. exit 1
  332. fi
  333. rm $tempOutput
  334. }
  335. function checkRoutes {
  336. if [ ! -d $backupDir ];then
  337. printf "ERROR:\tBackup directory don't exist\n"
  338. usage
  339. exit 1
  340. fi
  341. if [ ! -d $webDir ];then
  342. printf "WARNING:\t%s don't exist (no backup)\n" $webDir
  343. fi
  344. if [ ! -d $nginxDir ];then
  345. printf "WARNING:\t%s don't exist (no backup)\n" $nginxDir
  346. fi
  347. if [ ! -d $letsencryptDir ];then
  348. printf "WARNING:\t%s don't exist (no backup)\n" $letsencryptDir
  349. fi
  350. if [ ! -d $mailDir ];then
  351. printf "WARNING:\t%s don't exist (no backup)\n" $mailDir
  352. fi
  353. if [ ! -d $homeDir ];then
  354. printf "WARNING:\t%s don't exist (no backup)\n" $homeDir
  355. fi
  356. if [ ! -d $gogsDir ];then
  357. printf "WARNING:\t%s don't exist (no backup)\n" $gogsDir
  358. fi
  359. }
  360. function checkMethod {
  361. if [ $gzipOn = false ] && [ $bzip2On = false ] && [ $xzOn = false ] && [ $p7zipOn = false ];then
  362. printf "ERROR:\tCompressing method not set\n"
  363. exit 1
  364. fi
  365. if [ $noEncryption = false ];then
  366. if [ $gzipOn = true ] || [ $bzip2On = true ] || [ $xzOn = true ];then
  367. if [ ! -x $(which gpg2) ];then
  368. printf "WARNING:\tGPG2 Not Installed: continue without encryption (type \"%s -h\" for help)\n" $(basename $0)
  369. noEncryption=true
  370. else
  371. if [ ! -z $keyID ];then
  372. ext=$ext.gpg
  373. gpg2 --list-keys $keyID > /dev/null 2>&1
  374. if [ $? != 0 ];then
  375. printf "ERROR:\tKeyID %s don't exist in keyring (\"gpg2 --list-keys\" to see all keys)\n" $keyID
  376. exit 1
  377. fi
  378. else
  379. printf "WARNING:\tKeyID (-k keyID) not set: continue without encryption (type \"%s -h\" for help)\n" $(basename $0)
  380. noEncryption=true
  381. fi
  382. fi
  383. else
  384. if [ -z $pass7z ];then
  385. printf "WARNING:\t7Z password (--7z-password) not set: continue without encryption (type \"%s -h\" for help)\n" $(basename $0)
  386. noEncryption=true
  387. fi
  388. fi
  389. fi
  390. }
  391. function checkSqlAuth {
  392. if [ $noSql = false ];then
  393. if [ -z $mysqluser ] || [ -z $mysqlpass ]; then
  394. printf "WARNING:\tSQL User/Password not set: continue without SQL backup (type \"%s -h\" for help)\n" $(basename $0)
  395. noSql=true
  396. fi
  397. fi
  398. }
  399. #Check root
  400. startTime=$(date +"%s")
  401. if [ $(id -u) -ne 0 ]; then
  402. printf "ERROR:\tNeed to be root :O\n"
  403. exit 1
  404. fi
  405. #Default Data
  406. mysqluser="root"
  407. mysqlpass=""
  408. keyID=""
  409. pass7z=""
  410. backupDir=/var/www/backup
  411. backupPrefix="backup-castanedo.es"
  412. backupPostfix=-$(date +"%Y-%m-%d")-$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1)
  413. deleteDays="15"
  414. user="www-data"
  415. group="www-data"
  416. #Default Routes
  417. webDir="/var/www"
  418. nginxDir="/etc/nginx"
  419. mailDir="/var/mail"
  420. letsencryptDir="/etc/letsencrypt"
  421. homeDir="/home"
  422. gogsDir="/opt/gogs"
  423. #Control Variables
  424. noEncryption=false
  425. gzipOn=false
  426. bzip2On=false
  427. xzOn=false
  428. p7zipOn=false
  429. ext=""
  430. permisionMask=640
  431. removeOld=true
  432. noWeb=false
  433. noSql=false
  434. noNginx=false
  435. noMail=false
  436. noLetsencrypt=false
  437. noHome=false
  438. noGogs=false
  439. #Parse args
  440. TEMP=$(getopt -q -o zjJ7p:u:g:k:h --longoptions gzip,bzip2,xz,7zip,prefix:,no-postfix,no-remove,remove-days:,user:,group:,permision-mask:,key-id:,no-encryption,7z-password:,web-dir:,no-web,no-sql,sql-user:,sql-password:,nginx-dir:,no-nginx,letsencrypt-dir:,no-letsencrypt,mail-dir:,no-mail,home-dir:,no-home,gogs-dir:,no-gogs,help --name $(basename $0) -- $@)
  441. eval set -- $TEMP
  442. unset TEMP
  443. while true; do
  444. case $1 in
  445. -z|--gzip)
  446. gzipOn=true
  447. ext=".tar.gz"
  448. if [ $bzip2On = true ] || [ $xzOn = true ] || [ $p7zipOn = true ];then
  449. printf "ERROR:\tOnly choose one compressing method\n"
  450. exit 1
  451. fi
  452. if [ ! -x $(which gzip) ];then
  453. printf "ERROR:\tGzip Not Installed\n"
  454. exit 1
  455. fi
  456. shift
  457. ;;
  458. -j|--bzip2)
  459. bzip2On=true
  460. ext=".tar.bz2"
  461. if [ $gzipOn = true ] || [ $xzOn = true ] || [ $p7zipOn = true ];then
  462. printf "ERROR:\tOnly choose one compressing method\n"
  463. exit 1
  464. fi
  465. if [ ! -x $(which bzip2) ];then
  466. printf "ERROR:\tBzip2 Not Installed\n"
  467. exit 1
  468. fi
  469. shift
  470. ;;
  471. -J|--xz)
  472. xzOn=true
  473. ext=".tar.xz"
  474. if [ $gzipOn = true ] || [ $bzip2On = true ] || [ $p7zipOn = true ];then
  475. printf "ERROR:\tOnly choose one compressing method\n"
  476. exit 1
  477. fi
  478. if [ ! -x $(which xz) ];then
  479. printf "ERROR:\tXZ Not Installed\n"
  480. exit 1
  481. fi
  482. shift
  483. ;;
  484. -7|--7zip)
  485. p7zipOn=true
  486. ext=".7z"
  487. if [ $gzipOn = true ] || [ $bzip2On = true ] || [ $xzOn = true ];then
  488. printf "ERROR:\tOnly choose one compressing method\n"
  489. exit 1
  490. fi
  491. if [ ! -x $(which 7z) ];then
  492. printf "ERROR:\t7Z Not Installed\n"
  493. exit 1
  494. fi
  495. shift
  496. ;;
  497. -p|--prefix)
  498. backupPrefix=$2
  499. shift 2
  500. ;;
  501. --no-postfix)
  502. backupPostfix=""
  503. shift
  504. ;;
  505. --no-remove)
  506. removeOld=false
  507. shift
  508. ;;
  509. --remove-days)
  510. deleteDays=$2
  511. shift 2
  512. ;;
  513. -u|--user)
  514. grep $2 /etc/passwd > /dev/null 2>&1
  515. if [ $? = 0 ]; then
  516. user=$2
  517. else
  518. printf "ERROR:\tUser %s don't exist\n" $2
  519. exit 1
  520. fi
  521. shift 2
  522. ;;
  523. -g|--group)
  524. grep $2 /etc/group > /dev/null 2>&1
  525. if [ $? = 0 ];then
  526. group=$2
  527. else
  528. printf "ERROR:\tGroup %s don't exist\n" $2
  529. exit 1
  530. fi
  531. shift 2
  532. ;;
  533. --permision-mask)
  534. if [ $2 -eq $2 ] 2> /dev/null;then
  535. permisionMask=$2
  536. else
  537. printf "ERROR:\tPermission has to be a number (%s)\n" $2
  538. exit 1
  539. fi
  540. shift 2
  541. ;;
  542. -k|--key-id)
  543. keyID=$2
  544. shift 2
  545. ;;
  546. --no-encryption)
  547. noEncryption=true
  548. shift
  549. ;;
  550. --7z-password)
  551. pass7z=$2
  552. shift 2
  553. ;;
  554. --web-dir)
  555. webDir=$2
  556. shift 2
  557. ;;
  558. --no-web)
  559. noWeb=true
  560. shift
  561. ;;
  562. --no-sql)
  563. noSql=true
  564. shift
  565. ;;
  566. --sql-user)
  567. mysqluser=$2
  568. shift 2
  569. ;;
  570. --sql-password)
  571. mysqlpass=$2
  572. shift 2
  573. ;;
  574. --nginx-dir)
  575. nginxDir=$2
  576. shift 2
  577. ;;
  578. --no-nginx)
  579. noNginx=true
  580. shift
  581. ;;
  582. --letsencrypt-dir)
  583. letsencryptDir=$2
  584. shift 2
  585. ;;
  586. --no-letsencrypt)
  587. noLetsencrypt=true
  588. shift
  589. ;;
  590. --mail-dir)
  591. mailDir=$2
  592. shift 2
  593. ;;
  594. --no-mail)
  595. noMail=true
  596. shift
  597. ;;
  598. --home-dir)
  599. homeDir=$2
  600. shift 2
  601. ;;
  602. --no-home)
  603. noHome=true
  604. shift
  605. ;;
  606. --gogs-dir)
  607. gogsDir=$2
  608. shift 2
  609. ;;
  610. --no-gogs)
  611. noGogs=true
  612. shift
  613. ;;
  614. -h|--help)
  615. usage
  616. exit 0
  617. shift
  618. ;;
  619. --)
  620. #Last One
  621. shift
  622. break
  623. ;;
  624. *)
  625. #Unspected
  626. usage
  627. printf "\nERROR:\tInvalid Option (%s)\n" $1
  628. exit 1
  629. ;;
  630. esac
  631. done
  632. if [ ! $# -eq 1 ];then
  633. printf "ERROR:\tBackup directory not set\n"
  634. usage
  635. exit 1
  636. else
  637. backupDir=$1
  638. backupName=$backupPrefix$backupPostfix
  639. backupOutput=$backupDir/$backupName
  640. tempOutput=/tmp/$backupName.tar
  641. fi
  642. #Check (routes, compression, encryption and SQL auth)
  643. checkRoutes
  644. checkMethod
  645. checkSqlAuth
  646. #make tar file or 7z
  647. backupOutput=$backupOutput$ext
  648. if [ $p7zipOn = true ];then
  649. if [ ! -x $(which 7z) ];then
  650. printf "ERROR:\tp7zip Not Installed\n"
  651. exit 1
  652. fi
  653. makep7zip
  654. else
  655. if [ ! -x $(which tar) ];then
  656. printf "ERROR:\tTAR Not Installed\n"
  657. exit 1
  658. fi
  659. makeTar
  660. fi
  661. #Compression and encryption (Except 7Zip -> Already done)
  662. if [ $gzipOn = true ];then
  663. if [ $noEncryption = true ];then
  664. gzUnencrypted
  665. else
  666. gzEncrypted
  667. fi
  668. elif [ $bzip2On = true ];then
  669. if [ $noEncryption = true ];then
  670. bz2Unencrypted
  671. else
  672. bz2Encrypted
  673. fi
  674. elif [ $xzOn = true ];then
  675. if [ $noEncryption = true ];then
  676. xzUnencrypted
  677. else
  678. xzEncrypted
  679. fi
  680. else
  681. if [ $p7zipOn = false ];then
  682. printf "ERROR:\tCompression method not set\n"
  683. rm $tempOutput
  684. exit 1
  685. fi
  686. fi
  687. #Permissions
  688. chown $user:$group $backupOutput
  689. chmod $permisionMask $backupOutput
  690. #Remove files older than 15 days
  691. if [ $removeOld = true ];then
  692. printf "\nEliminando backups antiguos (+15 dias)\n"
  693. find $backupDir -mindepth 1 -maxdepth 1 -mtime +$deleteDays -type f -iname $backupPrefix*$ext -delete
  694. if [ $? != 0 ];then
  695. printf "WARNING:\tError eliminando backup's antiguos (%s dias)\n" $deleteDays
  696. fi
  697. fi
  698. #End
  699. finalTime=$(date +"%s")
  700. printf "\n"
  701. echo "------------------------------------------------"
  702. printf "Backup completado con exito en %s segundos :)\n" $((finalTime-startTime))
  703. echo "------------------------------------------------"