mirror of
				https://github.com/kevinveenbirkenbach/computer-playbook.git
				synced 2025-10-31 18:29:21 +00:00 
			
		
		
		
	optimized backups-cleanup
This commit is contained in:
		| @@ -0,0 +1,7 @@ | ||||
| [Unit] | ||||
| Description=delete old backups | ||||
| OnFailure=systemd-email@%n.service | ||||
|  | ||||
| [Service] | ||||
| Type=oneshot | ||||
| ExecStart=/usr/bin/python /usr/local/bin/backups-cleanup.py | ||||
							
								
								
									
										9
									
								
								roles/native-backups-cleanup/files/backups-cleanup.timer
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								roles/native-backups-cleanup/files/backups-cleanup.timer
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| [Unit] | ||||
| Description=starts backups-cleanup.service | ||||
|  | ||||
| [Timer] | ||||
| OnCalendar=19:00 | ||||
| RandomizedDelaySec=1h | ||||
|  | ||||
| [Install] | ||||
| WantedBy=timers.target | ||||
							
								
								
									
										12
									
								
								roles/native-backups-cleanup/handlers/main.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								roles/native-backups-cleanup/handlers/main.yml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| - name: "restart backups-cleanup.service" | ||||
|   systemd: | ||||
|     name: backups-cleanup.service | ||||
|     state: restarted | ||||
|     enabled: yes | ||||
|     daemon_reload: yes | ||||
| - name: "restart backups-cleanup.timer" | ||||
|   systemd: | ||||
|     name: backups-cleanup.timer | ||||
|     state: restarted | ||||
|     enabled: yes | ||||
|     daemon_reload: yes | ||||
| @@ -1,2 +1,3 @@ | ||||
| dependencies: | ||||
| - native-python-pip | ||||
|   - native-python-pip | ||||
|   - native-systemd-email | ||||
|   | ||||
| @@ -1,3 +1,18 @@ | ||||
| - name: install psutil | ||||
|   pip: | ||||
|     name: psutil | ||||
|  | ||||
| - name: create backups-cleanup.py | ||||
|   template: src=backups-cleanup.py.j2 dest=/usr/local/bin/backups-cleanup.py | ||||
|  | ||||
| - name: create backups-cleanup.service | ||||
|   copy: | ||||
|     src: backups-cleanup.service | ||||
|     dest: "/etc/systemd/system/backups-cleanup.service" | ||||
|   notify: restart backups-cleanup.service | ||||
|  | ||||
| - name: create backups-cleanup.timer | ||||
|   copy: | ||||
|     src: backups-cleanup.timer | ||||
|     dest: "/etc/systemd/system/backups-cleanup.timer" | ||||
|   notify: restart backups-cleanup.timer | ||||
|   | ||||
| @@ -2,10 +2,10 @@ | ||||
| import psutil | ||||
| import shutil | ||||
| import os | ||||
| backup_disk_path = "/media/encrypteddrive-sda/" | ||||
| backup_disk_path = "{{backup_disk_path}}" | ||||
| backups_folder_path = os.path.join(backup_disk_path, "Backups/") | ||||
| deleted = True | ||||
| while psutil.disk_usage(backup_disk_path).percent > 50 and deleted: | ||||
| while psutil.disk_usage(backup_disk_path).percent > int({{backup_space_percent}}) and deleted: | ||||
|     deleted = False | ||||
|     print("%d %% of disk %s are used. Freeing space..." % (psutil.disk_usage(backup_disk_path).percent,backup_disk_path)) | ||||
|     for primary_directory in os.listdir(backups_folder_path): | ||||
		Reference in New Issue
	
	Block a user